SHARE
TWEET

Untitled

a guest May 23rd, 2019 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <vector>
  4. #include <cstdlib>
  5. #include <ctime>
  6.  
  7. class AES
  8. {
  9.     uint8_t S_BOX[256] =
  10.     {
  11.             0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F, 0xC5, 0x30, 0x01, 0x67, 0x2B, 0xFE, 0xD7, 0xAB, 0x76,
  12.             0xCA, 0x82, 0xC9, 0x7D, 0xFA, 0x59, 0x47, 0xF0, 0xAD, 0xD4, 0xA2, 0xAF, 0x9C, 0xA4, 0x72, 0xC0,
  13.             0xB7, 0xFD, 0x93, 0x26, 0x36, 0x3F, 0xF7, 0xCC, 0x34, 0xA5, 0xE5, 0xF1, 0x71, 0xD8, 0x31, 0x15,
  14.             0x04, 0xC7, 0x23, 0xC3, 0x18, 0x96, 0x05, 0x9A, 0x07, 0x12, 0x80, 0xE2, 0xEB, 0x27, 0xB2, 0x75,
  15.             0x09, 0x83, 0x2C, 0x1A, 0x1B, 0x6E, 0x5A, 0xA0, 0x52, 0x3B, 0xD6, 0xB3, 0x29, 0xE3, 0x2F, 0x84,
  16.             0x53, 0xD1, 0x00, 0xED, 0x20, 0xFC, 0xB1, 0x5B, 0x6A, 0xCB, 0xBE, 0x39, 0x4A, 0x4C, 0x58, 0xCF,
  17.             0xD0, 0xEF, 0xAA, 0xFB, 0x43, 0x4D, 0x33, 0x85, 0x45, 0xF9, 0x02, 0x7F, 0x50, 0x3C, 0x9F, 0xA8,
  18.             0x51, 0xA3, 0x40, 0x8F, 0x92, 0x9D, 0x38, 0xF5, 0xBC, 0xB6, 0xDA, 0x21, 0x10, 0xFF, 0xF3, 0xD2,
  19.             0xCD, 0x0C, 0x13, 0xEC, 0x5F, 0x97, 0x44, 0x17, 0xC4, 0xA7, 0x7E, 0x3D, 0x64, 0x5D, 0x19, 0x73,
  20.             0x60, 0x81, 0x4F, 0xDC, 0x22, 0x2A, 0x90, 0x88, 0x46, 0xEE, 0xB8, 0x14, 0xDE, 0x5E, 0x0B, 0xDB,
  21.             0xE0, 0x32, 0x3A, 0x0A, 0x49, 0x06, 0x24, 0x5C, 0xC2, 0xD3, 0xAC, 0x62, 0x91, 0x95, 0xE4, 0x79,
  22.             0xE7, 0xC8, 0x37, 0x6D, 0x8D, 0xD5, 0x4E, 0xA9, 0x6C, 0x56, 0xF4, 0xEA, 0x65, 0x7A, 0xAE, 0x08,
  23.             0xBA, 0x78, 0x25, 0x2E, 0x1C, 0xA6, 0xB4, 0xC6, 0xE8, 0xDD, 0x74, 0x1F, 0x4B, 0xBD, 0x8B, 0x8A,
  24.             0x70, 0x3E, 0xB5, 0x66, 0x48, 0x03, 0xF6, 0x0E, 0x61, 0x35, 0x57, 0xB9, 0x86, 0xC1, 0x1D, 0x9E,
  25.             0xE1, 0xF8, 0x98, 0x11, 0x69, 0xD9, 0x8E, 0x94, 0x9B, 0x1E, 0x87, 0xE9, 0xCE, 0x55, 0x28, 0xDF,
  26.             0x8C, 0xA1, 0x89, 0x0D, 0xBF, 0xE6, 0x42, 0x68, 0x41, 0x99, 0x2D, 0x0F, 0xB0, 0x54, 0xBB, 0x16
  27.     };
  28.     uint8_t key[16];
  29.     uint8_t extkey[176];
  30.     void rotate_vect(uint8_t vect[]);
  31.     void sub_sbox(uint8_t vect[]);
  32.     void rcon(uint8_t vect[], int number);
  33.     void xor_append(uint8_t* block, uint8_t vect[]);
  34.     void sub_sbox2(uint8_t vect[4][4]);
  35.  
  36. public:
  37.     void generate_key128(uint8_t key[], uint8_t extkey[]);
  38.     void ciphertext(const uint8_t text[4][4], const uint8_t key[]);
  39.     void shift_rows(uint8_t matrix[4][4]);
  40.     vector< vector< vector<char> > >  stringtomatrix(string inputdata);
  41.     AES();
  42.  
  43. };
  44.  
  45. AES::AES()
  46. {
  47.     for (int i = 0; i < 16; i++)
  48.     {
  49.         key[i] = (std::rand()) + 256;
  50.         //std::cout << (int)key[i]<<std::endl;
  51.     }
  52.  
  53.  
  54.     for (int i = 0; i < 176; i++)
  55.     {
  56.         if (i < 16)
  57.             std::cout << (int)extkey[i] << " --- --- " << (int)key[i] << std::endl;
  58.         else
  59.             std::cout << (int)extkey[i] << std::endl;
  60.     }
  61.  
  62.  
  63.     std::cout << "!@#@$&*^%$##@#%%&^%$%$#@$#$^*^&^%$!#@$%$^&%^%%@$!$@%#" << std::endl;
  64.  
  65.     stringtomatrix(string inputdata) {
  66.     vector< vector<char> > matrix(2, vector <char>(2)); //declaration 2x2 matrix (4bytes x 4bytes)
  67.     vector< vector< vector<char> > >tab4;
  68.  
  69.     int counter = 0;
  70.     int k = out.length(); //the length of the input data
  71.     int division = k/4; //dividing into 4, because there are 4 char in each matrix
  72.     int rest=k%4; //the rest from dividing into 4
  73.  
  74.     for(int t=0;t<division;t++) {
  75.         for (int i = 0; i < 2; i++) {
  76.             for (int j = 0; j < 2; j++) {
  77.                 tab[i][j] = inputdata[counter]; //giving input data into the matrix
  78.                 counter++;
  79.             }
  80.         }
  81.         tab4.push_back(tab);
  82.     }
  83.     int o=k-rest+1;
  84.     for (int i = 0; i < 2; i++) {
  85.             for (int j = 0; j < 2; j++) {
  86.                 tab[i][j] = inputdata[o];
  87.                 o++;
  88.             }
  89.         }
  90.     tab4.push_back(tab);
  91.  
  92. return vector< vector< vector<char> > >tab4;
  93. }
  94.  
  95.  
  96.     generate_key128(key, extkey);
  97.  
  98.     for (int i = 0; i < 176; i++)
  99.     {
  100.         if (i < 16)
  101.             std::cout << (int)extkey[i] << " --- --- " << (int)key[i] << std::endl;
  102.         else
  103.             std::cout << (int)extkey[i] << std::endl;
  104.     }
  105. }
  106.  
  107.  
  108. void AES::rotate_vect(uint8_t vect[])
  109. {
  110.     uint8_t temp = vect[0];
  111.     memmove(vect, vect + 1, 3);
  112.     memcpy(vect + 3, &temp, 1);
  113. }
  114.  
  115. void AES::sub_sbox(uint8_t vect[])
  116. {
  117.     for (int i = 0; i < 4; i++)
  118.     {
  119.         vect[i] = S_BOX[vect[i]];
  120.     }
  121. }
  122.  
  123. void AES::rcon(uint8_t vect[], int number)
  124. {
  125.     vect[0] ^= (int)pow(2, number);
  126. }
  127.  
  128. void AES::xor_append(uint8_t* block, uint8_t vect[])
  129. {
  130.     uint8_t temp2[4];
  131.     memcpy(&temp2, block, 4);
  132.     for (int i = 0; i < 4; i++)
  133.     {
  134.         vect[i] ^= temp2[i];
  135.     }
  136.     memcpy(block + 16, vect, 4);
  137. }
  138.  
  139. void AES::generate_key128(uint8_t key[], uint8_t extkey[])
  140. {
  141.     memcpy(extkey, key, 16);
  142.     uint8_t state = 16;
  143.     uint8_t temp[4];
  144.     for (int i = 1;state<176; i++)
  145.     {
  146.         memcpy(temp, extkey + (state - 4), 4);
  147.         rotate_vect(temp);
  148.         sub_sbox(temp);
  149.         rcon(temp, i-1);
  150.         xor_append(extkey + state - 16, temp);
  151.         state += 4;
  152.         for (int s = 0; s < 3; s++)
  153.         {
  154.             memcpy(temp, extkey + (state - 4), 4);
  155.             xor_append(extkey + state - 16, temp);
  156.             state += 4;
  157.         }
  158.     }
  159. }
  160.  
  161. void AES::sub_sbox2(uint8_t vect[4][4])
  162. {
  163.     for (int i = 0; i < 4; i++)
  164.     {
  165.         sub_sbox(vect[i]);
  166.     }
  167. }
  168.  
  169. void AES::shift_rows(uint8_t matrix[4][4])
  170. {
  171.  
  172. }
  173. void AES::ciphertext(const uint8_t text[4][4], const uint8_t key[])
  174. {
  175.     uint8_t temp[4][4];
  176.     uint8_t ciphered[4][4];
  177.     memcpy(ciphered, text, 16);
  178.     memcpy(temp, key, 16);
  179.     // Initial round
  180.     for (int i = 0; i < 4; i++)
  181.     {
  182.         for (int j = 0; j < 4; j++)
  183.         {
  184.             ciphered[i][j] ^= temp[i][j];
  185.         }
  186.     }
  187.     // 9 repeats
  188.     for (int i = 0; i < 9; i++)
  189.     {
  190.         sub_sbox2(ciphered);
  191.         shift_rows(ciphered);
  192.     }
  193.  
  194. }
  195.  
  196.  
  197. int main()
  198. {
  199.     srand(time(NULL));
  200.     AES test;
  201. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top