문제

Even though I'm sure I have the right data (verified through an online decoder), I'm only getting empty strings as my output from the decoder.

This is my javascript:

var cipher  = CryptoJS.enc.Base64.parse(data.split("--")[0]);
var inv     = CryptoJS.enc.Base64.parse(data.split("--")[1]);

console.log("Ciphertext");
console.log(cipher);                             // as word array
console.log(CryptoJS.enc.Hex.stringify(cipher)); // as hex string
console.log("IV");
console.log(inv);                                // as word array
console.log(CryptoJS.enc.Hex.stringify(inv));    // as hex string

// don't worry, this key won't be used in production ;-)
var key = CryptoJS.enc.Utf8.parse("GzUaLsk7SI9VDja3");
var decrypted = CryptoJS.AES.decrypt(cipher, key, { iv: inv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });

console.log(decrypted);

decrypted = decrypted.toString(CryptoJS.enc.Utf8);
console.log(decrypted);

data is a string with the ciphertext and IV, both Base64 encoded, separated by 2 dashes ('--').

I pasted the hex representation of the ciphertext and IV into this tool to verify whether my data is right, and that gave me the desired result.

Can someone please help me with why I'm getting empty strings (and empty word arrays, for that matter) out of the decrypt function? I do not get any errors with this code at all, by the way.

도움이 되었습니까?

해결책

As I expected, the problem wasn't the padding. Here is the working code:

var cipher = CryptoJS.enc.Base64.parse(data.split("--")[0]);
var inv    = CryptoJS.enc.Base64.parse(data.split("--")[1]);

var key = CryptoJS.enc.Utf8.parse("GzUaLsk7SI9VDja3");
var aesDecryptor = CryptoJS.algo.AES.createDecryptor(key, { iv: inv });

var decrypted = aesDecryptor.process(cipher);
var plaintext = decrypted.toString(CryptoJS.enc.Utf8);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top