Cipher a string using crypto-js with hex encoding to make it url friendly

前端 未结 1 390
深忆病人
深忆病人 2021-02-02 03:45

I am using crypto-js by brix. I have this function below that handles the encryption of a plain text.

import CryptoJS from \'crypto-js\'
import AES          


        
相关标签:
1条回答
  • 2021-02-02 04:19

    You would likely want to do:

    export function dec(cipherText){
       var bytes = CryptoJS.AES.decrypt(cipherText, SECRET);
       var hex = bytes.toString(CryptoJS.enc.Hex);
       var plain = bytes.toString(CryptoJS.enc.Utf8);
       return [hex, plain];
    }
    

    This takes the encrypted base64 string and will return the decrypted plaintext and hexadecimal.

    EDIT: In regards to your comment and edited question:

    const SECRET = 'I am batman'
    
    function enc(plainText){
        var b64 = CryptoJS.AES.encrypt(plainText, SECRET).toString();
        var e64 = CryptoJS.enc.Base64.parse(b64);
        var eHex = e64.toString(CryptoJS.enc.Hex);
        return eHex;
    }
    
    function dec(cipherText){
       var reb64 = CryptoJS.enc.Hex.parse(cipherText);
       var bytes = reb64.toString(CryptoJS.enc.Base64);
       var decrypt = CryptoJS.AES.decrypt(bytes, SECRET);
       var plain = decrypt.toString(CryptoJS.enc.Utf8);
       return plain;
    }
    

    The end result takes the base64 string, makes it hexadecimal and returns the decrypted string.

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