Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.90 KB | None | 0 0
  1. import java.util.Arrays;
  2.  
  3. public class AES {
  4.     private static int Nb, Nk, Nr;
  5.     private static short[][] w;
  6.  
  7.     private static int[] sbox = { 0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F,
  8.             0xC5, 0x30, 0x01, 0x67, 0x2B, 0xFE, 0xD7, 0xAB, 0x76, 0xCA, 0x82,
  9.             0xC9, 0x7D, 0xFA, 0x59, 0x47, 0xF0, 0xAD, 0xD4, 0xA2, 0xAF, 0x9C,
  10.             0xA4, 0x72, 0xC0, 0xB7, 0xFD, 0x93, 0x26, 0x36, 0x3F, 0xF7, 0xCC,
  11.             0x34, 0xA5, 0xE5, 0xF1, 0x71, 0xD8, 0x31, 0x15, 0x04, 0xC7, 0x23,
  12.             0xC3, 0x18, 0x96, 0x05, 0x9A, 0x07, 0x12, 0x80, 0xE2, 0xEB, 0x27,
  13.             0xB2, 0x75, 0x09, 0x83, 0x2C, 0x1A, 0x1B, 0x6E, 0x5A, 0xA0, 0x52,
  14.             0x3B, 0xD6, 0xB3, 0x29, 0xE3, 0x2F, 0x84, 0x53, 0xD1, 0x00, 0xED,
  15.             0x20, 0xFC, 0xB1, 0x5B, 0x6A, 0xCB, 0xBE, 0x39, 0x4A, 0x4C, 0x58,
  16.             0xCF, 0xD0, 0xEF, 0xAA, 0xFB, 0x43, 0x4D, 0x33, 0x85, 0x45, 0xF9,
  17.             0x02, 0x7F, 0x50, 0x3C, 0x9F, 0xA8, 0x51, 0xA3, 0x40, 0x8F, 0x92,
  18.             0x9D, 0x38, 0xF5, 0xBC, 0xB6, 0xDA, 0x21, 0x10, 0xFF, 0xF3, 0xD2,
  19.             0xCD, 0x0C, 0x13, 0xEC, 0x5F, 0x97, 0x44, 0x17, 0xC4, 0xA7, 0x7E,
  20.             0x3D, 0x64, 0x5D, 0x19, 0x73, 0x60, 0x81, 0x4F, 0xDC, 0x22, 0x2A,
  21.             0x90, 0x88, 0x46, 0xEE, 0xB8, 0x14, 0xDE, 0x5E, 0x0B, 0xDB, 0xE0,
  22.             0x32, 0x3A, 0x0A, 0x49, 0x06, 0x24, 0x5C, 0xC2, 0xD3, 0xAC, 0x62,
  23.             0x91, 0x95, 0xE4, 0x79, 0xE7, 0xC8, 0x37, 0x6D, 0x8D, 0xD5, 0x4E,
  24.             0xA9, 0x6C, 0x56, 0xF4, 0xEA, 0x65, 0x7A, 0xAE, 0x08, 0xBA, 0x78,
  25.             0x25, 0x2E, 0x1C, 0xA6, 0xB4, 0xC6, 0xE8, 0xDD, 0x74, 0x1F, 0x4B,
  26.             0xBD, 0x8B, 0x8A, 0x70, 0x3E, 0xB5, 0x66, 0x48, 0x03, 0xF6, 0x0E,
  27.             0x61, 0x35, 0x57, 0xB9, 0x86, 0xC1, 0x1D, 0x9E, 0xE1, 0xF8, 0x98,
  28.             0x11, 0x69, 0xD9, 0x8E, 0x94, 0x9B, 0x1E, 0x87, 0xE9, 0xCE, 0x55,
  29.             0x28, 0xDF, 0x8C, 0xA1, 0x89, 0x0D, 0xBF, 0xE6, 0x42, 0x68, 0x41,
  30.             0x99, 0x2D, 0x0F, 0xB0, 0x54, 0xBB, 0x16 };
  31.  
  32.     private static int[] Rcon = { 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20,
  33.             0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e,
  34.             0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef,
  35.             0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25,
  36.             0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb,
  37.             0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36,
  38.             0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97,
  39.             0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72,
  40.             0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66,
  41.             0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04,
  42.             0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d,
  43.             0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3,
  44.             0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61,
  45.             0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a,
  46.             0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40,
  47.             0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc,
  48.             0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5,
  49.             0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a,
  50.             0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d,
  51.             0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c,
  52.             0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35,
  53.             0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4,
  54.             0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc,
  55.             0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb };
  56.  
  57.     private static short[] xor_func(short[] a, short[] b) {
  58.         short[] outp = new short[a.length];
  59.         for (int i = 0; i < a.length; i++) {
  60.             outp[i] = (short) (a[i] ^ b[i]);
  61.         }
  62.         return outp;
  63.     }
  64.  
  65.     private static short[][] generateSubkeys(short[] keyshort) {
  66.         short[][] tmp = new short[Nb * (Nr + 1)][4];
  67.         int i = 0;
  68.         while (i < Nk) {
  69.             tmp[i][0] = keyshort[i * 4];
  70.             tmp[i][1] = keyshort[i * 4 + 1];
  71.             tmp[i][2] = keyshort[i * 4 + 2];
  72.             tmp[i][3] = keyshort[i * 4 + 3];
  73.             i++;
  74.         }
  75.         i = Nk;
  76.         while (i < Nb * (Nr + 1)) {
  77.             short[] temp = new short[4];
  78.             for (int k = 0; k < 4; k++)
  79.                 temp[k] = tmp[i - 1][k];
  80.             if (i % Nk == 0) {
  81.                 temp = subWord(rotateWord(temp));
  82.                 temp[0] = (short) (temp[0] ^ (Rcon[i / Nk] & 0xff));
  83.             } else if (Nk > 6 && i % Nk == 4) {
  84.                 temp = subWord(temp);
  85.             }
  86.             short[] tmp2 = { tmp[i - Nk][0], tmp[i - Nk][1], tmp[i - Nk][2],
  87.                     tmp[i - Nk][3] };
  88.             short[] result = new short[4];
  89.             result = xor_func(tmp2, temp);
  90.             tmp[i] = xor_func(tmp2, temp);
  91.             tmp[i][0] = result[0];
  92.             tmp[i][1] = result[1];
  93.             tmp[i][2] = result[2];
  94.             tmp[i][3] = result[3];
  95.             i++;
  96.         }
  97.         return tmp;
  98.     }
  99.  
  100.     private static short[] subWord(short[] inp) {
  101.         short[] tmp = new short[inp.length];
  102.         for (int i = 0; i < tmp.length; i++)
  103.             tmp[i] = (short) (sbox[inp[i] & 0x000000ff] & 0xff);
  104.         return tmp;
  105.     }
  106.  
  107.     private static short[] rotateWord(short[] input) {
  108.         short[] tmp = new short[input.length];
  109.         tmp[0] = input[1];
  110.         tmp[1] = input[2];
  111.         tmp[2] = input[3];
  112.         tmp[3] = input[0];
  113.         return tmp;
  114.     }
  115.  
  116.     private static short[][] addRoundKey(short[][] state, short[][] w, int round) {
  117.         short[][] tmp = new short[4][4];
  118.         for (int c = 0; c < Nb; c++) {
  119.             for (int l = 0; l < 4; l++)
  120.                 tmp[l][c] = (short) ((state[l][c] ^ w[round * Nb + c][l])%255);
  121.         }
  122.         return tmp;
  123.     }
  124.  
  125.     private static short[][] subBytes(short[][] state) {
  126.  
  127.         short[][] tmp = new short[4][4];
  128.         for (int row = 0; row < 4; row++)
  129.             for (int col = 0; col < Nb; col++)
  130.                 tmp[row][col] = (short) (sbox[(state[row][col] & 0x000000ff)] & 0xff);
  131.  
  132.         return tmp;
  133.     }
  134.  
  135.     private static short[][] shiftRows(short[][] state) {
  136.  
  137.         short[] t = new short[4];
  138.         for (int r = 1; r < 4; r++) {
  139.             for (int c = 0; c < Nb; c++)
  140.                 t[c] = state[r][(c + r) % Nb];
  141.             for (int c = 0; c < Nb; c++)
  142.                 state[r][c] = t[c];
  143.         }
  144.  
  145.         return state;
  146.     }
  147.  
  148.     private static short[][] mixColumns(short[][] s) {
  149.         int[] sp = new int[4];
  150.         short b02 = (short) 0x02, b03 = (short) 0x03;
  151.         for (int c = 0; c < 4; c++) {
  152.             sp[0] = fFMul(b02, s[0][c]) ^ fFMul(b03, s[1][c]) ^ s[2][c]
  153.                     ^ s[3][c];
  154.             sp[1] = s[0][c] ^ fFMul(b02, s[1][c]) ^ fFMul(b03, s[2][c])
  155.                     ^ s[3][c];
  156.             sp[2] = s[0][c] ^ s[1][c] ^ fFMul(b02, s[2][c])
  157.                     ^ fFMul(b03, s[3][c]);
  158.             sp[3] = fFMul(b03, s[0][c]) ^ s[1][c] ^ s[2][c]
  159.                     ^ fFMul(b02, s[3][c]);
  160.             for (int i = 0; i < 4; i++) {
  161.                 if(sp[i]>255)
  162.                     sp[i]=sp[i]^0x1b;
  163.                 s[i][c] = (short) (sp[i]);
  164.             }
  165.         }
  166.  
  167.         return s;
  168.     }
  169.  
  170.     public static short fFMul(short a, short b) {
  171.         short aa = a, bb = b, r = 0, t;
  172.         while (aa != 0) {
  173.             if ((aa & 1) != 0)
  174.                 r = (short) (r ^ bb);
  175.             t = (short) (bb & 0x80);
  176.             bb = (short) (bb << 1);
  177.             if (t != 0)
  178.                 bb = (short) (bb ^ 0x1b);
  179.             aa = (short) ((aa & 0xff) >> 1);
  180.         }
  181.         return r;
  182.     }
  183.  
  184.     public static short[][] encryptBloc(short[] inp) {
  185.         short[][] state = new short[4][Nb];
  186.  
  187.         for (int i = 0; i < inp.length; i++)
  188.             state[i / 4][i % 4] = inp[i % 4 * 4 + i / 4];
  189.  
  190.         state = addRoundKey(state, w, 0);
  191.         for (short[] j : state) {
  192.             for (short k : j) {
  193.                 System.out.print(Integer.toHexString(k) + " ");
  194.             }
  195.         }
  196.         System.out.println();
  197.         for (int round = 1; round < Nr; round++) {
  198.             state = subBytes(state);
  199.             state = shiftRows(state);
  200.             state = mixColumns(state);
  201.             state = addRoundKey(state, w, round);
  202.             for (short[] j : state) {
  203.                 for (short k : j) {
  204.                     System.out.print(Integer.toHexString(k) + " ");
  205.                 }
  206.             }
  207.             System.out.println();
  208.         }
  209.         state = subBytes(state);
  210.         state = shiftRows(state);
  211.         state = addRoundKey(state, w, Nr);
  212.         for (short[] j : state) {
  213.             for (short k : j) {
  214.                 System.out.print(Integer.toHexString(k) + " ");
  215.             }
  216.         }
  217.         System.out.println();
  218.         return state;
  219.     }
  220.  
  221.     public static short[][] encrypt(short[] messageByte, short[] keyByte) {
  222.  
  223.         Nb = 4;
  224.         Nk = keyByte.length / 4;
  225.         Nr = Nk + 6;
  226.         short[][] tmp = new short[messageByte.length][messageByte.length];
  227.  
  228.         w = generateSubkeys(keyByte);
  229.         short[][] bloc = encryptBloc(messageByte);
  230.         tmp = Arrays.copyOf(bloc, bloc.length);
  231.         return tmp;
  232.     }
  233.  
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement