Posted By

onurcanoz on 05/15/17


Tagged

java Encryption aes


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

onurcanoz


aes encryption


 / Published in: Java
 

here is my aes code

  1. public class AES {
  2.  
  3. String text;
  4. String cipher_text;
  5.  
  6. int state_matrix[];
  7. int tmp[];
  8.  
  9. int i,j;
  10.  
  11. private final char s_box[] = {
  12. 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe,0xd7, 0xab, 0x76,
  13. 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72,0xc0,
  14. 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15,
  15. 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75,
  16. 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84,
  17. 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf,
  18. 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8,
  19. 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2,
  20. 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73,
  21. 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb,
  22. 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79,
  23. 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08,
  24. 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a,
  25. 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e,
  26. 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
  27. 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16
  28. };
  29.  
  30. private final char mul2[]= {
  31. 0x00,0x02,0x04,0x06,0x08,0x0a,0x0c,0x0e,0x10,0x12,0x14,0x16,0x18,0x1a,0x1c,0x1e,
  32. 0x20,0x22,0x24,0x26,0x28,0x2a,0x2c,0x2e,0x30,0x32,0x34,0x36,0x38,0x3a,0x3c,0x3e,
  33. 0x40,0x42,0x44,0x46,0x48,0x4a,0x4c,0x4e,0x50,0x52,0x54,0x56,0x58,0x5a,0x5c,0x5e,
  34. 0x60,0x62,0x64,0x66,0x68,0x6a,0x6c,0x6e,0x70,0x72,0x74,0x76,0x78,0x7a,0x7c,0x7e,
  35. 0x80,0x82,0x84,0x86,0x88,0x8a,0x8c,0x8e,0x90,0x92,0x94,0x96,0x98,0x9a,0x9c,0x9e,
  36. 0xa0,0xa2,0xa4,0xa6,0xa8,0xaa,0xac,0xae,0xb0,0xb2,0xb4,0xb6,0xb8,0xba,0xbc,0xbe,
  37. 0xc0,0xc2,0xc4,0xc6,0xc8,0xca,0xcc,0xce,0xd0,0xd2,0xd4,0xd6,0xd8,0xda,0xdc,0xde,
  38. 0xe0,0xe2,0xe4,0xe6,0xe8,0xea,0xec,0xee,0xf0,0xf2,0xf4,0xf6,0xf8,0xfa,0xfc,0xfe,
  39. 0x1b,0x19,0x1f,0x1d,0x13,0x11,0x17,0x15,0x0b,0x09,0x0f,0x0d,0x03,0x01,0x07,0x05,
  40. 0x3b,0x39,0x3f,0x3d,0x33,0x31,0x37,0x35,0x2b,0x29,0x2f,0x2d,0x23,0x21,0x27,0x25,
  41. 0x5b,0x59,0x5f,0x5d,0x53,0x51,0x57,0x55,0x4b,0x49,0x4f,0x4d,0x43,0x41,0x47,0x45,
  42. 0x7b,0x79,0x7f,0x7d,0x73,0x71,0x77,0x75,0x6b,0x69,0x6f,0x6d,0x63,0x61,0x67,0x65,
  43. 0x9b,0x99,0x9f,0x9d,0x93,0x91,0x97,0x95,0x8b,0x89,0x8f,0x8d,0x83,0x81,0x87,0x85,
  44. 0xbb,0xb9,0xbf,0xbd,0xb3,0xb1,0xb7,0xb5,0xab,0xa9,0xaf,0xad,0xa3,0xa1,0xa7,0xa5,
  45. 0xdb,0xd9,0xdf,0xdd,0xd3,0xd1,0xd7,0xd5,0xcb,0xc9,0xcf,0xcd,0xc3,0xc1,0xc7,0xc5,
  46. 0xfb,0xf9,0xff,0xfd,0xf3,0xf1,0xf7,0xf5,0xeb,0xe9,0xef,0xed,0xe3,0xe1,0xe7,0xe5
  47. };
  48.  
  49. private final char mul3[]={
  50. 0x00,0x03,0x06,0x05,0x0c,0x0f,0x0a,0x09,0x18,0x1b,0x1e,0x1d,0x14,0x17,0x12,0x11,
  51. 0x30,0x33,0x36,0x35,0x3c,0x3f,0x3a,0x39,0x28,0x2b,0x2e,0x2d,0x24,0x27,0x22,0x21,
  52. 0x60,0x63,0x66,0x65,0x6c,0x6f,0x6a,0x69,0x78,0x7b,0x7e,0x7d,0x74,0x77,0x72,0x71,
  53. 0x50,0x53,0x56,0x55,0x5c,0x5f,0x5a,0x59,0x48,0x4b,0x4e,0x4d,0x44,0x47,0x42,0x41,
  54. 0xc0,0xc3,0xc6,0xc5,0xcc,0xcf,0xca,0xc9,0xd8,0xdb,0xde,0xdd,0xd4,0xd7,0xd2,0xd1,
  55. 0xf0,0xf3,0xf6,0xf5,0xfc,0xff,0xfa,0xf9,0xe8,0xeb,0xee,0xed,0xe4,0xe7,0xe2,0xe1,
  56. 0xa0,0xa3,0xa6,0xa5,0xac,0xaf,0xaa,0xa9,0xb8,0xbb,0xbe,0xbd,0xb4,0xb7,0xb2,0xb1,
  57. 0x90,0x93,0x96,0x95,0x9c,0x9f,0x9a,0x99,0x88,0x8b,0x8e,0x8d,0x84,0x87,0x82,0x81,
  58. 0x9b,0x98,0x9d,0x9e,0x97,0x94,0x91,0x92,0x83,0x80,0x85,0x86,0x8f,0x8c,0x89,0x8a,
  59. 0xab,0xa8,0xad,0xae,0xa7,0xa4,0xa1,0xa2,0xb3,0xb0,0xb5,0xb6,0xbf,0xbc,0xb9,0xba,
  60. 0xfb,0xf8,0xfd,0xfe,0xf7,0xf4,0xf1,0xf2,0xe3,0xe0,0xe5,0xe6,0xef,0xec,0xe9,0xea,
  61. 0xcb,0xc8,0xcd,0xce,0xc7,0xc4,0xc1,0xc2,0xd3,0xd0,0xd5,0xd6,0xdf,0xdc,0xd9,0xda,
  62. 0x5b,0x58,0x5d,0x5e,0x57,0x54,0x51,0x52,0x43,0x40,0x45,0x46,0x4f,0x4c,0x49,0x4a,
  63. 0x6b,0x68,0x6d,0x6e,0x67,0x64,0x61,0x62,0x73,0x70,0x75,0x76,0x7f,0x7c,0x79,0x7a,
  64. 0x3b,0x38,0x3d,0x3e,0x37,0x34,0x31,0x32,0x23,0x20,0x25,0x26,0x2f,0x2c,0x29,0x2a,
  65. 0x0b,0x08,0x0d,0x0e,0x07,0x04,0x01,0x02,0x13,0x10,0x15,0x16,0x1f,0x1c,0x19,0x1a
  66. };
  67.  
  68. private final char key[][]={
  69. {0x54,0x68,0x61,0x74,0x73,0x20,0x6D,0x79,0x20,0x4B,0x75,0x6E,0x67,0x20,0x46,0x75},
  70. {0xE2,0x32,0xFC,0xF1,0x91,0x12,0x91,0x88,0xB1,0x59,0xE4,0xE6,0xD6,0x79,0xA2,0x93},
  71. {0x56,0x08,0x20,0x07,0xC7,0x1A,0xB1,0x8F,0x76,0x43,0x55,0x69,0xA0,0x3A,0xF7,0xFA},
  72. {0xD2,0x60,0x0D,0xE7,0x15,0x7A,0xBC,0x68,0x63,0x39,0xE9,0x01,0xC3,0x03,0x1E,0xFB},
  73. {0xA1,0x12,0x02,0xC9,0xB4,0x68,0xBE,0xA1,0xD7,0x51,0x57,0xA0,0x14,0x52,0x49,0x5B},
  74. {0xB1,0x29,0x3B,0x33,0x05,0x41,0x85,0x92,0xD2,0x10,0xD2,0x32,0xC6,0x42,0x9B,0x69},
  75. {0xBD,0x3D,0xC2,0xB7,0xB8,0x7C,0x47,0x15,0x6A,0x6C,0x95,0x27,0xAC,0x2E,0x0E,0x4E},
  76. {0xCC,0x96,0xED,0x16,0x74,0xEA,0xAA,0x03,0x1E,0x86,0x3F,0x24,0xB2,0xA8,0x31,0x6A},
  77. {0x8E,0x51,0xEF,0x21,0xFA,0xBB,0x45,0x22,0xE4,0x3D,0x7A,0x06,0x56,0x95,0x4B,0x6C},
  78. {0xBF,0xE2,0xBF,0x90,0x45,0x59,0xFA,0xB2,0xA1,0x64,0x80,0xB4,0xF7,0xF1,0xCB,0xD8},
  79. {0x28,0xFD,0xDE,0xF8,0x6D,0xA4,0x24,0x4A,0xCC,0xC0,0xA4,0xFE,0x3B,0x31,0x6F,0x26}
  80. };
  81.  
  82. public AES(String text){
  83.  
  84. this.text=text;
  85.  
  86. state_matrix=new int[16];
  87. tmp=new int[16];
  88. createStateMatrix();
  89.  
  90. addRoundKey(0);
  91. for(j=0;j<9;j++){
  92. subBytes();
  93. shiftRows();
  94. mixColumns();
  95. addRoundKey(j+1);
  96.  
  97. }
  98.  
  99. subBytes();
  100. shiftRows();
  101. addRoundKey(10);
  102.  
  103. printf();
  104.  
  105. for(i=0;i<16;i++){
  106. System.out.print((char)(state_matrix[i]));
  107. }
  108. }
  109.  
  110. public void createStateMatrix(){
  111.  
  112. byte[] byte_text = text.getBytes();
  113.  
  114. for(i=0;i<16;i++){
  115. state_matrix[i]=byte_text[i];
  116. }
  117. }
  118.  
  119. public void printf(){
  120.  
  121. System.out.println("State Matrix:\n");
  122. for(i=0;i<16;i++){
  123. if(i%4==0){
  124. System.out.println();
  125. System.out.print(state_matrix[i]+" ");
  126. }
  127.  
  128. else
  129. System.out.print(state_matrix[i]+",");
  130. }
  131. System.out.println("\n");
  132. }
  133.  
  134. public void addRoundKey(int index){
  135.  
  136. System.out.println("Add Round Key");
  137.  
  138. for(i=0;i<16;i++){
  139. state_matrix[i]^=key[index][i];
  140. }
  141.  
  142. }
  143.  
  144. public void subBytes(){
  145.  
  146. System.out.println("Sub Bytes");
  147.  
  148. for(i=0;i<16;i++){
  149. state_matrix[i]=s_box[state_matrix[i]];
  150. }
  151.  
  152. }
  153.  
  154. public void shiftRows(){
  155.  
  156. System.out.println("Shift Rows");
  157.  
  158. tmp[0]=state_matrix[0];
  159. tmp[1]=state_matrix[5];
  160. tmp[2]=state_matrix[10];
  161. tmp[3]=state_matrix[15];
  162.  
  163. tmp[4]=state_matrix[4];
  164. tmp[5]=state_matrix[9];
  165. tmp[6]=state_matrix[14];
  166. tmp[7]=state_matrix[3];
  167.  
  168. tmp[8]=state_matrix[8];
  169. tmp[9]=state_matrix[13];
  170. tmp[10]=state_matrix[2];
  171. tmp[11]=state_matrix[7];
  172.  
  173. tmp[12]=state_matrix[12];
  174. tmp[13]=state_matrix[1];
  175. tmp[14]=state_matrix[6];
  176. tmp[15]=state_matrix[11];
  177.  
  178. for(i=0;i<16;i++){
  179. state_matrix[i]=tmp[i];
  180. }
  181. }
  182.  
  183. public void mixColumns(){
  184.  
  185. System.out.println("Mix Columns");
  186.  
  187. tmp[0] = (mul2[state_matrix[0]] ^ mul3[state_matrix[1]] ^ state_matrix[2] ^ state_matrix[3]);
  188. tmp[1] = (state_matrix[0] ^ mul2[state_matrix[1]] ^ mul3[state_matrix[2]] ^ state_matrix[3]);
  189. tmp[2] = (state_matrix[0] ^ state_matrix[1] ^ mul2[state_matrix[2]] ^ mul3[state_matrix[3]]);
  190. tmp[3] = (mul3[state_matrix[0]] ^ state_matrix[1] ^ state_matrix[2] ^ mul2[state_matrix[3]]);
  191.  
  192. tmp[4] = (mul2[state_matrix[4]] ^ mul3[state_matrix[5]] ^ state_matrix[6] ^ state_matrix[7]);
  193. tmp[5] = (state_matrix[4] ^ mul2[state_matrix[5]] ^ mul3[state_matrix[6]] ^ state_matrix[7]);
  194. tmp[6] = (state_matrix[4] ^ state_matrix[5] ^ mul2[state_matrix[6]] ^ mul3[state_matrix[7]]);
  195. tmp[7] = (mul3[state_matrix[4]] ^ state_matrix[5] ^ state_matrix[6] ^ mul2[state_matrix[7]]);
  196.  
  197. tmp[8] = (mul2[state_matrix[8]] ^ mul3[state_matrix[9]] ^ state_matrix[10] ^ state_matrix[11]);
  198. tmp[9] = (state_matrix[8] ^ mul2[state_matrix[9]] ^ mul3[state_matrix[10]] ^ state_matrix[11]);
  199. tmp[10] = (state_matrix[8] ^ state_matrix[9] ^ mul2[state_matrix[10]] ^ mul3[state_matrix[11]]);
  200. tmp[11] = (mul3[state_matrix[8]] ^ state_matrix[9] ^ state_matrix[10] ^ mul2[state_matrix[11]]);
  201.  
  202. tmp[12] = (mul2[state_matrix[12]] ^ mul3[state_matrix[13]] ^ state_matrix[14] ^ state_matrix[15]);
  203. tmp[13] = (state_matrix[12] ^ mul2[state_matrix[13]] ^ mul3[state_matrix[14]] ^ state_matrix[15]);
  204. tmp[14] = (state_matrix[12] ^ state_matrix[13] ^ mul2[state_matrix[14]] ^ mul3[state_matrix[15]]);
  205. tmp[15] = (mul3[state_matrix[12]] ^ state_matrix[13] ^ state_matrix[14] ^ mul2[state_matrix[15]]);
  206.  
  207. for(int i=0;i<16;i++)
  208. state_matrix[i]=tmp[i];
  209.  
  210.  
  211. }
  212. }

Report this snippet  

You need to login to post a comment.