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
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>
Then you can simply do:
var wordArray = CryptoJS.lib.WordArray.create(arrayBuffer);
/* perform decryption of `wordArray` */
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
Edit:
For a more efficient conversion (no intermediate Base64String
necessary) check out Aletheios answer to that question (the function wordToByteArray(wordArray)
and then do .buffer
).
you can use the web crypto API to directly decrypt arrayBuffer. No need to convert binary data to a string, which is so inefficient, and it could cause memory and CPU rises.