一切福田,不離方寸,從心而覓,感無不通。

AES(Rijndael)算法的 JavaScript 源代码

下面的内容来自 Rijndael in JavaScript。 下面的代码是 Rijndael 算法的 JavaScript 实现。它可以在 IE4+、NS4+ 以及任何兼容 ECMAScript 第一版的浏览器中运行。这个实现没有进行优化,也就是说它不适合处理大量的数据(比如多于几 KB)和需要高速运行的应用程序中。

下载: rijndael.js
  1. /* rijndael.js      Rijndael Reference Implementation
  2.    Copyright (c) 2001 Fritz Schneider
  3. This software is provided as-is, without express or implied warranty.
  4. Permission to use, copy, modify, distribute or sell this software, with or
  5. without fee, for any purpose and by any individual or organization, is hereby
  6. granted, provided that the above copyright notice and this paragraph appear
  7. in all copies. Distribution as a part of an application or binary must
  8. include the above copyright notice in the documentation and/or other materials
  9. provided with the application or distribution.
  10.    As the above disclaimer notes, you are free to use this code however you
  11.    want. However, I would request that you send me an email
  12.    (fritz /at/ cs /dot/ ucsd /dot/ edu) to say hi if you find this code useful
  13.    or instructional. Seeing that people are using the code acts as
  14.    encouragement for me to continue development. If you *really* want to thank
  15.    me you can buy the book I wrote with Thomas Powell, _JavaScript:
  16.    _The_Complete_Reference_ :)
  17.    This code is an UNOPTIMIZED REFERENCE implementation of Rijndael.
  18.    If there is sufficient interest I can write an optimized (word-based,
  19.    table-driven) version, although you might want to consider using a
  20.    compiled language if speed is critical to your application. As it stands,
  21.    one run of the monte carlo test (10,000 encryptions) can take up to
  22.    several minutes, depending upon your processor. You shouldn’t expect more
  23.    than a few kilobytes per second in throughput.
  24.    Also note that there is very little error checking in these functions.
  25.    Doing proper error checking is always a good idea, but the ideal
  26.    implementation (using the instanceof operator and exceptions) requires
  27.    IE5+/NS6+, and I’ve chosen to implement this code so that it is compatible
  28.    with IE4/NS4.
  29.    And finally, because JavaScript doesn’t have an explicit byte/char data
  30.    type (although JavaScript 2.0 most likely will), when I refer to "byte"
  31.    in this code I generally mean "32 bit integer with value in the interval
  32.    [0,255]" which I treat as a byte.
  33.    See http://www-cse.ucsd.edu/~fritz/rijndael.html for more documentation
  34.    of the (very simple) API provided by this code.
  35.                                                Fritz Schneider
  36.                                                fritz at cs.ucsd.edu
  37. */
  38. // Rijndael parameters —  Valid values are 128, 192, or 256
  39. var keySizeInBits = 128;
  40. var blockSizeInBits = 128;
  41. ///////  You shouldn’t have to modify anything below this line except for
  42. ///////  the function getRandomBytes().
  43. //
  44. // Note: in the following code the two dimensional arrays are indexed as
  45. //       you would probably expect, as array[row][column]. The state arrays
  46. //       are 2d arrays of the form state[4][Nb].
  47. // The number of rounds for the cipher, indexed by [Nk][Nb]
  48. var roundsArray = [ ,,,,[,,,,10,, 12,, 14],,
  49.                         [,,,,12,, 12,, 14],,
  50.                         [,,,,14,, 14,, 14] ];
  51. // The number of bytes to shift by in shiftRow, indexed by [Nb][row]
  52. var shiftOffsets = [ ,,,,[,1, 2, 3],,[,1, 2, 3],,[,1, 3, 4] ];
  53. // The round constants used in subkey expansion
  54. var Rcon = [
  55. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20,
  56. 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8,
  57. 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc,
  58. 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4,
  59. 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91 ];
  60. // Precomputed lookup table for the SBox
  61. var SBox = [
  62.  99, 124, 119, 123, 242, 107, 111, 197,  48,   1, 103,  43, 254, 215, 171,
  63. 118, 202, 130, 201, 125, 250,  89,  71, 240, 173, 212, 162, 175, 156, 164,
  64. 114, 192, 183, 253, 147,  38,  54,  63, 247, 204,  52, 165, 229, 241, 113,
  65. 216,  49,  21,   4, 199,  35, 195,  24, 150,   5, 154,   7,  18, 128, 226,
  66. 235,  39, 178, 117,   9, 131,  44,  26,  27, 110,  90, 160,  82,  59, 214,
  67. 179,  41, 227,  47, 132,  83, 209,   0, 237,  32, 252, 177,  91, 106, 203,
  68. 190,  57,  74,  76,  88, 207, 208, 239, 170, 251,  67,  77,  51, 133,  69,
  69. 249,   2, 127,  80,  60, 159, 168,  81, 163,  64, 143, 146, 157,  56, 245,
  70. 188, 182, 218,  33,  16, 255, 243, 210, 205,  12,  19, 236,  95, 151,  68,
  71. 23,  196, 167, 126,  61, 100,  93,  25, 115,  96, 129,  79, 220,  34,  42,
  72. 144, 136,  70, 238, 184,  20, 222,  94,  11, 219, 224,  50,  58,  10,  73,
  73.   6,  36,  92, 194, 211, 172,  98, 145, 149, 228, 121, 231, 200,  55, 109,
  74. 141, 213,  78, 169, 108,  86, 244, 234, 101, 122, 174,   8, 186, 120,  37,
  75.  46,  28, 166, 180, 198, 232, 221, 116,  31,  75, 189, 139, 138, 112,  62,
  76. 181, 102,  72,   3, 246,  14,  97,  53,  87, 185, 134, 193,  29, 158, 225,
  77. 248, 152,  17, 105, 217, 142, 148, 155,  30, 135, 233, 206,  85,  40, 223,
  78. 140, 161, 137,  13, 191, 230,  66, 104,  65, 153,  45,  15, 176,  84, 187,
  79.  22 ];
  80. // Precomputed lookup table for the inverse SBox
  81. var SBoxInverse = [
  82.  82,   9, 106, 213,  48,  54, 165,  56, 191,  64, 163, 158, 129, 243, 215,
  83. 251, 124, 227,  57, 130, 155,  47, 255, 135,  52, 142,  67,  68, 196, 222,
  84. 233, 203,  84, 123, 148,  50, 166, 194,  35,  61, 238,  76, 149,  11,  66,
  85. 250, 195,  78,   8,  46, 161, 102,  40, 217,  36, 178, 118,  91, 162,  73,
  86. 109, 139, 209,  37, 114, 248, 246, 100, 134, 104, 152,  22, 212, 164,  92,
  87. 204,  93, 101, 182, 146, 108, 112,  72,  80, 253, 237, 185, 218,  94,  21,
  88.  70,  87, 167, 141, 157, 132, 144, 216, 171,   0, 140, 188, 211,  10, 247,
  89. 228,  88,   5, 184, 179,  69,   6, 208,  44,  30, 143, 202,  63,  15,   2,
  90. 193, 175, 189,   3,   1,  19, 138, 107,  58, 145,  17,  65,  79, 103, 220,
  91. 234, 151, 242, 207, 206, 240, 180, 230, 115, 150, 172, 116,  34, 231, 173,
  92.  53, 133, 226, 249,  55, 232,  28, 117, 223, 110,  71, 241,  26, 113,  29,
  93.  41, 197, 137, 111, 183,  98,  14, 170,  24, 190,  27, 252,  86,  62,  75,
  94. 198, 210, 121,  32, 154, 219, 192, 254, 120, 205,  90, 244,  31, 221, 168,
  95.  51, 136,   7, 199,  49, 177,  18,  16,  89,  39, 128, 236,  95,  96,  81,
  96. 127, 169,  25, 181,  74,  13,  45, 229, 122, 159, 147, 201, 156, 239, 160,
  97. 224,  59,  77, 174,  42, 245, 176, 200, 235, 187,  60, 131,  83, 153,  97,
  98.  23,  43,   4, 126, 186, 119, 214,  38, 225, 105,  20,  99,  85,  33,  12,
  99. 125 ];
  100. // This method circularly shifts the array left by the number of elements
  101. // given in its parameter. It returns the resulting array and is used for
  102. // the ShiftRow step. Note that shift() and push() could be used for a more
  103. // elegant solution, but they require IE5.5+, so I chose to do it manually.
  104. function cyclicShiftLeft(theArray, positions) {
  105.   var temp = theArray.slice(0, positions);
  106.   theArray = theArray.slice(positions).concat(temp);
  107.   return theArray;
  108. }
  109. // Cipher parameters … do not change these
  110. var Nk = keySizeInBits / 32;
  111. var Nb = blockSizeInBits / 32;
  112. var Nr = roundsArray[Nk][Nb];
  113. // Multiplies the element "poly" of GF(2^8) by x. See the Rijndael spec.
  114. function xtime(poly) {
  115.   poly <<= 1;
  116.   return ((poly & 0x100) ? (poly ^ 0x11B) : (poly));
  117. }
  118. // Multiplies the two elements of GF(2^8) together and returns the result.
  119. // See the Rijndael spec, but should be straightforward: for each power of
  120. // the indeterminant that has a 1 coefficient in x, add y times that power
  121. // to the result. x and y should be bytes representing elements of GF(2^8)
  122. function mult_GF256(x, y) {
  123.   var bit, result = 0;
  124.   for (bit = 1; bit < 256; bit *= 2, y = xtime(y)) {
  125.     if (x & bit)
  126.       result ^= y;
  127.   }
  128.   return result;
  129. }
  130. // Performs the substitution step of the cipher. State is the 2d array of
  131. // state information (see spec) and direction is string indicating whether
  132. // we are performing the forward substitution ("encrypt") or inverse
  133. // substitution (anything else)
  134. function byteSub(state, direction) {
  135.   var S;
  136.   if (direction == "encrypt")           // Point S to the SBox we’re using
  137.     S = SBox;
  138.   else
  139.     S = SBoxInverse;
  140.   for (var i = 0; i < 4; i++)           // Substitute for every byte in state
  141.     for (var j = 0; j < Nb; j++)
  142.        state[i][j] = S[state[i][j]];
  143. }
  144. // Performs the row shifting step of the cipher.
  145. function shiftRow(state, direction) {
  146.   for (var i=1; i<4; i++)               // Row 0 never shifts
  147.     if (direction == "encrypt")
  148.        state[i] = cyclicShiftLeft(state[i], shiftOffsets[Nb][i]);
  149.     else
  150.        state[i] = cyclicShiftLeft(state[i], Nb – shiftOffsets[Nb][i]);
  151. }
  152. // Performs the column mixing step of the cipher. Most of these steps can
  153. // be combined into table lookups on 32bit values (at least for encryption)
  154. // to greatly increase the speed.
  155. function mixColumn(state, direction) {
  156.   var b = [];                            // Result of matrix multiplications
  157.   for (var j = 0; j < Nb; j++) {         // Go through each column…
  158.     for (var i = 0; i < 4; i++) {        // and for each row in the column…
  159.       if (direction == "encrypt")
  160.         b[i] = mult_GF256(state[i][j], 2) ^          // perform mixing
  161.                mult_GF256(state[(i+1)%4][j], 3) ^
  162.                state[(i+2)%4][j] ^
  163.                state[(i+3)%4][j];
  164.       else
  165.         b[i] = mult_GF256(state[i][j], 0xE) ^
  166.                mult_GF256(state[(i+1)%4][j], 0xB) ^
  167.                mult_GF256(state[(i+2)%4][j], 0xD) ^
  168.                mult_GF256(state[(i+3)%4][j], 9);
  169.     }
  170.     for (var i = 0; i < 4; i++)          // Place result back into column
  171.       state[i][j] = b[i];
  172.   }
  173. }
  174. // Adds the current round key to the state information. Straightforward.
  175. function addRoundKey(state, roundKey) {
  176.   for (var j = 0; j < Nb; j++) {                 // Step through columns…
  177.     state[0][j] ^= (roundKey[j] & 0xFF);         // and XOR
  178.     state[1][j] ^= ((roundKey[j]>>8) & 0xFF);
  179.     state[2][j] ^= ((roundKey[j]>>16) & 0xFF);
  180.     state[3][j] ^= ((roundKey[j]>>24) & 0xFF);
  181.   }
  182. }
  183. // This function creates the expanded key from the input (128/192/256-bit)
  184. // key. The parameter key is an array of bytes holding the value of the key.
  185. // The returned value is an array whose elements are the 32-bit words that
  186. // make up the expanded key.
  187. function keyExpansion(key) {
  188.   var expandedKey = new Array();
  189.   var temp;
  190.   // in case the key size or parameters were changed…
  191.   Nk = keySizeInBits / 32;
  192.   Nb = blockSizeInBits / 32;
  193.   Nr = roundsArray[Nk][Nb];
  194.   for (var j=0; j < Nk; j++)     // Fill in input key first
  195.     expandedKey[j] =
  196.       (key[4*j]) | (key[4*j+1]<<8) | (key[4*j+2]<<16) | (key[4*j+3]<<24);
  197.   // Now walk down the rest of the array filling in expanded key bytes as
  198.   // per Rijndael’s spec
  199.   for (j = Nk; j < Nb * (Nr + 1); j++) {    // For each word of expanded key
  200.     temp = expandedKey[j – 1];
  201.     if (j % Nk == 0)
  202.       temp = ( (SBox[(temp>>8) & 0xFF]) |
  203.                (SBox[(temp>>16) & 0xFF]<<8) |
  204.                (SBox[(temp>>24) & 0xFF]<<16) |
  205.                (SBox[temp & 0xFF]<<24) ) ^ Rcon[Math.floor(j / Nk) – 1];
  206.     else if (Nk > 6 && j % Nk == 4)
  207.       temp = (SBox[(temp>>24) & 0xFF]<<24) |
  208.              (SBox[(temp>>16) & 0xFF]<<16) |
  209.              (SBox[(temp>>8) & 0xFF]<<8) |
  210.              (SBox[temp & 0xFF]);
  211.     expandedKey[j] = expandedKey[j-Nk] ^ temp;
  212.   }
  213.   return expandedKey;
  214. }
  215. // Rijndael’s round functions…
  216. function Round(state, roundKey) {
  217.   byteSub(state, "encrypt");
  218.   shiftRow(state, "encrypt");
  219.   mixColumn(state, "encrypt");
  220.   addRoundKey(state, roundKey);
  221. }
  222. function InverseRound(state, roundKey) {
  223.   addRoundKey(state, roundKey);
  224.   mixColumn(state, "decrypt");
  225.   shiftRow(state, "decrypt");
  226.   byteSub(state, "decrypt");
  227. }
  228. function FinalRound(state, roundKey) {
  229.   byteSub(state, "encrypt");
  230.   shiftRow(state, "encrypt");
  231.   addRoundKey(state, roundKey);
  232. }
  233. function InverseFinalRound(state, roundKey){
  234.   addRoundKey(state, roundKey);
  235.   shiftRow(state, "decrypt");
  236.   byteSub(state, "decrypt");
  237. }
  238. // encrypt is the basic encryption function. It takes parameters
  239. // block, an array of bytes representing a plaintext block, and expandedKey,
  240. // an array of words representing the expanded key previously returned by
  241. // keyExpansion(). The ciphertext block is returned as an array of bytes.
  242. function encrypt(block, expandedKey) {
  243.   var i;
  244.   if (!block || block.length*8 != blockSizeInBits)
  245.      return;
  246.   if (!expandedKey)
  247.      return;
  248.   block = packBytes(block);
  249.   addRoundKey(block, expandedKey);
  250.   for (i=1; i<Nr; i++)
  251.     Round(block, expandedKey.slice(Nb*i, Nb*(i+1)));
  252.   FinalRound(block, expandedKey.slice(Nb*Nr));
  253.   return unpackBytes(block);
  254. }
  255. // decrypt is the basic decryption function. It takes parameters
  256. // block, an array of bytes representing a ciphertext block, and expandedKey,
  257. // an array of words representing the expanded key previously returned by
  258. // keyExpansion(). The decrypted block is returned as an array of bytes.
  259. function decrypt(block, expandedKey) {
  260.   var i;
  261.   if (!block || block.length*8 != blockSizeInBits)
  262.      return;
  263.   if (!expandedKey)
  264.      return;
  265.   block = packBytes(block);
  266.   InverseFinalRound(block, expandedKey.slice(Nb*Nr));
  267.   for (i = Nr – 1; i>0; i--)
  268.     InverseRound(block, expandedKey.slice(Nb*i, Nb*(i+1)));
  269.   addRoundKey(block, expandedKey);
  270.   return unpackBytes(block);
  271. }
  272. // This method takes a byte array (byteArray) and converts it to a string by
  273. // applying String.fromCharCode() to each value and concatenating the result.
  274. // The resulting string is returned. Note that this function SKIPS zero bytes
  275. // under the assumption that they are padding added in formatPlaintext().
  276. // Obviously, do not invoke this method on raw data that can contain zero
  277. // bytes. It is really only appropriate for printable ASCII/Latin-1
  278. // values. Roll your own function for more robust functionality :)
  279. function byteArrayToString(byteArray) {
  280.   var result = "";
  281.   for(var i=0; i<byteArray.length; i++)
  282.     if (byteArray[i] != 0)
  283.       result += String.fromCharCode(byteArray[i]);
  284.   return result;
  285. }
  286. // This function takes an array of bytes (byteArray) and converts them
  287. // to a hexadecimal string. Array element 0 is found at the beginning of
  288. // the resulting string, high nibble first. Consecutive elements follow
  289. // similarly, for example [16, 255] --> "10ff". The function returns a
  290. // string.
  291. function byteArrayToHex(byteArray) {
  292.   var result = "";
  293.   if (!byteArray)
  294.     return;
  295.   for (var i=0; i<byteArray.length; i++)
  296.     result += ((byteArray[i]<16) ? "0" : "") + byteArray[i].toString(16);
  297.   return result;
  298. }
  299. // This function converts a string containing hexadecimal digits to an
  300. // array of bytes. The resulting byte array is filled in the order the
  301. // values occur in the string, for example "10FF" --> [16, 255]. This
  302. // function returns an array.
  303. function hexToByteArray(hexString) {
  304.   var byteArray = [];
  305.   if (hexString.length % 2)             // must have even length
  306.     return;
  307.   if (hexString.indexOf("0x") == 0 || hexString.indexOf("0X") == 0)
  308.     hexString = hexString.substring(2);
  309.   for (var i = 0; i<hexString.length; i += 2)
  310.     byteArray[Math.floor(i/2)] = parseInt(hexString.slice(i, i+2), 16);
  311.   return byteArray;
  312. }
  313. // This function packs an array of bytes into the four row form defined by
  314. // Rijndael. It assumes the length of the array of bytes is divisible by
  315. // four. Bytes are filled in according to the Rijndael spec (starting with
  316. // column 0, row 0 to 3). This function returns a 2d array.
  317. function packBytes(octets) {
  318.   var state = new Array();
  319.   if (!octets || octets.length % 4)
  320.     return;
  321.   state[0] = new Array();  state[1] = new Array();
  322.   state[2] = new Array();  state[3] = new Array();
  323.   for (var j=0; j<octets.length; j+= 4) {
  324.      state[0][j/4] = octets[j];
  325.      state[1][j/4] = octets[j+1];
  326.      state[2][j/4] = octets[j+2];
  327.      state[3][j/4] = octets[j+3];
  328.   }
  329.   return state;
  330. }
  331. // This function unpacks an array of bytes from the four row format preferred
  332. // by Rijndael into a single 1d array of bytes. It assumes the input "packed"
  333. // is a packed array. Bytes are filled in according to the Rijndael spec.
  334. // This function returns a 1d array of bytes.
  335. function unpackBytes(packed) {
  336.   var result = new Array();
  337.   for (var j=0; j<packed[0].length; j++) {
  338.     result[result.length] = packed[0][j];
  339.     result[result.length] = packed[1][j];
  340.     result[result.length] = packed[2][j];
  341.     result[result.length] = packed[3][j];
  342.   }
  343.   return result;
  344. }
  345. // This function takes a prospective plaintext (string or array of bytes)
  346. // and pads it with zero bytes if its length is not a multiple of the block
  347. // size. If plaintext is a string, it is converted to an array of bytes
  348. // in the process. The type checking can be made much nicer using the
  349. // instanceof operator, but this operator is not available until IE5.0 so I
  350. // chose to use the heuristic below.
  351. function formatPlaintext(plaintext) {
  352.   var bpb = blockSizeInBits / 8;               // bytes per block
  353.   var i;
  354.   // if primitive string or String instance
  355.   if (typeof plaintext == "string" || plaintext.indexOf) {
  356.     plaintext = plaintext.split("");
  357.     // Unicode issues here (ignoring high byte)
  358.     for (i=0; i<plaintext.length; i++)
  359.       plaintext[i] = plaintext[i].charCodeAt(0) & 0xFF;
  360.   }
  361.   for (i = bpb – (plaintext.length % bpb); i > 0 && i < bpb; i--)
  362.     plaintext[plaintext.length] = 0;
  363.   return plaintext;
  364. }
  365. // Returns an array containing "howMany" random bytes. YOU SHOULD CHANGE THIS
  366. // TO RETURN HIGHER QUALITY RANDOM BYTES IF YOU ARE USING THIS FOR A "REAL"
  367. // APPLICATION.
  368. function getRandomBytes(howMany) {
  369.   var i;
  370.   var bytes = new Array();
  371.   for (i=0; i<howMany; i++)
  372.     bytes[i] = Math.round(Math.random()*255);
  373.   return bytes;
  374. }
  375. // rijndaelEncrypt(plaintext, key, mode)
  376. // Encrypts the plaintext using the given key and in the given mode.
  377. // The parameter "plaintext" can either be a string or an array of bytes.
  378. // The parameter "key" must be an array of key bytes. If you have a hex
  379. // string representing the key, invoke hexToByteArray() on it to convert it
  380. // to an array of bytes. The third parameter "mode" is a string indicating
  381. // the encryption mode to use, either "ECB" or "CBC". If the parameter is
  382. // omitted, ECB is assumed.
  383. //
  384. // An array of bytes representing the cihpertext is returned. To convert
  385. // this array to hex, invoke byteArrayToHex() on it. If you are using this
  386. // "for real" it is a good idea to change the function getRandomBytes() to
  387. // something that returns truly random bits.
  388. function rijndaelEncrypt(plaintext, key, mode) {
  389.   var expandedKey, i, aBlock;
  390.   var bpb = blockSizeInBits / 8;          // bytes per block
  391.   var ct;                                 // ciphertext
  392.   if (!plaintext || !key)
  393.     return;
  394.   if (key.length*8 != keySizeInBits)
  395.     return;
  396.   if (mode == "CBC")
  397.     ct = getRandomBytes(bpb);             // get IV
  398.   else {
  399.     mode = "ECB";
  400.     ct = new Array();
  401.   }
  402.   // convert plaintext to byte array and pad with zeros if necessary.
  403.   plaintext = formatPlaintext(plaintext);
  404.   expandedKey = keyExpansion(key);
  405.   for (var block=0; block<plaintext.length / bpb; block++) {
  406.     aBlock = plaintext.slice(block*bpb, (block+1)*bpb);
  407.     if (mode == "CBC")
  408.       for (var i=0; i<bpb; i++)
  409.         aBlock[i] ^= ct[block*bpb + i];
  410.     ct = ct.concat(encrypt(aBlock, expandedKey));
  411.   }
  412.   return ct;
  413. }
  414. // rijndaelDecrypt(ciphertext, key, mode)
  415. // Decrypts the using the given key and mode. The parameter "ciphertext"
  416. // must be an array of bytes. The parameter "key" must be an array of key
  417. // bytes. If you have a hex string representing the ciphertext or key,
  418. // invoke hexToByteArray() on it to convert it to an array of bytes. The
  419. // parameter "mode" is a string, either "CBC" or "ECB".
  420. //
  421. // An array of bytes representing the plaintext is returned. To convert
  422. // this array to a hex string, invoke byteArrayToHex() on it. To convert it
  423. // to a string of characters, you can use byteArrayToString().
  424. function rijndaelDecrypt(ciphertext, key, mode) {
  425.   var expandedKey;
  426.   var bpb = blockSizeInBits / 8;          // bytes per block
  427.   var pt = new Array();                   // plaintext array
  428.   var aBlock;                             // a decrypted block
  429.   var block;                              // current block number
  430.   if (!ciphertext || !key || typeof ciphertext == "string")
  431.     return;
  432.   if (key.length*8 != keySizeInBits)
  433.     return;
  434.   if (!mode)
  435.     mode = "ECB";                         // assume ECB if mode omitted
  436.   expandedKey = keyExpansion(key);
  437.   // work backwards to accomodate CBC mode
  438.   for (block=(ciphertext.length / bpb)-1; block>0; block--) {
  439.     aBlock =
  440.      decrypt(ciphertext.slice(block*bpb,(block+1)*bpb), expandedKey);
  441.     if (mode == "CBC")
  442.       for (var i=0; i<bpb; i++)
  443.         pt[(block-1)*bpb + i] = aBlock[i] ^ ciphertext[(block-1)*bpb + i];
  444.     else
  445.       pt = aBlock.concat(pt);
  446.   }
  447.   // do last block if ECB (skips the IV in CBC)
  448.   if (mode == "ECB")
  449.     pt = decrypt(ciphertext.slice(0, bpb), expandedKey).concat(pt);
  450.   return pt;
  451. }

from:http://www.coolcode.org/?action=show&id=94