如何解决为什么React js Web应用程序中的加密不起作用?
我一直在使用带有打字稿的React Web应用程序进行工作,并尝试向其添加AES CBC模式加密,但是代码未解密加密的文本。奇怪的是,解密代码在测试html文件中工作正常,但在我的webApp中却无法正常工作。我正在使用crypto js version4作为工具库,我的代码是:
import IMiddleware from "./IMiddleware";
import { aesjs } from "aes-js";
import CryptoJS from "crypto-js";
const crypto = window.CryptoJS;
const key = '0123456701234567';
const iv = '0000000000000000';
export class decryption implements IMiddleware {
static instance: decryption;
constructor() {
if (!decryption.instance) {
decryption.instance = this;
}
return decryption.instance;
}
Invoke = (input) => {
console.log('encryption middleware before execution: ',input)
let decipher = crypto.AES.decrypt(input,crypto.enc.Utf8.parse(key),{
iv: crypto.enc.Utf8.parse(iv),// parse the IV
padding: crypto.pad.Pkcs7,mode: crypto.mode.CBC
})
let result = decipher.toString(crypto.enc.Utf8);
console.log('encryption middleware after execution: ',result)
return result;
}
}
但是控制台正在获得以下结果:
encryption middleware before execution: "B++uNPNFrjamQLD02WlhhKjf9r+R/pxttbQ9btltYyGZleE8a6+nuuvu6kQRxAs29KRbjq8CCr93Yr3WtsSK4K0Bpd3vOjqVEuvijuJegnuzvgZmUo7vUtXRir6iJ7fHLnRfbhcsVBWZ6xZZwGNzHan62uv58wx40lK1Bx7oGW+qGjcqJolDmM6ABiZGtLWiQEYI3sZ+iwXSe6nAfuUyvCzt6WCx0BqxUj/9tn6pGFVMBE6JWPA2KcosGMchE3FFIS/KiJHuOoH4E63EyUI8Y+dmwjL0yEHegexEugRgsKT5bw7Z+h8uZAYO9LQ/qfs2O2f+tl/hP3OdiznYKpVyRpaL0/i3HIma81MygyZdaffEGsqBSu5jKV710vMh260jzH8N0nTk/OG5cxNk8Rll2k9T00L6EWmG+ypx2LOVOSHeM3yIm3/oOT2dc1fxnCUeftmrVO4BhWH1kr8LCRjU5w=="
crypto-js.min.js:1 Uncaught Error: Malformed UTF-8 data
at Object.stringify (crypto-js.min.js:1)
at t.init.toString (crypto-js.min.js:1)
at decryption.Invoke (encryption.ts:67)
at WebSocket.socketWithManager.onMessageHandler (socket.ts:112)
感谢任何帮助
解决方法
欢迎使用Stackoverflow。抱歉,该错误在我的在线编译器中无法重现:
encryption middleware after execution: {"From": {"Type": "S","Token": "2222","Rule": 0},"To": {"Type": "Ws","Token": "db0f0c4348","Rule": 1},"Time": "2020-09-03 16:51:36","Info": {"Type": "Response","Action": "Get","Command": 5,"Data": {"UserID": "7b51868bdcfc5144cb55408a3fa66d","Rule": "1","Name": "\u0631\u0636\u0627 \u0628\u0647\u062c\u0627\u0646\u06cc","Version": "1"}}}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。