How to decrypt an ArrayBuffer?

前端 未结 2 1010
清歌不尽
清歌不尽 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:

    
    

    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).

提交回复
热议问题