mode-cfb.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. CryptoJS v3.1.2
  3. code.google.com/p/crypto-js
  4. (c) 2009-2013 by Jeff Mott. All rights reserved.
  5. code.google.com/p/crypto-js/wiki/License
  6. */
  7. /**
  8. * Cipher Feedback block mode.
  9. */
  10. CryptoJS.mode.CFB = (function () {
  11. var CFB = CryptoJS.lib.BlockCipherMode.extend();
  12. CFB.Encryptor = CFB.extend({
  13. processBlock: function (words, offset) {
  14. // Shortcuts
  15. var cipher = this._cipher;
  16. var blockSize = cipher.blockSize;
  17. generateKeystreamAndEncrypt.call(this, words, offset, blockSize, cipher);
  18. // Remember this block to use with next block
  19. this._prevBlock = words.slice(offset, offset + blockSize);
  20. }
  21. });
  22. CFB.Decryptor = CFB.extend({
  23. processBlock: function (words, offset) {
  24. // Shortcuts
  25. var cipher = this._cipher;
  26. var blockSize = cipher.blockSize;
  27. // Remember this block to use with next block
  28. var thisBlock = words.slice(offset, offset + blockSize);
  29. generateKeystreamAndEncrypt.call(this, words, offset, blockSize, cipher);
  30. // This block becomes the previous block
  31. this._prevBlock = thisBlock;
  32. }
  33. });
  34. function generateKeystreamAndEncrypt(words, offset, blockSize, cipher) {
  35. // Shortcut
  36. var iv = this._iv;
  37. // Generate keystream
  38. if (iv) {
  39. var keystream = iv.slice(0);
  40. // Remove IV for subsequent blocks
  41. this._iv = undefined;
  42. } else {
  43. var keystream = this._prevBlock;
  44. }
  45. cipher.encryptBlock(keystream, 0);
  46. // Encrypt
  47. for (var i = 0; i < blockSize; i++) {
  48. words[offset + i] ^= keystream[i];
  49. }
  50. }
  51. return CFB;
  52. }());