JSAES

JSAES is a compact JavaScript implementation of the AES block cipher.

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.gf.qytechs.cn/scripts/23618/150018/JSAES.js

  1. /*
  2. * jsaes version 0.1 - Copyright 2006 B. Poettering
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  17. * 02111-1307 USA
  18. */
  19.  
  20. /*
  21. * http://point-at-infinity.org/jsaes/
  22. *
  23. * This is a javascript implementation of the AES block cipher. Key lengths
  24. * of 128, 192 and 256 bits are supported.
  25. *
  26. * The well-functioning of the encryption/decryption routines has been
  27. * verified for different key lengths with the test vectors given in
  28. * FIPS-197, Appendix C.
  29. *
  30. * The following code example enciphers the plaintext block '00 11 22 .. EE FF'
  31. * with the 256 bit key '00 01 02 .. 1E 1F'.
  32. *
  33. * AES_Init();
  34. *
  35. * var block = new Array(16);
  36. * for(var i = 0; i < 16; i++)
  37. * block[i] = 0x11 * i;
  38. *
  39. * var key = new Array(32);
  40. * for(var i = 0; i < 32; i++)
  41. * key[i] = i;
  42. *
  43. * AES_ExpandKey(key);
  44. * AES_Encrypt(block, key);
  45. *
  46. * AES_Done();
  47. *
  48. * Report bugs to: jsaes AT point-at-infinity.org
  49. *
  50. */
  51.  
  52. /******************************************************************************/
  53.  
  54. /*
  55. AES_Init: initialize the tables needed at runtime. Call this function
  56. before the (first) key expansion.
  57. */
  58.  
  59. function AES_Init() {
  60. AES_Sbox_Inv = new Array(256);
  61. for(var i = 0; i < 256; i++)
  62. AES_Sbox_Inv[AES_Sbox[i]] = i;
  63. AES_ShiftRowTab_Inv = new Array(16);
  64. for(var i = 0; i < 16; i++)
  65. AES_ShiftRowTab_Inv[AES_ShiftRowTab[i]] = i;
  66.  
  67. AES_xtime = new Array(256);
  68. for(var i = 0; i < 128; i++) {
  69. AES_xtime[i] = i << 1;
  70. AES_xtime[128 + i] = (i << 1) ^ 0x1b;
  71. }
  72. }
  73.  
  74. /*
  75. AES_Done: release memory reserved by AES_Init. Call this function after
  76. the last encryption/decryption operation.
  77. */
  78.  
  79. function AES_Done() {
  80. delete AES_Sbox_Inv;
  81. delete AES_ShiftRowTab_Inv;
  82. delete AES_xtime;
  83. }
  84.  
  85. /*
  86. AES_ExpandKey: expand a cipher key. Depending on the desired encryption
  87. strength of 128, 192 or 256 bits 'key' has to be a byte array of length
  88. 16, 24 or 32, respectively. The key expansion is done "in place", meaning
  89. that the array 'key' is modified.
  90. */
  91.  
  92. function AES_ExpandKey(key) {
  93. var kl = key.length, ks, Rcon = 1;
  94. switch (kl) {
  95. case 16: ks = 16 * (10 + 1); break;
  96. case 24: ks = 16 * (12 + 1); break;
  97. case 32: ks = 16 * (14 + 1); break;
  98. default:
  99. alert("AES_ExpandKey: Only key lengths of 16, 24 or 32 bytes allowed!");
  100. }
  101. for(var i = kl; i < ks; i += 4) {
  102. var temp = key.slice(i - 4, i);
  103. if (i % kl == 0) {
  104. temp = new Array(AES_Sbox[temp[1]] ^ Rcon, AES_Sbox[temp[2]],
  105. AES_Sbox[temp[3]], AES_Sbox[temp[0]]);
  106. if ((Rcon <<= 1) >= 256)
  107. Rcon ^= 0x11b;
  108. }
  109. else if ((kl > 24) && (i % kl == 16))
  110. temp = new Array(AES_Sbox[temp[0]], AES_Sbox[temp[1]],
  111. AES_Sbox[temp[2]], AES_Sbox[temp[3]]);
  112. for(var j = 0; j < 4; j++)
  113. key[i + j] = key[i + j - kl] ^ temp[j];
  114. }
  115. }
  116.  
  117. /*
  118. AES_Encrypt: encrypt the 16 byte array 'block' with the previously
  119. expanded key 'key'.
  120. */
  121.  
  122. function AES_Encrypt(block, key) {
  123. var l = key.length;
  124. AES_AddRoundKey(block, key.slice(0, 16));
  125. for(var i = 16; i < l - 16; i += 16) {
  126. AES_SubBytes(block, AES_Sbox);
  127. AES_ShiftRows(block, AES_ShiftRowTab);
  128. AES_MixColumns(block);
  129. AES_AddRoundKey(block, key.slice(i, i + 16));
  130. }
  131. AES_SubBytes(block, AES_Sbox);
  132. AES_ShiftRows(block, AES_ShiftRowTab);
  133. AES_AddRoundKey(block, key.slice(i, l));
  134. }
  135.  
  136. /*
  137. AES_Decrypt: decrypt the 16 byte array 'block' with the previously
  138. expanded key 'key'.
  139. */
  140.  
  141. function AES_Decrypt(block, key) {
  142. var l = key.length;
  143. AES_AddRoundKey(block, key.slice(l - 16, l));
  144. AES_ShiftRows(block, AES_ShiftRowTab_Inv);
  145. AES_SubBytes(block, AES_Sbox_Inv);
  146. for(var i = l - 32; i >= 16; i -= 16) {
  147. AES_AddRoundKey(block, key.slice(i, i + 16));
  148. AES_MixColumns_Inv(block);
  149. AES_ShiftRows(block, AES_ShiftRowTab_Inv);
  150. AES_SubBytes(block, AES_Sbox_Inv);
  151. }
  152. AES_AddRoundKey(block, key.slice(0, 16));
  153. }
  154.  
  155. /******************************************************************************/
  156.  
  157. /* The following lookup tables and functions are for internal use only! */
  158.  
  159. AES_Sbox = new Array(99,124,119,123,242,107,111,197,48,1,103,43,254,215,171,
  160. 118,202,130,201,125,250,89,71,240,173,212,162,175,156,164,114,192,183,253,
  161. 147,38,54,63,247,204,52,165,229,241,113,216,49,21,4,199,35,195,24,150,5,154,
  162. 7,18,128,226,235,39,178,117,9,131,44,26,27,110,90,160,82,59,214,179,41,227,
  163. 47,132,83,209,0,237,32,252,177,91,106,203,190,57,74,76,88,207,208,239,170,
  164. 251,67,77,51,133,69,249,2,127,80,60,159,168,81,163,64,143,146,157,56,245,
  165. 188,182,218,33,16,255,243,210,205,12,19,236,95,151,68,23,196,167,126,61,
  166. 100,93,25,115,96,129,79,220,34,42,144,136,70,238,184,20,222,94,11,219,224,
  167. 50,58,10,73,6,36,92,194,211,172,98,145,149,228,121,231,200,55,109,141,213,
  168. 78,169,108,86,244,234,101,122,174,8,186,120,37,46,28,166,180,198,232,221,
  169. 116,31,75,189,139,138,112,62,181,102,72,3,246,14,97,53,87,185,134,193,29,
  170. 158,225,248,152,17,105,217,142,148,155,30,135,233,206,85,40,223,140,161,
  171. 137,13,191,230,66,104,65,153,45,15,176,84,187,22);
  172.  
  173. AES_ShiftRowTab = new Array(0,5,10,15,4,9,14,3,8,13,2,7,12,1,6,11);
  174.  
  175. function AES_SubBytes(state, sbox) {
  176. for(var i = 0; i < 16; i++)
  177. state[i] = sbox[state[i]];
  178. }
  179.  
  180. function AES_AddRoundKey(state, rkey) {
  181. for(var i = 0; i < 16; i++)
  182. state[i] ^= rkey[i];
  183. }
  184.  
  185. function AES_ShiftRows(state, shifttab) {
  186. var h = new Array().concat(state);
  187. for(var i = 0; i < 16; i++)
  188. state[i] = h[shifttab[i]];
  189. }
  190.  
  191. function AES_MixColumns(state) {
  192. for(var i = 0; i < 16; i += 4) {
  193. var s0 = state[i + 0], s1 = state[i + 1];
  194. var s2 = state[i + 2], s3 = state[i + 3];
  195. var h = s0 ^ s1 ^ s2 ^ s3;
  196. state[i + 0] ^= h ^ AES_xtime[s0 ^ s1];
  197. state[i + 1] ^= h ^ AES_xtime[s1 ^ s2];
  198. state[i + 2] ^= h ^ AES_xtime[s2 ^ s3];
  199. state[i + 3] ^= h ^ AES_xtime[s3 ^ s0];
  200. }
  201. }
  202.  
  203. function AES_MixColumns_Inv(state) {
  204. for(var i = 0; i < 16; i += 4) {
  205. var s0 = state[i + 0], s1 = state[i + 1];
  206. var s2 = state[i + 2], s3 = state[i + 3];
  207. var h = s0 ^ s1 ^ s2 ^ s3;
  208. var xh = AES_xtime[h];
  209. var h1 = AES_xtime[AES_xtime[xh ^ s0 ^ s2]] ^ h;
  210. var h2 = AES_xtime[AES_xtime[xh ^ s1 ^ s3]] ^ h;
  211. state[i + 0] ^= h1 ^ AES_xtime[s0 ^ s1];
  212. state[i + 1] ^= h2 ^ AES_xtime[s1 ^ s2];
  213. state[i + 2] ^= h1 ^ AES_xtime[s2 ^ s3];
  214. state[i + 3] ^= h2 ^ AES_xtime[s3 ^ s0];
  215. }
  216. }

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址