【问题标题】:Javascript encryption in Crypto decryption in CryptoJSCryptoJS中的加密解密中的Javascript加密
【发布时间】:2016-12-02 07:16:58
【问题描述】:

我正在尝试加密服务器端(加密节点)和解密客户端(CryptoJS)。我可以使用 cryptoJS 创建密钥,并且可以在使用同一个单独的库时进行加密和解密,但是问题是我不能使用 Crypto 加密,而是使用 CryptoJS 解密,这是现实世界的场景。没有错误,只是一个空响应。

任何帮助都非常感谢!

iv = crypto.randomBytes(16),
orig = 'A confidential message.';
//Crypto JS creates key
var password = "sixteen byte key";
var salt = CryptoJS.lib.WordArray.random(128/8);
var key = CryptoJS.PBKDF2(password, salt, { keySize: 128 / 32, iterations: 1000 });
console.log("step1 generated key: "+ key);

//Convert key for crypto use - as a Buffer
var hashHex = key.toString(CryptoJS.enc.Hex);
var hash = new Buffer(hashHex,'hex');

//Test encryption and decryption with crypto (Node)
//use CryptoJS key to encrypt data using crypto cipheriv
var cipher2 = crypto.createCipheriv('aes-128-cbc', hash, iv); //iv must be a buffer
var encrypted1 = cipher2.update(orig, 'utf8', 'hex');
var encrypted2 = encrypted1 += cipher2.final('hex');
console.log("Crypto string:", encrypted2.toString());

// Start decrypt
var decipher = crypto.createDecipheriv('aes-128-cbc', hash, iv);
var dec = decipher.update(encrypted2, 'hex', 'utf8')
dec += decipher.final('utf8');
console.log("Crypto decrypted msg:", dec);

//test with crypto JS (ie the client)
//CryptoJS key is a string
var encryptedCJS = CryptoJS.AES.encrypt(orig, key.toString(), { iv: iv, mode: CryptoJS.mode.CBC});
console.log("CryptoJS encrypted: "+encryptedCJS);

var decryptedCryptoJS = CryptoJS.AES.decrypt(encryptedCJS, key.toString(), { mode: CryptoJS.mode.CBC, iv: iv });
console.log("CryptoJS decrypted msg: "+decryptedCryptoJS.toString(CryptoJS.enc.Utf8));

//This part does not work - use message encrypted by crypto but cannot decrypt with CryptoJS. decryptedCryptoJSFinal is empty
var decryptedCryptoJSFinal = CryptoJS.AES.decrypt(encrypted2, key.toString(), {iv: iv, mode: CryptoJS.mode.CBC});
console.log("FINAL CryptoJS decrypted: "+decryptedCryptoJSFinal.toString(CryptoJS.enc.Utf8));

我认为加密加密的输出必须与 CryptoJS 加密的输出格式不同,但我找不到问题所在。 总的来说,我打算将加密数据作为 JSON 发送,以便 CryptoJS 在客户端进行解密。

【问题讨论】:

  • FWIW 如果你使用 webpack/browserify/etc,你可以使用相同的节点 crypto API。
  • @mscdex 好点谢谢。 FWIW 我想使用一个不同的库,因为服务器真的可以是 Java、.Net 等。顺便说一句,我想我已经破解了它,CryptoJS 需要一个“字数组”来解密,所以我认为问题就在那里。仍在证明解决方案。

标签: javascript node.js encryption cryptography cryptojs


【解决方案1】:

我认为您的问题出在客户端,如果您将“密钥”和“iv”作为字符串传递给“CryptoJS.AES.encrypt”,那么 CryptoJS 会获取您的“密钥”和随机的“盐”并生成密码的不同密钥。您可以验证它使用相同的密钥和 iv 从相同的明文生成不同的密文,它们总是不同的,因为每次运行该函数时 CryptoJS 内部都会生成不同的密钥。

为避免这种情况,您需要传递 'key' 和 'iv' 编码(在 'hex' 或 'base64' 取决于您使用的代码),然后 CryptoJS 解释它不必生成密钥并用你的“密钥”来加密。

检查这个例子:

//BACKEND with node crypto aes-256-cbc->  generate key and ciphertext
/////////////////////////////////////////////////////////////////////
var crypto = require('crypto');
var algorithm = 'aes-256-cbc';
var inputEncoding = 'utf8';
var outputEncoding = 'base64';
var pt = 'HELLO';

//generate key and iv
var masterKey = "253D3FB468A0E24677C28A624BE0F939";
var salt  = "0000000000000000";
var keySize = 256/8;
var ivSize = 128/8;
var iterations = 100;
var outputKey = crypto.pbkdf2Sync(masterKey, salt, iterations, keySize+ivSize, "sha1");

// obtain key and IV  splitting outputKey
var buffer = new Buffer(outputKey, inputEncoding);
var secretKey = buffer.slice(0, keySize);
var iv = buffer.slice(keySize, (keySize+ivSize)); 

console.log('secretKey->',secretKey.toString('base64'));
console.log('iv->',iv.toString('base64'));       

//encrypt
var encrypt = crypto.createCipheriv(algorithm, secretKey, iv);
var encrypted = encrypt.update(pt, inputEncoding, outputEncoding);
encrypted += encrypt.final(outputEncoding);
console.log('Ciphering "%s"', pt);
//We obtain a 
console.log('CipherText base64' string "%s ', encrypted.toString());



//FRONTEND with node CryptoJS aes-256-cbc->  generate same key and obtain cleartext
////////////////////////////////////////////////////////////////////
var masterKey = "253D3FB468A0E24677C28A624BE0F939";
var salt ="0000000000000000";
var iterations = 100; 
var keySize = 256;
var ivSize = 128;
var outputKey = CryptoJS.PBKDF2(masterKey, salt, {
  keySize: (keySize+ivSize)/32,
  iterations: iterations
});
// the underlying words arrays might have more content than was asked: remove insignificant words
outputKey.clamp();

// split key and IV
var secretKey = CryptoJS.lib.WordArray.create(outputKey.words.slice(0, 
keySize/32));
var iv = CryptoJS.lib.WordArray.create(outputKey.words.slice(keySize/32));

console.log('secretKey->', secretKey.toString(CryptoJS.enc.Base64));
console.log('iv->', iv.toString(CryptoJS.enc.Base64));

var decrypted = CryptoJS.AES.decrypt(ct, secretKey,{iv: iv});//Default mode CBC { mode: CryptoJS.mode.CFB });
console.log('CipherText->', ct);
console.log('ClearText decrypted', decrypted.toString(CryptoJS.enc.Utf8));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-01
    • 2017-01-23
    • 2017-05-04
    • 1970-01-01
    相关资源
    最近更新 更多