如何解密ArrayBuffer? [英] How to decrypt an ArrayBuffer?
问题描述
我一直在使用CryptoJS解密ArrayBuffer对象,但到目前为止它总是返回一个空白的WordArray。文件(图像)在iOS和Android应用程序中加密,发送到服务器,并在此Web应用程序中下载以进行解密和显示。 iOS和Android应用程序能够解密文件没有任何问题,所以加密过程没有任何问题。
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.
这些文件以 XMLHttpRequest
与 responseType
设置为 arraybuffer
。这是我的代码到目前为止:
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);
}
另一个问题是如何转换code> WordArray 到 ArrayBuffer
?
Another question I have is how do you convert a WordArray
to an ArrayBuffer
?
推荐答案
转换 ArrayBuffer - >已经在CryptoJS的 =nofollow noreferrer> issue 46 。因此,添加了
TypedWordArray
,您也可以在其中传递 ArrayBuffer
。
The conversion of ArrayBuffer -> WordArray
has been discussed in CryptoJS's issue 46. For that reason a TypedWordArray
where you can also pass an ArrayBuffer
has been added.
为了使用,另外还包括以下脚本:
To use that additionally include the following script:
<script src="http://crypto-js.googlecode.com/svn/tags/3.1/build/components/lib-typedarrays.js"></script>
然后你可以简单地做:
var wordArray = CryptoJS.lib.WordArray.create(arrayBuffer);
/* perform decryption of `wordArray` */
要将生成的 decryptptedWordArray
重新转换为 ArrayBuffer
,最简单的方法可能是,首先将其转换为 Base64-String
(正如讨论这里),然后将该字符串解码为所需的 ArrayBuffer
(请参阅 here )。整个过程将如下所示:
To reconvert the resulting decryptedWordArray
to an ArrayBuffer
, the simplest approach would probably be, to first convert it to a Base64-String
(as discussed here) and then decode that String to the desired ArrayBuffer
(see here). The whole procedure would look something like this:
dcWordArray = ... // your decrypted WordArray
dcBase64String = dcWordArray.toString(CryptoJS.enc.Base64); // to Base64-String
dcArrayBuffer = base64DecToArr(dcBase64String).buffer; // to ArrayBuffer
编辑:
为了更有效的转换(不需要中间 Base64String
必需)请查看 hlet =https: //stackoverflow.com/questions/11889329/word-array-to-string\">这个问题(函数 wordToByteArray(wordArray)
然后执行 .buffer
)。
For a more efficient conversion (no intermediate Base64String
necessary) check out Aletheios answer to that question (the function wordToByteArray(wordArray)
and then do .buffer
).
这篇关于如何解密ArrayBuffer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!