thenuke321

Untitled

Mar 14th, 2015
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <cstdlib>
  5. #include <cassert>
  6. #include <iterator>
  7. #include <sstream>
  8.  
  9. using namespace std;
  10.  
  11. unsigned char s[256] =  
  12. {
  13.    0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F, 0xC5, 0x30, 0x01, 0x67, 0x2B, 0xFE, 0xD7, 0xAB, 0x76,
  14.    0xCA, 0x82, 0xC9, 0x7D, 0xFA, 0x59, 0x47, 0xF0, 0xAD, 0xD4, 0xA2, 0xAF, 0x9C, 0xA4, 0x72, 0xC0,
  15.    0xB7, 0xFD, 0x93, 0x26, 0x36, 0x3F, 0xF7, 0xCC, 0x34, 0xA5, 0xE5, 0xF1, 0x71, 0xD8, 0x31, 0x15,
  16.    0x04, 0xC7, 0x23, 0xC3, 0x18, 0x96, 0x05, 0x9A, 0x07, 0x12, 0x80, 0xE2, 0xEB, 0x27, 0xB2, 0x75,
  17.    0x09, 0x83, 0x2C, 0x1A, 0x1B, 0x6E, 0x5A, 0xA0, 0x52, 0x3B, 0xD6, 0xB3, 0x29, 0xE3, 0x2F, 0x84,
  18.    0x53, 0xD1, 0x00, 0xED, 0x20, 0xFC, 0xB1, 0x5B, 0x6A, 0xCB, 0xBE, 0x39, 0x4A, 0x4C, 0x58, 0xCF,
  19.    0xD0, 0xEF, 0xAA, 0xFB, 0x43, 0x4D, 0x33, 0x85, 0x45, 0xF9, 0x02, 0x7F, 0x50, 0x3C, 0x9F, 0xA8,
  20.    0x51, 0xA3, 0x40, 0x8F, 0x92, 0x9D, 0x38, 0xF5, 0xBC, 0xB6, 0xDA, 0x21, 0x10, 0xFF, 0xF3, 0xD2,
  21.    0xCD, 0x0C, 0x13, 0xEC, 0x5F, 0x97, 0x44, 0x17, 0xC4, 0xA7, 0x7E, 0x3D, 0x64, 0x5D, 0x19, 0x73,
  22.    0x60, 0x81, 0x4F, 0xDC, 0x22, 0x2A, 0x90, 0x88, 0x46, 0xEE, 0xB8, 0x14, 0xDE, 0x5E, 0x0B, 0xDB,
  23.    0xE0, 0x32, 0x3A, 0x0A, 0x49, 0x06, 0x24, 0x5C, 0xC2, 0xD3, 0xAC, 0x62, 0x91, 0x95, 0xE4, 0x79,
  24.    0xE7, 0xC8, 0x37, 0x6D, 0x8D, 0xD5, 0x4E, 0xA9, 0x6C, 0x56, 0xF4, 0xEA, 0x65, 0x7A, 0xAE, 0x08,
  25.    0xBA, 0x78, 0x25, 0x2E, 0x1C, 0xA6, 0xB4, 0xC6, 0xE8, 0xDD, 0x74, 0x1F, 0x4B, 0xBD, 0x8B, 0x8A,
  26.    0x70, 0x3E, 0xB5, 0x66, 0x48, 0x03, 0xF6, 0x0E, 0x61, 0x35, 0x57, 0xB9, 0x86, 0xC1, 0x1D, 0x9E,
  27.    0xE1, 0xF8, 0x98, 0x11, 0x69, 0xD9, 0x8E, 0x94, 0x9B, 0x1E, 0x87, 0xE9, 0xCE, 0x55, 0x28, 0xDF,
  28.    0x8C, 0xA1, 0x89, 0x0D, 0xBF, 0xE6, 0x42, 0x68, 0x41, 0x99, 0x2D, 0x0F, 0xB0, 0x54, 0xBB, 0x16
  29. };
  30.  
  31. unsigned char inv_s[256] =  
  32. {
  33.    0x52, 0x09, 0x6A, 0xD5, 0x30, 0x36, 0xA5, 0x38, 0xBF, 0x40, 0xA3, 0x9E, 0x81, 0xF3, 0xD7, 0xFB,
  34.    0x7C, 0xE3, 0x39, 0x82, 0x9B, 0x2F, 0xFF, 0x87, 0x34, 0x8E, 0x43, 0x44, 0xC4, 0xDE, 0xE9, 0xCB,
  35.    0x54, 0x7B, 0x94, 0x32, 0xA6, 0xC2, 0x23, 0x3D, 0xEE, 0x4C, 0x95, 0x0B, 0x42, 0xFA, 0xC3, 0x4E,
  36.    0x08, 0x2E, 0xA1, 0x66, 0x28, 0xD9, 0x24, 0xB2, 0x76, 0x5B, 0xA2, 0x49, 0x6D, 0x8B, 0xD1, 0x25,
  37.    0x72, 0xF8, 0xF6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xD4, 0xA4, 0x5C, 0xCC, 0x5D, 0x65, 0xB6, 0x92,
  38.    0x6C, 0x70, 0x48, 0x50, 0xFD, 0xED, 0xB9, 0xDA, 0x5E, 0x15, 0x46, 0x57, 0xA7, 0x8D, 0x9D, 0x84,
  39.    0x90, 0xD8, 0xAB, 0x00, 0x8C, 0xBC, 0xD3, 0x0A, 0xF7, 0xE4, 0x58, 0x05, 0xB8, 0xB3, 0x45, 0x06,
  40.    0xD0, 0x2C, 0x1E, 0x8F, 0xCA, 0x3F, 0x0F, 0x02, 0xC1, 0xAF, 0xBD, 0x03, 0x01, 0x13, 0x8A, 0x6B,
  41.    0x3A, 0x91, 0x11, 0x41, 0x4F, 0x67, 0xDC, 0xEA, 0x97, 0xF2, 0xCF, 0xCE, 0xF0, 0xB4, 0xE6, 0x73,
  42.    0x96, 0xAC, 0x74, 0x22, 0xE7, 0xAD, 0x35, 0x85, 0xE2, 0xF9, 0x37, 0xE8, 0x1C, 0x75, 0xDF, 0x6E,
  43.    0x47, 0xF1, 0x1A, 0x71, 0x1D, 0x29, 0xC5, 0x89, 0x6F, 0xB7, 0x62, 0x0E, 0xAA, 0x18, 0xBE, 0x1B,
  44.    0xFC, 0x56, 0x3E, 0x4B, 0xC6, 0xD2, 0x79, 0x20, 0x9A, 0xDB, 0xC0, 0xFE, 0x78, 0xCD, 0x5A, 0xF4,
  45.    0x1F, 0xDD, 0xA8, 0x33, 0x88, 0x07, 0xC7, 0x31, 0xB1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xEC, 0x5F,
  46.    0x60, 0x51, 0x7F, 0xA9, 0x19, 0xB5, 0x4A, 0x0D, 0x2D, 0xE5, 0x7A, 0x9F, 0x93, 0xC9, 0x9C, 0xEF,
  47.    0xA0, 0xE0, 0x3B, 0x4D, 0xAE, 0x2A, 0xF5, 0xB0, 0xC8, 0xEB, 0xBB, 0x3C, 0x83, 0x53, 0x99, 0x61,
  48.    0x17, 0x2B, 0x04, 0x7E, 0xBA, 0x77, 0xD6, 0x26, 0xE1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0C, 0x7D
  49. };
  50.  
  51. void printThing(unsigned char** text)
  52. {
  53.     /*
  54.     for(int i = 0; i < 4; ++i)
  55.     {
  56.         for(int j = 0; j < 4; ++j)
  57.         {  
  58.             printf("%x  ", text[i][j]);
  59.         }
  60.         std::cout << endl;
  61.     }
  62.     */
  63. }
  64.  
  65. unsigned char hexMult(unsigned char hexVal, int quantity)
  66. {
  67.     unsigned char result;
  68.     unsigned char temp;
  69.  
  70.     if(quantity == 2)
  71.     {
  72.         temp = hexVal>>7;
  73.         result = (hexVal<<1) + temp;
  74.         if(hexVal > 127)
  75.         {
  76.             result ^= 0x1A;
  77.         }
  78.         return result;
  79.     }
  80.     else
  81.     {
  82.         temp = hexVal>>7;
  83.         result = (hexVal<<1) + temp;
  84.         if(hexVal > 127)
  85.         {
  86.             result ^= 0x1A;
  87.         }
  88.         result ^= hexVal;
  89.         return result;
  90.     }
  91. }
  92.  
  93. unsigned char* stringToUSChar(string text, int size)
  94. {
  95.     string tempText = "";
  96.     for(int i = 0; i < size - 1; i+=2)
  97.     {
  98.          
  99.         tempText += text[i];
  100.         tempText += text[i+1];
  101.         tempText += " ";
  102.     }
  103.      
  104.     unsigned char* result  = new unsigned char[size/2];
  105.     int i = 0;
  106.     std::istringstream text_2(tempText);
  107.  
  108.     unsigned int c;
  109.  
  110.     while (text_2 >> hex >> c)
  111.     {
  112.         result[i] = (unsigned char )c;
  113.         ++i;
  114.     }
  115.  
  116.     return result;
  117. }
  118.  
  119. unsigned char inv_subByte(unsigned char x)
  120. {
  121.     return inv_s[x];
  122. }
  123.  
  124. unsigned char subByte(unsigned char x)
  125. {
  126.     return s[(int)x];
  127. }
  128.  
  129. unsigned char rcon(unsigned char input)
  130. {
  131.     unsigned char c = 1;
  132.     if(input == 0)
  133.         return 0;
  134.     while(input != 1)
  135.     {
  136.         unsigned char temp = c & 0x80;
  137.         c <<= 1;
  138.         if(temp == 0x80)
  139.         {
  140.             c ^= 0x1b;
  141.         }
  142.  
  143.         input--;
  144.     }
  145.  
  146.     return c;
  147. }
  148.  
  149. void schedule_core(unsigned char *input, unsigned char i)  
  150. {
  151.         char a;
  152.         int temp = input[0];
  153.         for(int i = 0; i < 3; ++i)
  154.         {
  155.             input[i] = input[i + 1];
  156.         }
  157.  
  158.         input[3] = temp;
  159.         for(a = 0; a < 4; a++)  
  160.                 input[a] = subByte(input[a]);
  161.         input[0] ^= rcon(i);
  162. }
  163.  
  164. void expandKey(unsigned char key[], unsigned char* result)
  165. {
  166.     unsigned char t[4];
  167.     int c = 16;
  168.     unsigned char i = 1;
  169.     unsigned char temp = 0;
  170.     int a;
  171.  
  172.     for(int j = 0; j < 16; ++j)
  173.     {
  174.         result[j] = key[j];
  175.     }
  176.    
  177.     while(c < 176)  
  178.     {
  179.         for(a = 0; a < 4; a++)
  180.         {
  181.             t[a] = result[a + c - 4];
  182.         }
  183.         if(c % 16 == 0)  
  184.         {
  185.             schedule_core(t,i);
  186.             i++;
  187.         }
  188.         for(a = 0; a < 4; a++)  
  189.         {
  190.             result[c] = result[c - 16] ^ t[a];
  191.             c++;
  192.         }
  193.        
  194.     }
  195. }
  196.  
  197. void addRoundKey(unsigned char** state,unsigned char* key)
  198. {
  199.     cout << "Key: ";
  200.     for(int j = 0; j < 16; ++j)
  201.         printf("%x,", key[j]);
  202.     cout << endl;
  203.  
  204.     for(int i = 0; i < 4; ++i)
  205.     {
  206.         for(int j = 0; j < 4; ++j)
  207.         {
  208.             state[i][j] = state[i][j] ^ key[i*4 + j];
  209.         }
  210.     }
  211. }
  212.  
  213. void shiftRow(unsigned char* row, int bytes)
  214. {
  215.    
  216.     if(bytes == 1)
  217.     {
  218.         int temp = row[0];
  219.         for(int i = 0; i < 3; ++i)
  220.         {
  221.             row[i] = row[i + 1];
  222.         }
  223.  
  224.         row[3] = temp;
  225.     }
  226.     else if(bytes == 2)
  227.     {
  228.         int temp = row[0];
  229.         int temp2 = row[1];
  230.         for(int i = 0; i < 2; ++i)
  231.         {
  232.             row[i] = row[i + 2];
  233.         }
  234.         row[2] = temp;
  235.         row[3] = temp2;
  236.     }
  237.     else if(bytes == 3)
  238.     {
  239.         int temp = row[3];
  240.         for(int i = 3; i > 0; --i)
  241.         {
  242.             row[i] = row[i - 1];
  243.         }
  244.        
  245.         row[0] = temp;
  246.     }
  247. }
  248.  
  249. void inv_shiftRow(unsigned char* row, int bytes)
  250. {
  251.  
  252.     if(bytes == 1)
  253.     {
  254.         int temp = row[0];
  255.         for(int i = 0; i < 3; ++i)
  256.         {
  257.             row[i] = row[i + 1];
  258.         }
  259.  
  260.         row[3] = temp;
  261.     }
  262.     else if(bytes == 2)
  263.     {
  264.         int temp = row[0];
  265.         int temp2 = row[1];
  266.         for(int i = 0; i < 2; ++i)
  267.         {
  268.             row[i] = row[i + 2];
  269.         }
  270.         row[2] = temp;
  271.         row[3] = temp2;
  272.     }
  273.     else if(bytes == 3)
  274.     {
  275.         int temp = row[3];
  276.         for(int i = 3; i > 0; --i)
  277.         {
  278.             row[i] = row[i - 1];
  279.         }
  280.        
  281.         row[0] = temp;
  282.     }
  283. }
  284.  
  285. void mixColumn(unsigned char* column)
  286. {
  287.     unsigned char a[4] , b[4], h;
  288.  
  289.     for(int i = 0; i < 4; ++i)
  290.     {
  291.         a[i] = column[i];
  292.     }
  293.    
  294.     column[0] = hexMult(a[0], 2) ^ hexMult(a[1], 3) ^ a[2] ^ a[3];
  295.     column[1] = a[0] ^ hexMult(a[1], 2) ^ hexMult(a[2], 3) ^ a[3];
  296.     column[2] = a[0] ^ a[1] ^ hexMult(a[2], 2) ^ hexMult(a[3], 3);
  297.     column[3] = hexMult(a[0], 3) ^ a[1] ^ a[2] ^ hexMult(a[3], 2);
  298.  
  299. }
  300.  
  301. unsigned char gmul(unsigned char a, unsigned char b)
  302. {
  303.     unsigned char p = 0;
  304.     unsigned char hi_bit_set;
  305.     for(int i = 0; i < 8; i++)
  306.     {
  307.         if((b & 1) == 1)
  308.             p ^= a;
  309.         hi_bit_set = (a & 0x80);
  310.         a <<= 1;
  311.         if(hi_bit_set == 0x80)
  312.             a ^= 0x1b;     
  313.         b >>= 1;
  314.     }
  315.     return p;
  316. }
  317.  
  318. void inv_mixColumn(unsigned char *column)
  319. {
  320.     unsigned char a[4];
  321.     for(int i=0;i<4;i++)
  322.     {
  323.         a[i] = column[i];
  324.     }
  325.     column[0] = gmul(a[0],14) ^ gmul(a[3],9) ^ gmul(a[2],13) ^ gmul(a[1],11);
  326.     column[1] = gmul(a[1],14) ^ gmul(a[0],9) ^ gmul(a[3],13) ^ gmul(a[2],11);
  327.     column[2] = gmul(a[2],14) ^ gmul(a[1],9) ^ gmul(a[0],13) ^ gmul(a[3],11);
  328.     column[3] = gmul(a[3],14) ^ gmul(a[2],9) ^ gmul(a[1],13) ^ gmul(a[0],11);
  329. }
  330.  
  331. void encrypt(unsigned char** text, unsigned char* key, int size)
  332. {
  333.     cout <<"----------------------------" << endl;
  334.     unsigned char result[4];
  335.     unsigned char* expandedKey = new unsigned char[176];
  336.     expandKey(key, expandedKey);
  337.     addRoundKey(text, expandedKey);
  338.     for(int f = 1; f < 11; ++f)
  339.     {
  340.         cout<<"ROUND " << f << endl;
  341.  
  342.         printThing(text);
  343.                        
  344.         for(int j = 0; j < size; ++j)
  345.         {
  346.             for(int k = 0; k < size; ++k)
  347.             {
  348.                 text[j][k] = subByte(text[j][k]);
  349.             }
  350.         }
  351.         cout<<"After SubByte" << endl;
  352.         printThing(text);
  353.        
  354.         for(int j = 1; j < 4; j++)
  355.         {
  356.             for(int k = 0; k < 4; k++)
  357.             {
  358.                 result[k] = text[k][j];
  359.             }
  360.             shiftRow(result, j);
  361.  
  362.             text[0][j] = result[0];
  363.             text[1][j] = result[1];
  364.             text[2][j] = result[2];
  365.             text[3][j] = result[3];
  366.         }
  367.  
  368.         cout<<"After ShiftRow" << endl;
  369.         printThing(text);
  370.         if(f != 10)
  371.         {
  372.             for(int j = 0; j < 4; j++)
  373.             {
  374.                 mixColumn(text[j]);
  375.             }
  376.            
  377.             cout<<"After mixColumn" << endl;
  378.             printThing(text);
  379.         }
  380.  
  381.         cout<<"After AddRoundKey" << endl;
  382.         expandedKey += 16;
  383.  
  384.         addRoundKey(text, expandedKey);
  385.         printThing(text);
  386.  
  387.         cout <<"----------------------------" << endl;
  388.         cout << endl;
  389.     }
  390. }
  391.  
  392. void decrypt(unsigned char** text, unsigned char* key, int size)
  393. {
  394.     cout <<"----------------------------" << endl;
  395.     unsigned char result[4];
  396.     unsigned char* expandedKey = new unsigned char[176];
  397.     expandKey(key, expandedKey);
  398.     expandedKey += 160;
  399.     addRoundKey(text, expandedKey);
  400.     for(int f = 1; f < 11; ++f)
  401.     {
  402.         cout<<"ROUND " << f << endl;
  403.         printThing(text);
  404.  
  405.                        
  406.         for(int j = 1; j < 4; j++)
  407.         {
  408.             for(int k = 0; k < 4; k++)
  409.             {
  410.                 result[k] = text[k][j];
  411.             }
  412.             inv_shiftRow(result, 4-j);
  413.  
  414.             text[0][j] = result[0];
  415.             text[1][j] = result[1];
  416.             text[2][j] = result[2];
  417.             text[3][j] = result[3];
  418.         }
  419.  
  420.         cout<<"After ShiftRow" << endl;
  421.         printThing(text);
  422.  
  423.         for(int j = 0; j < size; ++j)
  424.         {
  425.             for(int k = 0; k < size; ++k)
  426.             {
  427.                 text[j][k] = inv_subByte(text[j][k]);
  428.             }
  429.         }
  430.         cout<<"After SubByte" << endl;
  431.         printThing(text);
  432.         cout<<"After AddRoundKey" << endl;
  433.                 expandedKey -= 16;
  434.         addRoundKey(text, expandedKey);
  435.  
  436.         printThing(text);
  437.  
  438.         if(f != 10)
  439.         {
  440.             for(int j = 0; j < 4; j++)
  441.             {
  442.                 inv_mixColumn(text[j]);
  443.             }
  444.            
  445.             cout<<"After mixColumn" << endl;
  446.             printThing(text);
  447.         }
  448.  
  449.         cout <<"----------------------------" << endl;
  450.     }
  451.    
  452. }
  453.  
  454. int main()
  455. {
  456.     char type = 'D';
  457.     string text = "" ;
  458.     int size;
  459.     unsigned char **charText;
  460.     unsigned char *charKey;
  461.     string key = "" ;
  462.     std::cout << "Enter E to encrypt or D to decrypt: ";
  463.     cin >> type;
  464.     getline(cin, key);
  465.     std::cout << "Enter Key" << endl;
  466.     getline(cin, key);
  467.      
  468.     if(type == 'E' || type == 'e')
  469.     {
  470.         std::cout << "Enter plaintext" << endl;
  471.         getline(cin, text);
  472.  
  473.         size = text.length();
  474.         charText = new unsigned char* [size/8];
  475.         for(int i = 0; i < size/8; ++i)
  476.         {
  477.             charText[i] = stringToUSChar(text.substr(i*8, size/4), size/4);
  478.         }
  479.        
  480.         charKey = stringToUSChar(key, size);
  481.  
  482.         encrypt(charText, charKey,4);
  483.         std::cout << "The ciphertext is " << endl;
  484.         printThing(charText);
  485.  
  486.         for(int i = 0; i < 4; ++i)
  487.        
  488.         delete[] charText[i];
  489.         delete[] charText;
  490.         delete[] charKey;
  491.     }
  492.     else if(type == 'D' || type == 'd')
  493.     {
  494.         std::cout << "Enter ciphertext" << endl;
  495.         getline(cin, text);
  496.         cout <<"----------------------------" << endl;
  497.         size = text.length();
  498.         charText = new unsigned char* [size/8];
  499.         for(int i = 0; i < size/8; ++i)
  500.         {
  501.             charText[i] = stringToUSChar(text.substr(i*8, size/4), size/4);
  502.         }
  503.        
  504.         charKey = stringToUSChar(key, size);
  505.  
  506.         decrypt(charText, charKey,4);
  507.         std::cout << "The plaintext is " << endl;
  508.         printThing(charText);
  509.  
  510.         for(int i = 0; i < 4; ++i)
  511.    
  512.         delete[] charText[i];
  513.         delete[] charText;
  514.         delete[] charKey;
  515.        
  516.     }    
  517.  
  518.     cin >> text;
  519.      
  520. }
Advertisement
Add Comment
Please, Sign In to add comment