SHARE
TWEET

Untitled

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