How to decrypt an ArrayBuffer?

前端 未结 2 1007
清歌不尽
清歌不尽 2020-12-13 16:20

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

相关标签:
2条回答
  • 2020-12-13 16:51

    The conversion of ArrayBuffer -> WordArray has been discussed in CryptoJS's issue 46. For that reason a TypedWordArraywhere 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).

    0 讨论(0)
  • 2020-12-13 17:09

    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.

    0 讨论(0)
提交回复
热议问题