AI销管
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

234 řádky
8.4 KiB

  1. ;(function (root, factory, undef) {
  2. if (typeof exports === "object") {
  3. // CommonJS
  4. module.exports = exports = factory(require("./core"), require("./enc-base64"), require("./md5"), require("./evpkdf"), require("./cipher-core"));
  5. }
  6. else if (typeof define === "function" && define.amd) {
  7. // AMD
  8. define(["./core", "./enc-base64", "./md5", "./evpkdf", "./cipher-core"], factory);
  9. }
  10. else {
  11. // Global (browser)
  12. factory(root.CryptoJS);
  13. }
  14. }(this, function (CryptoJS) {
  15. (function () {
  16. // Shortcuts
  17. var C = CryptoJS;
  18. var C_lib = C.lib;
  19. var BlockCipher = C_lib.BlockCipher;
  20. var C_algo = C.algo;
  21. // Lookup tables
  22. var SBOX = [];
  23. var INV_SBOX = [];
  24. var SUB_MIX_0 = [];
  25. var SUB_MIX_1 = [];
  26. var SUB_MIX_2 = [];
  27. var SUB_MIX_3 = [];
  28. var INV_SUB_MIX_0 = [];
  29. var INV_SUB_MIX_1 = [];
  30. var INV_SUB_MIX_2 = [];
  31. var INV_SUB_MIX_3 = [];
  32. // Compute lookup tables
  33. (function () {
  34. // Compute double table
  35. var d = [];
  36. for (var i = 0; i < 256; i++) {
  37. if (i < 128) {
  38. d[i] = i << 1;
  39. } else {
  40. d[i] = (i << 1) ^ 0x11b;
  41. }
  42. }
  43. // Walk GF(2^8)
  44. var x = 0;
  45. var xi = 0;
  46. for (var i = 0; i < 256; i++) {
  47. // Compute sbox
  48. var sx = xi ^ (xi << 1) ^ (xi << 2) ^ (xi << 3) ^ (xi << 4);
  49. sx = (sx >>> 8) ^ (sx & 0xff) ^ 0x63;
  50. SBOX[x] = sx;
  51. INV_SBOX[sx] = x;
  52. // Compute multiplication
  53. var x2 = d[x];
  54. var x4 = d[x2];
  55. var x8 = d[x4];
  56. // Compute sub bytes, mix columns tables
  57. var t = (d[sx] * 0x101) ^ (sx * 0x1010100);
  58. SUB_MIX_0[x] = (t << 24) | (t >>> 8);
  59. SUB_MIX_1[x] = (t << 16) | (t >>> 16);
  60. SUB_MIX_2[x] = (t << 8) | (t >>> 24);
  61. SUB_MIX_3[x] = t;
  62. // Compute inv sub bytes, inv mix columns tables
  63. var t = (x8 * 0x1010101) ^ (x4 * 0x10001) ^ (x2 * 0x101) ^ (x * 0x1010100);
  64. INV_SUB_MIX_0[sx] = (t << 24) | (t >>> 8);
  65. INV_SUB_MIX_1[sx] = (t << 16) | (t >>> 16);
  66. INV_SUB_MIX_2[sx] = (t << 8) | (t >>> 24);
  67. INV_SUB_MIX_3[sx] = t;
  68. // Compute next counter
  69. if (!x) {
  70. x = xi = 1;
  71. } else {
  72. x = x2 ^ d[d[d[x8 ^ x2]]];
  73. xi ^= d[d[xi]];
  74. }
  75. }
  76. }());
  77. // Precomputed Rcon lookup
  78. var RCON = [0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36];
  79. /**
  80. * AES block cipher algorithm.
  81. */
  82. var AES = C_algo.AES = BlockCipher.extend({
  83. _doReset: function () {
  84. var t;
  85. // Skip reset of nRounds has been set before and key did not change
  86. if (this._nRounds && this._keyPriorReset === this._key) {
  87. return;
  88. }
  89. // Shortcuts
  90. var key = this._keyPriorReset = this._key;
  91. var keyWords = key.words;
  92. var keySize = key.sigBytes / 4;
  93. // Compute number of rounds
  94. var nRounds = this._nRounds = keySize + 6;
  95. // Compute number of key schedule rows
  96. var ksRows = (nRounds + 1) * 4;
  97. // Compute key schedule
  98. var keySchedule = this._keySchedule = [];
  99. for (var ksRow = 0; ksRow < ksRows; ksRow++) {
  100. if (ksRow < keySize) {
  101. keySchedule[ksRow] = keyWords[ksRow];
  102. } else {
  103. t = keySchedule[ksRow - 1];
  104. if (!(ksRow % keySize)) {
  105. // Rot word
  106. t = (t << 8) | (t >>> 24);
  107. // Sub word
  108. t = (SBOX[t >>> 24] << 24) | (SBOX[(t >>> 16) & 0xff] << 16) | (SBOX[(t >>> 8) & 0xff] << 8) | SBOX[t & 0xff];
  109. // Mix Rcon
  110. t ^= RCON[(ksRow / keySize) | 0] << 24;
  111. } else if (keySize > 6 && ksRow % keySize == 4) {
  112. // Sub word
  113. t = (SBOX[t >>> 24] << 24) | (SBOX[(t >>> 16) & 0xff] << 16) | (SBOX[(t >>> 8) & 0xff] << 8) | SBOX[t & 0xff];
  114. }
  115. keySchedule[ksRow] = keySchedule[ksRow - keySize] ^ t;
  116. }
  117. }
  118. // Compute inv key schedule
  119. var invKeySchedule = this._invKeySchedule = [];
  120. for (var invKsRow = 0; invKsRow < ksRows; invKsRow++) {
  121. var ksRow = ksRows - invKsRow;
  122. if (invKsRow % 4) {
  123. var t = keySchedule[ksRow];
  124. } else {
  125. var t = keySchedule[ksRow - 4];
  126. }
  127. if (invKsRow < 4 || ksRow <= 4) {
  128. invKeySchedule[invKsRow] = t;
  129. } else {
  130. invKeySchedule[invKsRow] = INV_SUB_MIX_0[SBOX[t >>> 24]] ^ INV_SUB_MIX_1[SBOX[(t >>> 16) & 0xff]] ^
  131. INV_SUB_MIX_2[SBOX[(t >>> 8) & 0xff]] ^ INV_SUB_MIX_3[SBOX[t & 0xff]];
  132. }
  133. }
  134. },
  135. encryptBlock: function (M, offset) {
  136. this._doCryptBlock(M, offset, this._keySchedule, SUB_MIX_0, SUB_MIX_1, SUB_MIX_2, SUB_MIX_3, SBOX);
  137. },
  138. decryptBlock: function (M, offset) {
  139. // Swap 2nd and 4th rows
  140. var t = M[offset + 1];
  141. M[offset + 1] = M[offset + 3];
  142. M[offset + 3] = t;
  143. this._doCryptBlock(M, offset, this._invKeySchedule, INV_SUB_MIX_0, INV_SUB_MIX_1, INV_SUB_MIX_2, INV_SUB_MIX_3, INV_SBOX);
  144. // Inv swap 2nd and 4th rows
  145. var t = M[offset + 1];
  146. M[offset + 1] = M[offset + 3];
  147. M[offset + 3] = t;
  148. },
  149. _doCryptBlock: function (M, offset, keySchedule, SUB_MIX_0, SUB_MIX_1, SUB_MIX_2, SUB_MIX_3, SBOX) {
  150. // Shortcut
  151. var nRounds = this._nRounds;
  152. // Get input, add round key
  153. var s0 = M[offset] ^ keySchedule[0];
  154. var s1 = M[offset + 1] ^ keySchedule[1];
  155. var s2 = M[offset + 2] ^ keySchedule[2];
  156. var s3 = M[offset + 3] ^ keySchedule[3];
  157. // Key schedule row counter
  158. var ksRow = 4;
  159. // Rounds
  160. for (var round = 1; round < nRounds; round++) {
  161. // Shift rows, sub bytes, mix columns, add round key
  162. var t0 = SUB_MIX_0[s0 >>> 24] ^ SUB_MIX_1[(s1 >>> 16) & 0xff] ^ SUB_MIX_2[(s2 >>> 8) & 0xff] ^ SUB_MIX_3[s3 & 0xff] ^ keySchedule[ksRow++];
  163. var t1 = SUB_MIX_0[s1 >>> 24] ^ SUB_MIX_1[(s2 >>> 16) & 0xff] ^ SUB_MIX_2[(s3 >>> 8) & 0xff] ^ SUB_MIX_3[s0 & 0xff] ^ keySchedule[ksRow++];
  164. var t2 = SUB_MIX_0[s2 >>> 24] ^ SUB_MIX_1[(s3 >>> 16) & 0xff] ^ SUB_MIX_2[(s0 >>> 8) & 0xff] ^ SUB_MIX_3[s1 & 0xff] ^ keySchedule[ksRow++];
  165. var t3 = SUB_MIX_0[s3 >>> 24] ^ SUB_MIX_1[(s0 >>> 16) & 0xff] ^ SUB_MIX_2[(s1 >>> 8) & 0xff] ^ SUB_MIX_3[s2 & 0xff] ^ keySchedule[ksRow++];
  166. // Update state
  167. s0 = t0;
  168. s1 = t1;
  169. s2 = t2;
  170. s3 = t3;
  171. }
  172. // Shift rows, sub bytes, add round key
  173. var t0 = ((SBOX[s0 >>> 24] << 24) | (SBOX[(s1 >>> 16) & 0xff] << 16) | (SBOX[(s2 >>> 8) & 0xff] << 8) | SBOX[s3 & 0xff]) ^ keySchedule[ksRow++];
  174. var t1 = ((SBOX[s1 >>> 24] << 24) | (SBOX[(s2 >>> 16) & 0xff] << 16) | (SBOX[(s3 >>> 8) & 0xff] << 8) | SBOX[s0 & 0xff]) ^ keySchedule[ksRow++];
  175. var t2 = ((SBOX[s2 >>> 24] << 24) | (SBOX[(s3 >>> 16) & 0xff] << 16) | (SBOX[(s0 >>> 8) & 0xff] << 8) | SBOX[s1 & 0xff]) ^ keySchedule[ksRow++];
  176. var t3 = ((SBOX[s3 >>> 24] << 24) | (SBOX[(s0 >>> 16) & 0xff] << 16) | (SBOX[(s1 >>> 8) & 0xff] << 8) | SBOX[s2 & 0xff]) ^ keySchedule[ksRow++];
  177. // Set output
  178. M[offset] = t0;
  179. M[offset + 1] = t1;
  180. M[offset + 2] = t2;
  181. M[offset + 3] = t3;
  182. },
  183. keySize: 256/32
  184. });
  185. /**
  186. * Shortcut functions to the cipher's object interface.
  187. *
  188. * @example
  189. *
  190. * var ciphertext = CryptoJS.AES.encrypt(message, key, cfg);
  191. * var plaintext = CryptoJS.AES.decrypt(ciphertext, key, cfg);
  192. */
  193. C.AES = BlockCipher._createHelper(AES);
  194. }());
  195. return CryptoJS.AES;
  196. }));