AI销管
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

116 rivejä
2.3 KiB

  1. ;(function (root, factory, undef) {
  2. if (typeof exports === "object") {
  3. // CommonJS
  4. module.exports = exports = factory(require("./core"), require("./cipher-core"));
  5. }
  6. else if (typeof define === "function" && define.amd) {
  7. // AMD
  8. define(["./core", "./cipher-core"], factory);
  9. }
  10. else {
  11. // Global (browser)
  12. factory(root.CryptoJS);
  13. }
  14. }(this, function (CryptoJS) {
  15. /** @preserve
  16. * Counter block mode compatible with Dr Brian Gladman fileenc.c
  17. * derived from CryptoJS.mode.CTR
  18. * Jan Hruby jhruby.web@gmail.com
  19. */
  20. CryptoJS.mode.CTRGladman = (function () {
  21. var CTRGladman = CryptoJS.lib.BlockCipherMode.extend();
  22. function incWord(word)
  23. {
  24. if (((word >> 24) & 0xff) === 0xff) { //overflow
  25. var b1 = (word >> 16)&0xff;
  26. var b2 = (word >> 8)&0xff;
  27. var b3 = word & 0xff;
  28. if (b1 === 0xff) // overflow b1
  29. {
  30. b1 = 0;
  31. if (b2 === 0xff)
  32. {
  33. b2 = 0;
  34. if (b3 === 0xff)
  35. {
  36. b3 = 0;
  37. }
  38. else
  39. {
  40. ++b3;
  41. }
  42. }
  43. else
  44. {
  45. ++b2;
  46. }
  47. }
  48. else
  49. {
  50. ++b1;
  51. }
  52. word = 0;
  53. word += (b1 << 16);
  54. word += (b2 << 8);
  55. word += b3;
  56. }
  57. else
  58. {
  59. word += (0x01 << 24);
  60. }
  61. return word;
  62. }
  63. function incCounter(counter)
  64. {
  65. if ((counter[0] = incWord(counter[0])) === 0)
  66. {
  67. // encr_data in fileenc.c from Dr Brian Gladman's counts only with DWORD j < 8
  68. counter[1] = incWord(counter[1]);
  69. }
  70. return counter;
  71. }
  72. var Encryptor = CTRGladman.Encryptor = CTRGladman.extend({
  73. processBlock: function (words, offset) {
  74. // Shortcuts
  75. var cipher = this._cipher
  76. var blockSize = cipher.blockSize;
  77. var iv = this._iv;
  78. var counter = this._counter;
  79. // Generate keystream
  80. if (iv) {
  81. counter = this._counter = iv.slice(0);
  82. // Remove IV for subsequent blocks
  83. this._iv = undefined;
  84. }
  85. incCounter(counter);
  86. var keystream = counter.slice(0);
  87. cipher.encryptBlock(keystream, 0);
  88. // Encrypt
  89. for (var i = 0; i < blockSize; i++) {
  90. words[offset + i] ^= keystream[i];
  91. }
  92. }
  93. });
  94. CTRGladman.Decryptor = Encryptor;
  95. return CTRGladman;
  96. }());
  97. return CryptoJS.mode.CTRGladman;
  98. }));