thenuke321

Untitled

Mar 1st, 2015
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.62 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <string>
  5. #include <iostream>
  6. #include <vector>
  7. using namespace std;
  8.  
  9. #define AES_RPOL          0x011b // reduction polynomial (x^8 + x^4 + x^3 + x + 1)
  10. #define AES_GEN    0x03   // gf(2^8) generator  (x + 1)
  11. #define AES_SBOX_CC   0x63   // S-Box C constant
  12.  
  13. #define KEY_128 (128/8)
  14. #define KEY_192 (192/8)
  15. #define KEY_256 (256/8)
  16.  
  17. #define aes_mul(a, b) ((a)&&(b)?g_aes_ilogt[(g_aes_logt[(a)]+g_aes_logt[(b)])%0xff]:0)
  18. #define aes_inv(a)      ((a)?g_aes_ilogt[0xff-g_aes_logt[(a)]]:0)
  19.  
  20. unsigned char g_aes_logt[256], g_aes_ilogt[256];
  21. unsigned char g_aes_sbox[256], g_aes_isbox[256];
  22.  
  23. typedef struct {
  24.     unsigned char state[4][4];
  25.     int kcol;
  26.     size_t rounds;
  27.     unsigned long keysched[0];
  28. } aes_ctx_t;
  29.  
  30. void aes_init();
  31. aes_ctx_t *aes_alloc_ctx(unsigned char *key, size_t keyLen);
  32. inline unsigned long aes_subword(unsigned long w);
  33. inline unsigned long aes_rotword(unsigned long w);
  34. void aes_keyexpansion(aes_ctx_t *ctx);
  35.  
  36. inline unsigned char aes_mul_manual(unsigned char a, unsigned char b); // use aes_mul instead
  37.  
  38. void aes_subbytes(aes_ctx_t *ctx);
  39. void aes_shiftrows(aes_ctx_t *ctx);
  40. void aes_mixcolumns(aes_ctx_t *ctx);
  41. void aes_addroundkey(aes_ctx_t *ctx, int round);
  42. void aes_encrypt(aes_ctx_t *ctx, unsigned char input[16], unsigned char output[16]);
  43.  
  44. void aes_invsubbytes(aes_ctx_t *ctx);
  45. void aes_invshiftrows(aes_ctx_t *ctx);
  46. void aes_invmixcolumns(aes_ctx_t *ctx);
  47. void aes_decrypt(aes_ctx_t *ctx, unsigned char input[16], unsigned char output[16]);
  48.  
  49. void aes_free_ctx(aes_ctx_t *ctx);
  50.  
  51. void init_aes()
  52. {
  53.     int i;
  54.     unsigned char gen;
  55.  
  56.     // build logarithm table and it's inverse
  57.     gen = 1;
  58.     for (i = 0; i < 0xff; i++) {
  59.         g_aes_logt[gen] = i;
  60.         g_aes_ilogt[i] = gen;
  61.         gen = aes_mul_manual(gen, AES_GEN);
  62.     }
  63.  
  64.     // build S-Box and it's inverse
  65.     for (i = 0; i <= 0xff; i++) {
  66.         char bi;
  67.         unsigned char inv = aes_inv(i);
  68.  
  69.         g_aes_sbox[i] = 0;
  70.         for (bi = 0; bi < 8; bi++) {
  71.             // based on transformation 5.1
  72.             // could also be done with a loop based on the matrix
  73.             g_aes_sbox[i] |= ((inv & (1 << bi) ? 1 : 0)
  74.                 ^ (inv & (1 << ((bi + 4) & 7)) ? 1 : 0)
  75.                 ^ (inv & (1 << ((bi + 5) & 7)) ? 1 : 0)
  76.                 ^ (inv & (1 << ((bi + 6) & 7)) ? 1 : 0)
  77.                 ^ (inv & (1 << ((bi + 7) & 7)) ? 1 : 0)
  78.                 ^ (AES_SBOX_CC & (1 << bi) ? 1 : 0)
  79.                 ) << bi;
  80.         }
  81.         g_aes_isbox[g_aes_sbox[i]] = i;
  82.     }
  83.     // warning: quickhack
  84.     g_aes_sbox[1] = 0x7c;
  85.     g_aes_isbox[0x7c] = 1;
  86.     g_aes_isbox[0x63] = 0;
  87. }
  88.  
  89. aes_ctx_t *aes_alloc_ctx(unsigned char *key, size_t keyLen)
  90. {
  91.     aes_ctx_t *ctx;
  92.     size_t rounds;
  93.     size_t ks_size;
  94.  
  95.     switch (keyLen) {
  96.     case 16: // 128-bit key
  97.         rounds = 10;
  98.         break;
  99.  
  100.     case 24: // 192-bit key
  101.         rounds = 12;
  102.         break;
  103.  
  104.     case 32: // 256-bit key
  105.         rounds = 14;
  106.         break;
  107.  
  108.     defaut:
  109.         return NULL;
  110.     }
  111.  
  112.     ks_size = 4 * (rounds + 1)*sizeof(unsigned long);
  113.     ctx = (aes_ctx_t*)malloc(sizeof(aes_ctx_t) + ks_size);
  114.     if (ctx) {
  115.         ctx->rounds = rounds;
  116.         ctx->kcol = keyLen / 4;
  117.         memcpy(ctx->keysched, key, keyLen);
  118.         ctx->keysched[43] = 0;
  119.         aes_keyexpansion(ctx);
  120.     }
  121.  
  122.     return ctx;
  123. }
  124.  
  125. inline unsigned long aes_subword(unsigned long w)
  126. {
  127.     return g_aes_sbox[w & 0x000000ff] |
  128.         (g_aes_sbox[(w & 0x0000ff00) >> 8] << 8) |
  129.         (g_aes_sbox[(w & 0x00ff0000) >> 16] << 16) |
  130.         (g_aes_sbox[(w & 0xff000000) >> 24] << 24);
  131. }
  132.  
  133. inline unsigned long aes_rotword(unsigned long w)
  134. {
  135.     // May seem a bit different from the spec
  136.     // It was changed because unsigned long is represented with little-endian convention on x86
  137.     // Should not depend on architecture, but this is only a POC
  138.     return ((w & 0x000000ff) << 24) |
  139.         ((w & 0x0000ff00) >> 8) |
  140.         ((w & 0x00ff0000) >> 8) |
  141.         ((w & 0xff000000) >> 8);
  142. }
  143.  
  144. void aes_keyexpansion(aes_ctx_t *ctx)
  145. {
  146.     unsigned long temp;
  147.     unsigned long rcon;
  148.     register int i;
  149.  
  150.     rcon = 0x00000001;
  151.     for (i = ctx->kcol; i < (4 * (ctx->rounds + 1)); i++) {
  152.         temp = ctx->keysched[i - 1];
  153.         if (!(i%ctx->kcol)) {
  154.             temp = aes_subword(aes_rotword(temp)) ^ rcon;
  155.             rcon = aes_mul(rcon, 2);
  156.         }
  157.         else if (ctx->kcol > 6 && i%ctx->kcol == 4)
  158.             temp = aes_subword(temp);
  159.         ctx->keysched[i] = ctx->keysched[i - ctx->kcol] ^ temp;
  160.     }
  161. }
  162.  
  163. inline unsigned char aes_mul_manual(unsigned char a, unsigned char b)
  164. {
  165.     register unsigned short ac;
  166.     register unsigned char ret;
  167.  
  168.     ac = a;
  169.     ret = 0;
  170.     while (b) {
  171.         if (b & 0x01)
  172.             ret ^= ac;
  173.         ac <<= 1;
  174.         b >>= 1;
  175.         if (ac & 0x0100)
  176.             ac ^= AES_RPOL;
  177.     }
  178.  
  179.     return ret;
  180. }
  181.  
  182. void aes_subbytes(aes_ctx_t *ctx)
  183. {
  184.     int i;
  185.  
  186.     for (i = 0; i < 16; i++) {
  187.         int x, y;
  188.  
  189.         x = i & 0x03;
  190.         y = i >> 2;
  191.         ctx->state[x][y] = g_aes_sbox[ctx->state[x][y]];
  192.     }
  193. }
  194.  
  195. void aes_shiftrows(aes_ctx_t *ctx)
  196. {
  197.     unsigned char nstate[4][4];
  198.     int i;
  199.  
  200.     for (i = 0; i < 16; i++) {
  201.         int x, y;
  202.  
  203.         x = i & 0x03;
  204.         y = i >> 2;
  205.         nstate[x][y] = ctx->state[x][(y + x) & 0x03];
  206.     }
  207.  
  208.     memcpy(ctx->state, nstate, sizeof(ctx->state));
  209. }
  210.  
  211. void aes_mixcolumns(aes_ctx_t *ctx)
  212. {
  213.     unsigned char nstate[4][4];
  214.     int i;
  215.  
  216.     for (i = 0; i < 4; i++) {
  217.         nstate[0][i] = aes_mul(0x02, ctx->state[0][i]) ^
  218.             aes_mul(0x03, ctx->state[1][i]) ^
  219.             ctx->state[2][i] ^
  220.             ctx->state[3][i];
  221.         nstate[1][i] = ctx->state[0][i] ^
  222.             aes_mul(0x02, ctx->state[1][i]) ^
  223.             aes_mul(0x03, ctx->state[2][i]) ^
  224.             ctx->state[3][i];
  225.         nstate[2][i] = ctx->state[0][i] ^
  226.             ctx->state[1][i] ^
  227.             aes_mul(0x02, ctx->state[2][i]) ^
  228.             aes_mul(0x03, ctx->state[3][i]);
  229.         nstate[3][i] = aes_mul(0x03, ctx->state[0][i]) ^
  230.             ctx->state[1][i] ^
  231.             ctx->state[2][i] ^
  232.             aes_mul(0x02, ctx->state[3][i]);
  233.     }
  234.  
  235.     memcpy(ctx->state, nstate, sizeof(ctx->state));
  236. }
  237.  
  238. void aes_addroundkey(aes_ctx_t *ctx, int round)
  239. {
  240.     int i;
  241.  
  242.     for (i = 0; i < 16; i++) {
  243.         int x, y;
  244.  
  245.         x = i & 0x03;
  246.         y = i >> 2;
  247.         ctx->state[x][y] = ctx->state[x][y] ^
  248.             ((ctx->keysched[round * 4 + y] & (0xff << (x * 8))) >> (x * 8));
  249.     }
  250. }
  251.  
  252. void aes_encrypt(aes_ctx_t *ctx, unsigned char input[16], unsigned char output[16])
  253. {
  254.     int i;
  255.  
  256.     // copy input to state
  257.     for (i = 0; i < 16; i++)
  258.         ctx->state[i & 0x03][i >> 2] = input[i];
  259.  
  260.     aes_addroundkey(ctx, 0);
  261.  
  262.     for (i = 1; i < ctx->rounds; i++) {
  263.         aes_subbytes(ctx);
  264.         aes_shiftrows(ctx);
  265.         aes_mixcolumns(ctx);
  266.         aes_addroundkey(ctx, i);
  267.     }
  268.  
  269.     aes_subbytes(ctx);
  270.     aes_shiftrows(ctx);
  271.     aes_addroundkey(ctx, ctx->rounds);
  272.  
  273.     // copy state to output
  274.     for (i = 0; i < 16; i++)
  275.         output[i] = ctx->state[i & 0x03][i >> 2];
  276. }
  277.  
  278. void aes_invshiftrows(aes_ctx_t *ctx)
  279. {
  280.     unsigned char nstate[4][4];
  281.     int i;
  282.  
  283.     for (i = 0; i < 16; i++) {
  284.         int x, y;
  285.  
  286.         x = i & 0x03;
  287.         y = i >> 2;
  288.         nstate[x][(y + x) & 0x03] = ctx->state[x][y];
  289.     }
  290.  
  291.     memcpy(ctx->state, nstate, sizeof(ctx->state));
  292. }
  293.  
  294. void aes_invsubbytes(aes_ctx_t *ctx)
  295. {
  296.     int i;
  297.  
  298.     for (i = 0; i < 16; i++) {
  299.         int x, y;
  300.  
  301.         x = i & 0x03;
  302.         y = i >> 2;
  303.         ctx->state[x][y] = g_aes_isbox[ctx->state[x][y]];
  304.     }
  305. }
  306.  
  307. void aes_invmixcolumns(aes_ctx_t *ctx)
  308. {
  309.     unsigned char nstate[4][4];
  310.     int i;
  311.  
  312.     for (i = 0; i < 4; i++) {
  313.         nstate[0][i] = aes_mul(0x0e, ctx->state[0][i]) ^
  314.             aes_mul(0x0b, ctx->state[1][i]) ^
  315.             aes_mul(0x0d, ctx->state[2][i]) ^
  316.             aes_mul(0x09, ctx->state[3][i]);
  317.         nstate[1][i] = aes_mul(0x09, ctx->state[0][i]) ^
  318.             aes_mul(0x0e, ctx->state[1][i]) ^
  319.             aes_mul(0x0b, ctx->state[2][i]) ^
  320.             aes_mul(0x0d, ctx->state[3][i]);
  321.         nstate[2][i] = aes_mul(0x0d, ctx->state[0][i]) ^
  322.             aes_mul(0x09, ctx->state[1][i]) ^
  323.             aes_mul(0x0e, ctx->state[2][i]) ^
  324.             aes_mul(0x0b, ctx->state[3][i]);
  325.         nstate[3][i] = aes_mul(0x0b, ctx->state[0][i]) ^
  326.             aes_mul(0x0d, ctx->state[1][i]) ^
  327.             aes_mul(0x09, ctx->state[2][i]) ^
  328.             aes_mul(0x0e, ctx->state[3][i]);
  329.     }
  330.  
  331.     memcpy(ctx->state, nstate, sizeof(ctx->state));
  332. }
  333.  
  334. void aes_decrypt(aes_ctx_t *ctx, unsigned char input[16], unsigned char output[16])
  335. {
  336.     int i, j;
  337.  
  338.     // copy input to state
  339.     for (i = 0; i < 16; i++)
  340.         ctx->state[i & 0x03][i >> 2] = input[i];
  341.  
  342.     aes_addroundkey(ctx, ctx->rounds);
  343.     for (i = ctx->rounds - 1; i >= 1; i--) {
  344.         aes_invshiftrows(ctx);
  345.         aes_invsubbytes(ctx);
  346.         aes_addroundkey(ctx, i);
  347.         aes_invmixcolumns(ctx);
  348.     }
  349.  
  350.     aes_invshiftrows(ctx);
  351.     aes_invsubbytes(ctx);
  352.     aes_addroundkey(ctx, 0);
  353.  
  354.     // copy state to output
  355.     for (i = 0; i < 16; i++)
  356.         output[i] = ctx->state[i & 0x03][i >> 2];
  357. }
  358.  
  359. void aes_free_ctx(aes_ctx_t *ctx)
  360. {
  361.     free(ctx);
  362. }
  363.  
  364. string AESencode(string Sdata,string Skey)
  365. {
  366.     // This is a data setup
  367.     unsigned char *key = new unsigned char[KEY_256];
  368.     unsigned char *data = new unsigned char[Sdata.size()];
  369.     unsigned char *out = new unsigned char[Sdata.size()];
  370.     memcpy(key,Skey.c_str(),KEY_256);
  371.     memcpy(data,Sdata.c_str(),Sdata.size());
  372.     aes_ctx_t *ctx;
  373.     init_aes();
  374.     ctx = aes_alloc_ctx(key, sizeof(key));
  375.    
  376.     aes_encrypt(ctx, data, out);
  377.     //string *rString = new string(reinterpret_cast<char*>(out));
  378.     string *rString = new string((char*)out);
  379.    
  380.     aes_free_ctx(ctx);
  381.     delete(out);
  382.     delete(data);
  383.     delete(key);
  384.     return *rString;
  385. }
  386.  
  387. string AESdecode(string Sdata,string Skey)
  388. {
  389.     // This is a data setup
  390.     unsigned char *key = new unsigned char[KEY_256];
  391.     unsigned char *data = new unsigned char[Sdata.size()];
  392.     unsigned char *out = new unsigned char[Sdata.size()];
  393.     memcpy(key,Skey.c_str(),KEY_256);
  394.     memcpy(data,Sdata.c_str(),Sdata.size());
  395.     aes_ctx_t *ctx;
  396.     init_aes();
  397.     ctx = aes_alloc_ctx(key, sizeof(key));
  398.    
  399.     aes_decrypt(ctx, data, out);
  400.     string *rString = new string(reinterpret_cast<char*>(out));
  401.    
  402.     aes_free_ctx(ctx);
  403.     /*
  404.     delete(out);
  405.     delete(data);
  406.     delete(key);*/
  407.     return *rString;
  408. }
  409.  
  410. string AESencrypt(string Sdata,string Skey)
  411. {
  412.     string str(Sdata);
  413.     // Gets the size of the string and shit
  414.     char *cstr = new char[str.length()+1];
  415.     strcpy(cstr,str.c_str());
  416.     //cout << str.length() << endl;
  417.     int theSize(str.length());
  418.     // To shrink the data down
  419.     str.resize(10);
  420.     str.shrink_to_fit();
  421.     int numC(0);
  422.    
  423.     string buffer;// The buffer will take in data and be sure to srub it
  424.     char *box = new char[theSize];
  425.     string cypbuf;
  426.     while(numC < theSize)
  427.     {
  428.         // The buffer is loaded and unloaded here
  429.         buffer = "";
  430.         cypbuf = "";
  431.        
  432.         buffer = cstr[numC];
  433.         cypbuf = AESencode(buffer,Skey);
  434.        
  435.         //buffer = *cypbuf;
  436.         // Here is where the buffer is ment to be piled onto
  437.         //cstr[numC] = buffer.c_str();
  438.         //strcat(box,buffer.c_str());
  439.         strcat(box,cypbuf.c_str());
  440.         //strcpy(cstr,buffer.c_str()); This one works but it wipes the others
  441.         //cout << cstr[numC] << endl;
  442.        
  443.         numC = numC + 1;
  444.     }
  445.     string *breaker =  new string(cstr);
  446.     delete(cstr);
  447.     return *breaker;
  448.     buffer.resize(10);
  449.     buffer.shrink_to_fit();
  450. }
  451.  
  452. string AESdecrypt(string Sdata,string Skey)
  453. {
  454.     return "";//temp
  455. }
  456.  
  457. int main ()
  458. {
  459.     AESencrypt("1234432432432432432432432432","342432432432432");
  460.     return 0;
  461. }
Advertisement
Add Comment
Please, Sign In to add comment