AI销管
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 

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