[英]Progressive AES decryption with CryptoJS
I try to use progressive ciphering from CryptoJS to decrypt files. 我尝试使用CryptoJS的渐进式加密来解密文件。 The files are encrypted & base64 encoded with OpenSSL as shown in the CryptoJS documentation ( CryptoJS ). 这些文件使用OpenSSL进行加密和base64编码,如CryptoJS文档( CryptoJS )所示。
When I use CryptoJS.AES.decrypt everything works fine: 当我使用CryptoJS.AES.decrypt一切正常时:
var encryptedText = "U2FsdGVkX19X2wD+xFnLd3WLuzW5qA0dppGtV+VPOFdjslLsZpwfdqd02BOe4pvxG2zZok06DchVfZBBBS/JWg==";
var key = CryptoJS.enc.Hex.parse("A6420198998C341308AF100CF7CCAC95884E4084581A4F8CFB8DFA7FEAD045EF");
var iv = CryptoJS.enc.Hex.parse("7F418B4532F8BC83261639DBA60C0A50");
var decrypted = CryptoJS.AES.decrypt(encryptedText, key, {iv: iv});
var base64 = CryptoJS.enc.Base64.stringify(decrypted);
var result = atob(base64);
But when I try progressive decryption the result is corrupted: 但是当我尝试渐进式解密时,结果已损坏:
var decrypted;
var cipher = CryptoJS.algo.AES.createDecryptor(key, { iv: iv});
decrypted = cipher.process(encryptedText);
decrypted.concat(cipher.finalize());
var base64 = CryptoJS.enc.Base64.stringify(decrypted);
var result = atob(base64);
Has anyone an idea what I am doing wrong? 有谁知道我做错了什么?
The problem is that encryptedText
is a string which is OpenSSL formatted. 问题是encryptedText
是一个OpenSSL格式的字符串。 It is not the pure ciphertext which you get from the ciphertext
property after encryption: 它不是加密后从ciphertext
属性中获得的纯密ciphertext
:
var ct = CryptoJS.AES.encrypt(plaintext, key, {iv: iv}).ciphertext;
The Cipher.process
function only accepts a WordArray
or the data string and encryptedText
is neither. Cipher.process
函数只接受WordArray
或数据字符串,而encryptedText
也不接受。 You either need to use CryptoJS.format.OpenSSL.parse()
to retrieve the ciphertext WordArray
: 您需要使用CryptoJS.format.OpenSSL.parse()
来检索密文WordArray
:
var ct = CryptoJS.format.OpenSSL.parse(encryptedText).ciphertext;
or directly retrieve ciphertext
when you encrypt it like in the first snippet. 或者在加密时直接检索ciphertext
,就像在第一个片段中一样。
After that, you can do: 之后,您可以:
var decrypted = cipher.process(ct);
decrypted = decrypted.concat(cipher.finalize());
I'm also not sure why you stringify as Base64 and then use atob
, because there is an easier way to get the original string back: 我也不确定为什么你将字符串化为Base64,然后使用atob
,因为有一种更简单的方法来获取原始字符串:
console.log(decrypted.toString(CryptoJS.enc.Utf8));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.