【问题标题】:How can i use cryptojs in Native JS?我如何在 Native JS 中使用 cryptojs?
【发布时间】:2019-11-07 00:21:38
【问题描述】:

我在 HTML 中的 NativeJS 中使用 cryptoJs。但是当我用 aesDecrypt(decrypted,key) 对 txt 进行解密时,它就不起作用了。我已经尝试了许多其他密码,但它仍然不起作用。你们会查看下面的代码并给我一些建议吗?

我在后端尝试了许多密码(如 aes-192-ecb、aes-256-ecb),但仍然无法正常工作。它报告:错误加密错误。

Front code: encrypt the txt and send to backend.
function aesEncrypt(data, key) {    
    key = CryptoJS.enc.Utf8.parse(key);    
    let encrypted = CryptoJS.AES.encrypt(data, key, {    
        mode: CryptoJS.mode.ECB,    
        padding: CryptoJS.pad.Pkcs7    
    });    
    return encrypted.toString();    
}   

Backend code: decrypt the txt sent from front.
function aesDecrypt(encrypted, key) {    
    const decipher = crypto.createDecipher('aes192', key);    
    let decrypted = decipher.update(encrypted, 'binary', 'utf8');    
    decrypted += decipher.final('utf8');    
    return decrypted;    
}

【问题讨论】:

    标签: cryptojs


    【解决方案1】:

    看来我在前端和后端都使用了错误的方法。

    front code:
    <script src="https://cdn.jsdelivr.net/npm/crypto-js@3.1.9-1/crypto-js.js"></script>
    let encrypted = CryptoJS.AES.encrypt("txt", "Secret Passphrase").toString();
    
    
    backend code:
    const cryptojs = require("crypto-js");
    let txt = cryptojs.AES.decrypt(encrypted , "Secret Passphrase").toString(cryptojs.enc.Utf8);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-11
      • 2019-05-26
      • 2018-12-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多