Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <bitset>
- using namespace std;
- struct base64_block
- {
- unsigned char c1;
- unsigned char c2;
- unsigned char c3;
- };
- struct base64_encodedblock
- {
- bitset<6> b1;
- bitset<6> b2;
- bitset<6> b3;
- bitset<6> b4;
- };
- char Base64Table[65] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p' ,'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'};
- int main(int argc, char *argv[])
- {
- string testo ("cia");
- double passes = ceil( (double) testo.length() / 3 );
- vector<struct base64_encodedblock> blocks;
- cout << "Passes for '" << testo.c_str() << "': " << passes << endl;
- for (int i = 0; i < passes; i++)
- {
- string buff = testo.substr(i*3, 3);
- base64_block block;
- base64_encodedblock eblock;
- memset(&block, 0, sizeof(base64_block));
- memset(&eblock, 0, sizeof(base64_encodedblock));
- cout << i << " - buff: " << buff.c_str() << endl;
- block.c1 = buff[0];
- if (buff.length() > 1)
- {
- block.c2 = buff[1];
- if (buff.length() > 2)
- block.c3 = buff[2];
- else
- block.c3 = 0;
- }
- else
- {
- block.c2 = 0;
- block.c3 = 0;
- }
- cout << " - - Ascii of '" << block.c1 << "' is " << (short) block.c1 << endl;
- cout << " - - Ascii of '" << block.c2 << "' is " << (short) block.c2 << endl;
- cout << " - - Ascii of '" << block.c3 << "' is " << (short) block.c3 << endl;
- bitset<8> bc1(block.c1);
- bitset<8> bc2(block.c2);
- bitset<8> bc3(block.c3);
- // inverto i bit
- bool temp;
- for (int j = 0; j < 4; j++)
- {
- temp = bc1[j];
- bc1[j] = bc1[7-j];
- bc1[7-j] = temp;
- }
- for (int j = 0; j < 4; j++)
- {
- temp = bc2[j];
- bc2[j] = bc2[7-j];
- bc2[7-j] = temp;
- }
- for (int j = 0; j < 4; j++)
- {
- temp = bc3[j];
- bc3[j] = bc3[7-j];
- bc3[7-j] = temp;
- }
- // li divido per 6
- for (int j = 0; j < 6; j++)
- {
- eblock.b1[j] = bc1[j];
- }
- eblock.b2[0] = bc1[6];
- eblock.b2[1] = bc1[7];
- for (int j = 0; j < 4; j++)
- {
- eblock.b2[j+2] = bc2[j];
- }
- for (int j = 0; j < 4; j++)
- {
- eblock.b3[j] = bc2[j+4];
- }
- eblock.b3[4] = bc3[0];
- eblock.b3[5] = bc3[1];
- for (int j = 0; j < 6; j++)
- {
- eblock.b4[j] = bc3[j+2];
- }
- cout << "ASCII in binario: " << endl;
- for (int j = 0; j < 8; j++)
- {
- cout << bc1[j];
- }
- cout << endl;
- for (int j = 0; j < 8; j++)
- {
- cout << bc2[j];
- }
- cout << endl;
- for (int j = 0; j < 8; j++)
- {
- cout << bc3[j];
- }
- cout << endl << endl;
- for (int j = 0; j < 6; j++)
- {
- cout << eblock.b1[j];
- }
- cout << endl;
- for (int j = 0; j < 6; j++)
- {
- cout << eblock.b2[j];
- }
- cout << endl;
- for (int j = 0; j < 6; j++)
- {
- cout << eblock.b3[j];
- }
- cout << endl;
- for (int j = 0; j < 6; j++)
- {
- cout << eblock.b4[j];
- }
- cout << endl;
- blocks.push_back(eblock);
- }
- cout << endl;
- string encoded ("");
- for (vector<struct base64_encodedblock>::iterator i = blocks.begin(); i != blocks.end(); i++)
- {
- char buf[5];
- buf[0] = Base64Table[(*i).b1.to_ulong()];
- buf[1] = Base64Table[(*i).b2.to_ulong()];
- buf[2] = Base64Table[(*i).b3.to_ulong()];
- buf[3] = Base64Table[(*i).b4.to_ulong()];
- buf[4] = '\0';
- cout << (*i).b1.to_ulong() << endl;
- cout << (*i).b2.to_ulong() << endl;
- cout << (*i).b3.to_ulong() << endl;
- cout << (*i).b4.to_ulong() << endl;
- encoded += buf;
- }
- cout << encoded;
- getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement