123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- /*!
- * Crypto-JS v1.1.0
- * http://code.google.com/p/crypto-js/
- * Copyright (c) 2009, Jeff Mott. All rights reserved.
- * http://code.google.com/p/crypto-js/wiki/License
- */
- (function(){
- // Shortcut
- var util = Crypto.util;
- var MARC4 = Crypto.MARC4 = {
- /**
- * Public API
- */
- encrypt: function (message, password) {
- var
- // Convert to bytes
- m = util.stringToBytes(message),
- // Generate random IV
- iv = util.randomBytes(16),
- // Generate key
- k = Crypto.PBKDF2(password, util.bytesToString(iv), 32, { asBytes: true });
- // Encrypt
- MARC4._marc4(m, k, 1536);
- // Return ciphertext
- return util.bytesToBase64(iv.concat(m));
- },
- decrypt: function (ciphertext, password) {
- var
- // Convert to bytes
- c = util.base64ToBytes(ciphertext),
- // Separate IV and message
- iv = c.splice(0, 16),
- // Generate key
- k = Crypto.PBKDF2(password, util.bytesToString(iv), 32, { asBytes: true });
- // Decrypt
- MARC4._marc4(c, k, 1536);
- // Return plaintext
- return util.bytesToString(c);
- },
- /**
- * Internal methods
- */
- // The core
- _marc4: function (m, k, drop) {
- // State variables
- var i, j, s, temp;
- // Key setup
- for (i = 0, s = []; i < 256; i++) s[i] = i;
- for (i = 0, j = 0; i < 256; i++) {
- j = (j + s[i] + k[i % k.length]) % 256;
- // Swap
- temp = s[i];
- s[i] = s[j];
- s[j] = temp;
- }
- // Clear counters
- i = j = 0;
- // Encryption
- for (var k = 0 - drop; k < m.length; k++) {
- i = (i + 1) % 256;
- j = (j + s[i]) % 256;
- // Swap
- temp = s[i];
- s[i] = s[j];
- s[j] = temp;
- // Stop here if we're still dropping keystream
- if (k < 0) continue;
- // Encrypt
- m[k] ^= s[(s[i] + s[j]) % 256];
- }
- }
- };
- })();
|