rc4.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. } else if (typeof define === "function" && define.amd) {
  6. // AMD
  7. define(["./core", "./enc-base64", "./md5", "./evpkdf", "./cipher-core"], factory);
  8. } else {
  9. // Global (browser)
  10. factory(root.CryptoJS);
  11. }
  12. }(this, function (CryptoJS) {
  13. (function () {
  14. // Shortcuts
  15. var C = CryptoJS;
  16. var C_lib = C.lib;
  17. var StreamCipher = C_lib.StreamCipher;
  18. var C_algo = C.algo;
  19. /**
  20. * RC4 stream cipher algorithm.
  21. */
  22. var RC4 = C_algo.RC4 = StreamCipher.extend({
  23. _doReset: function () {
  24. // Shortcuts
  25. var key = this._key;
  26. var keyWords = key.words;
  27. var keySigBytes = key.sigBytes;
  28. // Init sbox
  29. var S = this._S = [];
  30. for (var i = 0; i < 256; i++) {
  31. S[i] = i;
  32. }
  33. // Key setup
  34. for (var i = 0, j = 0; i < 256; i++) {
  35. var keyByteIndex = i % keySigBytes;
  36. var keyByte = (keyWords[keyByteIndex >>> 2] >>> (24 - (keyByteIndex % 4) * 8)) & 0xff;
  37. j = (j + S[i] + keyByte) % 256;
  38. // Swap
  39. var t = S[i];
  40. S[i] = S[j];
  41. S[j] = t;
  42. }
  43. // Counters
  44. this._i = this._j = 0;
  45. },
  46. _doProcessBlock: function (M, offset) {
  47. M[offset] ^= generateKeystreamWord.call(this);
  48. },
  49. keySize: 256 / 32,
  50. ivSize: 0
  51. });
  52. function generateKeystreamWord() {
  53. // Shortcuts
  54. var S = this._S;
  55. var i = this._i;
  56. var j = this._j;
  57. // Generate keystream word
  58. var keystreamWord = 0;
  59. for (var n = 0; n < 4; n++) {
  60. i = (i + 1) % 256;
  61. j = (j + S[i]) % 256;
  62. // Swap
  63. var t = S[i];
  64. S[i] = S[j];
  65. S[j] = t;
  66. keystreamWord |= S[(S[i] + S[j]) % 256] << (24 - n * 8);
  67. }
  68. // Update counters
  69. this._i = i;
  70. this._j = j;
  71. return keystreamWord;
  72. }
  73. /**
  74. * Shortcut functions to the cipher's object interface.
  75. *
  76. * @example
  77. *
  78. * var ciphertext = CryptoJS.RC4.encrypt(message, key, cfg);
  79. * var plaintext = CryptoJS.RC4.decrypt(ciphertext, key, cfg);
  80. */
  81. C.RC4 = StreamCipher._createHelper(RC4);
  82. /**
  83. * Modified RC4 stream cipher algorithm.
  84. */
  85. var RC4Drop = C_algo.RC4Drop = RC4.extend({
  86. /**
  87. * Configuration options.
  88. *
  89. * @property {number} drop The number of keystream words to drop. Default 192
  90. */
  91. cfg: RC4.cfg.extend({
  92. drop: 192
  93. }),
  94. _doReset: function () {
  95. RC4._doReset.call(this);
  96. // Drop
  97. for (var i = this.cfg.drop; i > 0; i--) {
  98. generateKeystreamWord.call(this);
  99. }
  100. }
  101. });
  102. /**
  103. * Shortcut functions to the cipher's object interface.
  104. *
  105. * @example
  106. *
  107. * var ciphertext = CryptoJS.RC4Drop.encrypt(message, key, cfg);
  108. * var plaintext = CryptoJS.RC4Drop.decrypt(ciphertext, key, cfg);
  109. */
  110. C.RC4Drop = StreamCipher._createHelper(RC4Drop);
  111. }());
  112. return CryptoJS.RC4;
  113. }));