Advertisement
Guest User

Untitled

a guest
Jun 13th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <cassert>
  3. #include <cstring>
  4. #include <cstdio>
  5.  
  6. using namespace std;
  7.  
  8. class AES
  9. {
  10. private:
  11. int Nb;
  12. int Nk;
  13. int Nr;
  14.  
  15. unsigned int blockBytesLen;
  16.  
  17. void SubBytes(unsigned char **state);
  18.  
  19. void ShiftRow(unsigned char **state, int i, int n); // shift row i on n positions
  20.  
  21. void ShiftRows(unsigned char **state);
  22.  
  23. unsigned char xtime(unsigned char b); // multiply on x
  24.  
  25. unsigned char mul_bytes(unsigned char a, unsigned char b);
  26.  
  27. void MixColumns(unsigned char **state);
  28.  
  29. void AddRoundKey(unsigned char **state, unsigned char *key);
  30.  
  31. void SubWord(unsigned char *a);
  32.  
  33. void RotWord(unsigned char *a);
  34.  
  35. void XorWords(unsigned char *a, unsigned char *b, unsigned char *c);
  36.  
  37. void Rcon(unsigned char * a, int n);
  38.  
  39. void InvSubBytes(unsigned char **state);
  40.  
  41. void InvMixColumns(unsigned char **state);
  42.  
  43. void InvShiftRows(unsigned char **state);
  44.  
  45. unsigned char* PaddingNulls(unsigned char in[], unsigned int inLen, unsigned int alignLen);
  46.  
  47. unsigned int GetPaddingLength(unsigned int len);
  48.  
  49. void KeyExpansion(unsigned char key[], unsigned char w[]);
  50.  
  51. void EncryptBlock(unsigned char in[], unsigned char out[], unsigned char key[]);
  52.  
  53. void DecryptBlock(unsigned char in[], unsigned char out[], unsigned char key[]);
  54.  
  55. void XorBlocks(unsigned char *a, unsigned char * b, unsigned char *c, unsigned int len);
  56.  
  57. public:
  58. AES(int keyLen);
  59.  
  60. unsigned char *EncryptECB(unsigned char in[], unsigned int inLen, unsigned char key[], unsigned int &outLen);
  61.  
  62. unsigned char *DecryptECB(unsigned char in[], unsigned int inLen, unsigned char key[], unsigned int &outLen);
  63.  
  64. };
  65.  
  66. const unsigned char sbox[16][16] = {
  67. 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5,
  68. 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
  69. 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0,
  70. 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0,
  71. 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc,
  72. 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15,
  73. 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a,
  74. 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75,
  75. 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0,
  76. 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84,
  77. 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b,
  78. 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf,
  79. 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85,
  80. 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8,
  81. 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5,
  82. 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2,
  83. 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17,
  84. 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73,
  85. 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88,
  86. 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb,
  87. 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c,
  88. 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79,
  89. 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9,
  90. 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08,
  91. 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6,
  92. 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a,
  93. 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e,
  94. 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e,
  95. 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94,
  96. 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
  97. 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68,
  98. 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16
  99. };
  100.  
  101. const unsigned char inv_sbox[16][16] = {
  102. 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38,
  103. 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb,
  104. 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87,
  105. 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb,
  106. 0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d,
  107. 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e,
  108. 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2,
  109. 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25,
  110. 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16,
  111. 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92,
  112. 0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda,
  113. 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84,
  114. 0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a,
  115. 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06,
  116. 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02,
  117. 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b,
  118. 0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea,
  119. 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73,
  120. 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85,
  121. 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e,
  122. 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89,
  123. 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b,
  124. 0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20,
  125. 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4,
  126. 0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31,
  127. 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f,
  128. 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d,
  129. 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef,
  130. 0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0,
  131. 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61,
  132. 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26,
  133. 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d, };
  134.  
  135. AES::AES(int keyLen = 256)
  136. {
  137. this->Nb = 4;
  138. switch (keyLen)
  139. {
  140. case 128:
  141. this->Nk = 4;
  142. this->Nr = 10;
  143. break;
  144. case 192:
  145. this->Nk = 6;
  146. this->Nr = 12;
  147. break;
  148. case 256:
  149. this->Nk = 8;
  150. this->Nr = 14;
  151. break;
  152. default:
  153. throw "Incorrect key length";
  154. }
  155.  
  156. blockBytesLen = 4 * this->Nb * sizeof(unsigned char);
  157. }
  158.  
  159. unsigned char * AES::EncryptECB(unsigned char in[], unsigned int inLen, unsigned char key[], unsigned int &outLen)
  160. {
  161. outLen = GetPaddingLength(inLen);
  162. unsigned char *alignIn = PaddingNulls(in, inLen, outLen);
  163. unsigned char *out = new unsigned char[outLen];
  164. for (unsigned int i = 0; i < outLen; i+= blockBytesLen)
  165. {
  166. EncryptBlock(alignIn + i, out + i, key);
  167. }
  168.  
  169. delete[] alignIn;
  170.  
  171. return out;
  172. }
  173.  
  174. unsigned char * AES::DecryptECB(unsigned char in[], unsigned int inLen, unsigned char key[], unsigned int &outLen)
  175. {
  176. outLen = GetPaddingLength(inLen);
  177. unsigned char *alignIn = PaddingNulls(in, inLen, outLen);
  178. unsigned char *out = new unsigned char[outLen];
  179. for (unsigned int i = 0; i < outLen; i+= blockBytesLen)
  180. {
  181. DecryptBlock(alignIn + i, out + i, key);
  182. }
  183.  
  184. delete[] alignIn;
  185.  
  186. return out;
  187. }
  188.  
  189. unsigned char * AES::PaddingNulls(unsigned char in[], unsigned int inLen, unsigned int alignLen)
  190. {
  191. unsigned char * alignIn = new unsigned char[alignLen];
  192. memcpy(alignIn, in, inLen);
  193. return alignIn;
  194. }
  195.  
  196. unsigned int AES::GetPaddingLength(unsigned int len)
  197. {
  198. return (len / blockBytesLen) * blockBytesLen;
  199. }
  200.  
  201. void AES::EncryptBlock(unsigned char in[], unsigned char out[], unsigned char key[])
  202. {
  203. unsigned char *w = new unsigned char[4 * Nb * (Nr + 1)];
  204. KeyExpansion(key, w);
  205. unsigned char **state = new unsigned char *[4];
  206. state[0] = new unsigned char[4 * Nb];
  207. int i, j, round;
  208. for (i = 0; i < 4; i++)
  209. {
  210. state[i] = state[0] + Nb * i;
  211. }
  212.  
  213.  
  214. for (i = 0; i < 4; i++)
  215. {
  216. for (j = 0; j < Nb; j++)
  217. {
  218. state[i][j] = in[i + 4 * j];
  219. }
  220. }
  221.  
  222. AddRoundKey(state, w);
  223.  
  224. for (round = 1; round <= Nr - 1; round++)
  225. {
  226. SubBytes(state);
  227. ShiftRows(state);
  228. MixColumns(state);
  229. AddRoundKey(state, w + round * 4 * Nb);
  230. }
  231.  
  232. SubBytes(state);
  233. ShiftRows(state);
  234. AddRoundKey(state, w + Nr * 4 * Nb);
  235.  
  236. for (i = 0; i < 4; i++)
  237. {
  238. for (j = 0; j < Nb; j++)
  239. {
  240. out[i + 4 * j] = state[i][j];
  241. }
  242. }
  243.  
  244. delete[] state[0];
  245. delete[] state;
  246. delete[] w;
  247. }
  248.  
  249. void AES::DecryptBlock(unsigned char in[], unsigned char out[], unsigned char key[])
  250. {
  251. unsigned char *w = new unsigned char[4 * Nb * (Nr + 1)];
  252. KeyExpansion(key, w);
  253. unsigned char **state = new unsigned char *[4];
  254. state[0] = new unsigned char[4 * Nb];
  255. int i, j, round;
  256. for (i = 0; i < 4; i++)
  257. {
  258. state[i] = state[0] + Nb * i;
  259. }
  260.  
  261.  
  262. for (i = 0; i < 4; i++)
  263. {
  264. for (j = 0; j < Nb; j++) {
  265. state[i][j] = in[i + 4 * j];
  266. }
  267. }
  268.  
  269. AddRoundKey(state, w + Nr * 4 * Nb);
  270.  
  271. for (round = Nr - 1; round >= 1; round--)
  272. {
  273. InvSubBytes(state);
  274. InvShiftRows(state);
  275. AddRoundKey(state, w + round * 4 * Nb);
  276. InvMixColumns(state);
  277. }
  278.  
  279. InvSubBytes(state);
  280. InvShiftRows(state);
  281. AddRoundKey(state, w);
  282.  
  283. for (i = 0; i < 4; i++)
  284. {
  285. for (j = 0; j < Nb; j++) {
  286. out[i + 4 * j] = state[i][j];
  287. }
  288. }
  289.  
  290. delete[] state[0];
  291. delete[] state;
  292. delete[] w;
  293. }
  294.  
  295.  
  296. void AES::SubBytes(unsigned char **state)
  297. {
  298. int i, j;
  299. unsigned char t;
  300. for (i = 0; i < 4; i++)
  301. {
  302. for (j = 0; j < Nb; j++)
  303. {
  304. t = state[i][j];
  305. state[i][j] = sbox[t / 16][t % 16];
  306. }
  307. }
  308.  
  309. }
  310.  
  311. void AES::ShiftRow(unsigned char **state, int i, int n) // shift row i on n positions
  312. {
  313. unsigned char t;
  314. int k, j;
  315. for (k = 0; k < n; k++)
  316. {
  317. t = state[i][0];
  318. for (j = 0; j < Nb - 1; j++)
  319. {
  320. state[i][j] = state[i][j + 1];
  321. }
  322. state[i][Nb - 1] = t;
  323. }
  324. }
  325.  
  326. void AES::ShiftRows(unsigned char **state)
  327. {
  328. ShiftRow(state, 1, 1);
  329. ShiftRow(state, 2, 2);
  330. ShiftRow(state, 3, 3);
  331. }
  332.  
  333. unsigned char AES::xtime(unsigned char b) // multiply on x
  334. {
  335. unsigned char mask = 0x80, m = 0x1b;
  336. unsigned char high_bit = b & mask;
  337. b = b << 1;
  338. if (high_bit) { // mod m(x)
  339. b = b ^ m;
  340. }
  341. return b;
  342. }
  343.  
  344. unsigned char AES::mul_bytes(unsigned char a, unsigned char b)
  345. {
  346. unsigned char c = 0, mask = 1, bit, d;
  347. int i, j;
  348. for (i = 0; i < 8; i++)
  349. {
  350. bit = b & mask;
  351. if (bit)
  352. {
  353. d = a;
  354. for (j = 0; j < i; j++)
  355. { // multiply on x^i
  356. d = xtime(d);
  357. }
  358. c = c ^ d; // xor to result
  359. }
  360. b = b >> 1;
  361. }
  362. return c;
  363. }
  364.  
  365. void AES::MixColumns(unsigned char **state)
  366. {
  367. unsigned char s[4], s1[4];
  368. int i, j;
  369.  
  370. for (j = 0; j < Nb; j++)
  371. {
  372. for (i = 0; i < 4; i++)
  373. {
  374. s[i] = state[i][j];
  375. }
  376.  
  377. s1[0] = mul_bytes(0x02, s[0]) ^ mul_bytes(0x03, s[1]) ^ s[2] ^ s[3];
  378. s1[1] = s[0] ^ mul_bytes(0x02, s[1]) ^ mul_bytes(0x03, s[2]) ^ s[3];
  379. s1[2] = s[0] ^ s[1] ^ mul_bytes(0x02, s[2]) ^ mul_bytes(0x03, s[3]);
  380. s1[3] = mul_bytes(0x03, s[0]) ^ s[1] ^ s[2] ^ mul_bytes(0x02, s[3]);
  381. for (i = 0; i < 4; i++)
  382. {
  383. state[i][j] = s1[i];
  384. }
  385.  
  386. }
  387.  
  388. }
  389.  
  390. void AES::AddRoundKey(unsigned char **state, unsigned char *key)
  391. {
  392. int i, j;
  393. for (i = 0; i < 4; i++)
  394. {
  395. for (j = 0; j < Nb; j++)
  396. {
  397. state[i][j] = state[i][j] ^ key[i + 4 * j];
  398. }
  399. }
  400. }
  401.  
  402. void AES::SubWord(unsigned char *a)
  403. {
  404. int i;
  405. for (i = 0; i < 4; i++)
  406. {
  407. a[i] = sbox[a[i] / 16][a[i] % 16];
  408. }
  409. }
  410.  
  411. void AES::RotWord(unsigned char *a)
  412. {
  413. unsigned char c = a[0];
  414. a[0] = a[1];
  415. a[1] = a[2];
  416. a[2] = a[3];
  417. a[3] = c;
  418. }
  419.  
  420. void AES::XorWords(unsigned char *a, unsigned char *b, unsigned char *c)
  421. {
  422. int i;
  423. for (i = 0; i < 4; i++)
  424. {
  425. c[i] = a[i] ^ b[i];
  426. }
  427. }
  428.  
  429. void AES::Rcon(unsigned char * a, int n)
  430. {
  431. int i;
  432. unsigned char c = 1;
  433. for (i = 0; i < n - 1; i++)
  434. {
  435. c = xtime(c);
  436. }
  437.  
  438. a[0] = c;
  439. a[1] = a[2] = a[3] = 0;
  440. }
  441.  
  442. void AES::KeyExpansion(unsigned char key[], unsigned char w[])
  443. {
  444. unsigned char *temp = new unsigned char[4];
  445. unsigned char *rcon = new unsigned char[4];
  446.  
  447. int i = 0;
  448. while (i < 4 * Nk)
  449. {
  450. w[i] = key[i];
  451. i++;
  452. }
  453.  
  454. i = 4 * Nk;
  455. while (i < 4 * Nb * (Nr + 1))
  456. {
  457. temp[0] = w[i - 4 + 0];
  458. temp[1] = w[i - 4 + 1];
  459. temp[2] = w[i - 4 + 2];
  460. temp[3] = w[i - 4 + 3];
  461.  
  462. if (i / 4 % Nk == 0)
  463. {
  464. RotWord(temp);
  465. SubWord(temp);
  466. Rcon(rcon, i / (Nk * 4));
  467. XorWords(temp, rcon, temp);
  468. }
  469. else if (Nk > 6 && i / 4 % Nk == 4)
  470. {
  471. SubWord(temp);
  472. }
  473.  
  474. w[i + 0] = w[i - 4 * Nk] ^ temp[0];
  475. w[i + 1] = w[i + 1 - 4 * Nk] ^ temp[1];
  476. w[i + 2] = w[i + 2 - 4 * Nk] ^ temp[2];
  477. w[i + 3] = w[i + 3 - 4 * Nk] ^ temp[3];
  478. i += 4;
  479. }
  480.  
  481. delete []rcon;
  482. delete []temp;
  483.  
  484. }
  485.  
  486.  
  487. void AES::InvSubBytes(unsigned char **state)
  488. {
  489. int i, j;
  490. unsigned char t;
  491. for (i = 0; i < 4; i++)
  492. {
  493. for (j = 0; j < Nb; j++)
  494. {
  495. t = state[i][j];
  496. state[i][j] = inv_sbox[t / 16][t % 16];
  497. }
  498. }
  499. }
  500.  
  501. void AES::InvMixColumns(unsigned char **state)
  502. {
  503. unsigned char s[4], s1[4];
  504. int i, j;
  505.  
  506. for (j = 0; j < Nb; j++)
  507. {
  508. for (i = 0; i < 4; i++)
  509. {
  510. s[i] = state[i][j];
  511. }
  512. s1[0] = mul_bytes(0x0e, s[0]) ^ mul_bytes(0x0b, s[1]) ^ mul_bytes(0x0d, s[2]) ^ mul_bytes(0x09, s[3]);
  513. s1[1] = mul_bytes(0x09, s[0]) ^ mul_bytes(0x0e, s[1]) ^ mul_bytes(0x0b, s[2]) ^ mul_bytes(0x0d, s[3]);
  514. s1[2] = mul_bytes(0x0d, s[0]) ^ mul_bytes(0x09, s[1]) ^ mul_bytes(0x0e, s[2]) ^ mul_bytes(0x0b, s[3]);
  515. s1[3] = mul_bytes(0x0b, s[0]) ^ mul_bytes(0x0d, s[1]) ^ mul_bytes(0x09, s[2]) ^ mul_bytes(0x0e, s[3]);
  516.  
  517. for (i = 0; i < 4; i++)
  518. {
  519. state[i][j] = s1[i];
  520. }
  521. }
  522. }
  523.  
  524. void AES::InvShiftRows(unsigned char **state)
  525. {
  526. ShiftRow(state, 1, Nb - 1);
  527. ShiftRow(state, 2, Nb - 2);
  528. ShiftRow(state, 3, Nb - 3);
  529. }
  530.  
  531. void AES::XorBlocks(unsigned char *a, unsigned char * b, unsigned char *c, unsigned int len)
  532. {
  533. for (unsigned int i = 0; i < len; i++)
  534. {
  535. c[i] = a[i] ^ b[i];
  536. }
  537. }
  538.  
  539. void Test128()
  540. {
  541. AES aes(128);
  542. //unsigned char plain[] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 };
  543. //unsigned char key[] = { 0x0f, 0x15, 0x71, 0xc9, 0x47, 0xd9, 0xe8, 0x59, 0x0c, 0xb7, 0xad, 0xd6, 0xaf, 0x7f, 0x67, 0x98 };
  544. //unsigned char right[] = { 0xff, 0x0b, 0x84, 0x4a, 0x08, 0x53, 0xbf, 0x7c, 0x69, 0x34, 0xab, 0x43, 0x64, 0x14, 0x8f, 0xb9 };
  545.  
  546. FILE *fp = fopen("key.txt", "r");
  547. if(fp == NULL) return ;
  548. unsigned char key[16] = {};
  549. int i = 0;
  550. while(!feof(fp)){
  551. unsigned char val;
  552. fscanf(fp, "%x", &val);
  553. key[i] = val;
  554. i += 1;
  555. }
  556. fclose(fp);
  557.  
  558. FILE *fp_n = fopen("nonce.txt", "r");
  559. if(fp_n == NULL) return ;
  560. unsigned char nonce[16] = {};
  561. i = 0;
  562. while(!feof(fp_n)){
  563. unsigned char val;
  564. fscanf(fp_n, "%x", &val);
  565. nonce[i] = val;
  566. i += 1;
  567. }
  568. fclose(fp_n);
  569.  
  570. FILE *fp_s = fopen("input.txt", "r");
  571. if(fp_s == NULL) return ;
  572. unsigned char plain[16] = {};
  573. i = 0;
  574. while(!feof(fp_s)){
  575. unsigned char val;
  576. fscanf(fp_s, "%x", &val);
  577. plain[i] = val;
  578. i += 1;
  579. if(i == 16) break;
  580. }
  581. fclose(fp_s);
  582.  
  583. unsigned int len = 0;
  584. unsigned char *out = aes.EncryptECB(nonce, 16 * sizeof(unsigned char), key, len);
  585.  
  586. for (unsigned int j = 0; j < 16; j++)
  587. {
  588. printf("%02x ", out[j] ^ plain[j]);
  589. }
  590. printf("\n");
  591. unsigned char *sout = aes.EncryptECB(out, 16 * sizeof(unsigned char), key, len);
  592. for (unsigned int i = 0; i < 16; i++)
  593. printf("%02x ", sout[i] ^ plain[i]);
  594. printf("\n");
  595.  
  596. //assert(!memcmp(right, out, 16 * sizeof(unsigned char)));
  597. //cout << "Test 128 [OK]" << endl;
  598. delete[] out;
  599. }
  600.  
  601. void Test192()
  602. {
  603. AES aes(192);
  604. unsigned char plain[] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff };
  605. unsigned char key[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x011,
  606. 0x12, 0x13, 0x14, 0x15, 0x16, 0x17};
  607. unsigned char right[] = {0xdd, 0xa9, 0x7c, 0xa4, 0x86, 0x4c, 0xdf, 0xe0, 0x6e, 0xaf, 0x70, 0xa0, 0xec, 0x0d, 0x71, 0x91 };
  608.  
  609. unsigned int len;
  610. unsigned char *out = aes.EncryptECB(plain, 16 * sizeof(unsigned char), key, len);
  611. for (unsigned int j = 0; j < 16; j++)
  612. printf("%02x ", out[j]);
  613. //assert(!memcmp(right, out, 16 * sizeof(unsigned char)));
  614. //cout << "Test 192 [OK]" << endl;
  615. delete[] out;
  616. }
  617.  
  618.  
  619. void Test256()
  620. {
  621. AES aes(256);
  622. unsigned char plain[] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff };
  623. unsigned char key[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x011,
  624. 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f };
  625. unsigned char right[] = { 0x8e, 0xa2, 0xb7, 0xca, 0x51, 0x67, 0x45, 0xbf, 0xea, 0xfc, 0x49, 0x90, 0x4b, 0x49, 0x60, 0x89 };
  626.  
  627. unsigned int len = 0;
  628. unsigned char *out = aes.EncryptECB(plain, 16 * sizeof(unsigned char), key, len);
  629. //assert(!memcmp(right, out, 16 * sizeof(unsigned char)));
  630. //cout << "Test 256 [OK]" << endl;
  631. delete[] out;
  632. }
  633.  
  634. int main()
  635. {
  636. Test128();
  637. //Test192();
  638. return 0;
  639. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement