I've been trying to decrypt an ArrayBuffer object using CryptoJS, but so far it always returns a blank WordArray. The files (images) are encrypted in an iOS and Android app, sent to a server, and downloaded in this web app to be decrypted and displayed. The iOS and Android apps are able to decrypt the files without problems, so there's nothing wrong with the encryption process.
The files are downloaded with an XMLHttpRequest
with responseType
set to arraybuffer
. Here's my code so far:
// Decrypt a Base64 encrypted string (this works perfectly)
String.prototype.aesDecrypt = function(key) {
var nkey = CryptoJS.enc.Hex.parse(key.sha256());
return CryptoJS.AES.decrypt(this.toString(), nkey, {
iv: CryptoJS.enc.Hex.parse('00000000000000000000000000000000'),
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
}).toString(CryptoJS.enc.Utf8);
}
// Decrypt a plain encrypted ArrayBuffer (this is the problem, it always outputs an empty WordArray)
ArrayBuffer.prototype.aesDecrypt = function(key) {
// Get key
if (!key) return null;
var nkey = CryptoJS.enc.Hex.parse(key.sha256());
// Get input (if I pass the ArrayBuffer directly to the create function, it returns
// a WordList with sigBytes set to NaN)
//var input = CryptoJS.lib.WordArray.create(this);
var input = CryptoJS.lib.WordArray.create(new Uint8Array(this));
// Decrypt
var output = CryptoJS.AES.decrypt(input, nkey, {
iv: CryptoJS.enc.Hex.parse('00000000000000000000000000000000'),
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
// Output is an empty WordList
console.log("Output: ", output);
}
Another question I have is how do you convert a WordArray
to an ArrayBuffer
?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…