Advertisement
Guest User

Untitled

a guest
Feb 10th, 2013
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.62 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <bitset>
  4.  
  5. using namespace std;
  6.  
  7. struct base64_block
  8. {
  9.     unsigned char c1;
  10.     unsigned char c2;
  11.     unsigned char c3;
  12. };
  13.  
  14. struct base64_encodedblock
  15. {
  16.     bitset<6> b1;
  17.     bitset<6> b2;
  18.     bitset<6> b3;
  19.     bitset<6> b4;
  20. };
  21.  
  22. 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', '+', '/'};
  23.  
  24. int main(int argc, char *argv[])
  25. {
  26.     string testo ("cia");
  27.    
  28.     double passes = ceil( (double) testo.length() / 3 );
  29.     vector<struct base64_encodedblock> blocks;
  30.  
  31.     cout << "Passes for '" << testo.c_str() << "': " << passes << endl;
  32.  
  33.     for (int i = 0; i < passes; i++)
  34.     {
  35.         string buff = testo.substr(i*3, 3);
  36.         base64_block block;
  37.         base64_encodedblock eblock;
  38.  
  39.         memset(&block, 0, sizeof(base64_block));
  40.         memset(&eblock, 0, sizeof(base64_encodedblock));
  41.  
  42.         cout << i << " - buff: " << buff.c_str() << endl;
  43.        
  44.         block.c1 = buff[0];
  45.         if (buff.length() > 1)
  46.         {
  47.             block.c2 = buff[1];
  48.             if (buff.length() > 2)
  49.                 block.c3 = buff[2];
  50.             else
  51.                 block.c3 = 0;
  52.         }
  53.         else
  54.         {
  55.             block.c2 = 0;
  56.             block.c3 = 0;
  57.         }
  58.  
  59.         cout << " - - Ascii of '" << block.c1 << "' is " << (short) block.c1 << endl;
  60.         cout << " - - Ascii of '" << block.c2 << "' is " << (short) block.c2 << endl;
  61.         cout << " - - Ascii of '" << block.c3 << "' is " << (short) block.c3 << endl;
  62.        
  63.         bitset<8> bc1(block.c1);
  64.         bitset<8> bc2(block.c2);
  65.         bitset<8> bc3(block.c3);
  66.         // inverto i bit
  67.         bool temp;
  68.         for (int j = 0; j < 4; j++)
  69.         {
  70.             temp = bc1[j];
  71.             bc1[j] = bc1[7-j];
  72.             bc1[7-j] = temp;
  73.         }  
  74.         for (int j = 0; j < 4; j++)
  75.         {
  76.             temp = bc2[j];
  77.             bc2[j] = bc2[7-j];
  78.             bc2[7-j] = temp;
  79.         }
  80.         for (int j = 0; j < 4; j++)
  81.         {
  82.             temp = bc3[j];
  83.             bc3[j] = bc3[7-j];
  84.             bc3[7-j] = temp;
  85.         }
  86.  
  87.         // li divido per 6
  88.         for (int j = 0; j < 6; j++)
  89.         {
  90.             eblock.b1[j] = bc1[j];
  91.         }
  92.         eblock.b2[0] = bc1[6];
  93.         eblock.b2[1] = bc1[7];
  94.         for (int j = 0; j < 4; j++)
  95.         {
  96.             eblock.b2[j+2] = bc2[j];
  97.         }
  98.         for (int j = 0; j < 4; j++)
  99.         {
  100.             eblock.b3[j] = bc2[j+4];
  101.         }
  102.         eblock.b3[4] = bc3[0];
  103.         eblock.b3[5] = bc3[1];
  104.         for (int j = 0; j < 6; j++)
  105.         {
  106.             eblock.b4[j] = bc3[j+2];
  107.         }
  108.  
  109.  
  110.         cout << "ASCII in binario: " << endl;
  111.         for (int j = 0; j < 8; j++)
  112.         {
  113.             cout << bc1[j];
  114.         }
  115.         cout << endl;
  116.         for (int j = 0; j < 8; j++)
  117.         {
  118.             cout << bc2[j];
  119.         }
  120.         cout << endl;
  121.         for (int j = 0; j < 8; j++)
  122.         {
  123.             cout << bc3[j];
  124.         }
  125.         cout << endl << endl;
  126.  
  127.         for (int j = 0; j < 6; j++)
  128.         {
  129.             cout << eblock.b1[j];
  130.         }
  131.         cout << endl;
  132.         for (int j = 0; j < 6; j++)
  133.         {
  134.             cout << eblock.b2[j];
  135.         }
  136.         cout << endl;
  137.         for (int j = 0; j < 6; j++)
  138.         {
  139.             cout << eblock.b3[j];
  140.         }
  141.         cout << endl;
  142.         for (int j = 0; j < 6; j++)
  143.         {
  144.             cout << eblock.b4[j];
  145.         }
  146.         cout << endl;
  147.  
  148.         blocks.push_back(eblock);
  149.     }
  150.     cout << endl;
  151.  
  152.     string encoded ("");
  153.     for (vector<struct base64_encodedblock>::iterator i = blocks.begin(); i != blocks.end(); i++)
  154.     {
  155.         char buf[5];
  156.         buf[0] = Base64Table[(*i).b1.to_ulong()];
  157.         buf[1] = Base64Table[(*i).b2.to_ulong()];
  158.         buf[2] = Base64Table[(*i).b3.to_ulong()];
  159.         buf[3] = Base64Table[(*i).b4.to_ulong()];
  160.         buf[4] = '\0';
  161.  
  162.         cout << (*i).b1.to_ulong() << endl;
  163.         cout << (*i).b2.to_ulong() << endl;
  164.         cout << (*i).b3.to_ulong() << endl;
  165.         cout << (*i).b4.to_ulong() << endl;
  166.  
  167.         encoded += buf;
  168.     }
  169.  
  170.     cout << encoded;
  171.  
  172.     getchar();
  173.  
  174.     return 0;
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement