【发布时间】:2021-09-01 23:13:36
【问题描述】:
加密有效,但解密根本无效,我无法发现我的错误
在 javascript 中我有四个函数:
前两个加密和解密文本有或没有密钥,前两个函数可能没有错
在第三个和第四个函数中,我从 html 页面获取输入并将它们存储在变量中,我正在加密和解密它们
function encrypt(message = '', key = '') { //This function will take message and key for encryption
var x = CryptoJS.AES.encrypt(message, key);
return x.toString();
}
function decrypt(message = '', key = '') { //This function will take message and key for decryption
var y = CryptoJS.AES.decrypt(message, key);
var decryptedMessage = decry.toString(CryptoJS.enc.Utf8);
return decryptedMessage;
}
function AesEncrypt() {
const text = document.getElementById('inputText').value;
const password = document.getElementById('inputPassword').value;
var x = encrypt(text, password);
document.getElementById("demo1").innerHTML = x;
}
function AesDecrypt() {
const text1 = document.getElementById('inputText').value;
const password2 = document.getElementById('inputPassword').value;
var x1 = decrypt(text1, password2);
document.getElementById("demo2").innerHTML = x1;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/aes.js"></script>
<input type="text" id="inputText" placeholder="Enter Plain text or Text to Decrypt">
<input type="text" style="width: 100%;" id="inputText" placeholder="Enter Plain text or Text to Decrypt">
<input type="text" id="inputPassword" placeholder="Enter a Key">
<button type="button" onclick="AesEncrypt()">Encrypt</button>
<button type="button" onclick="AesDecrypt()">Decrypt</button>
<p id="demo1"> </p>
<p id="demo2"> </p>
【问题讨论】:
-
您是否要解密刚刚加密的文本?如果是这样,那是不是意味着您应该将 demo1 的值放入 text1 中?
-
您文档中的 ID“inputText”重复了,您确实应该检查浏览器控制台是否有错误
-
还有
y != decry? -
@FirdausIndradhirmaya 我想手动复制加密的文本,我可以在相同的密钥的帮助下解密文本,但解密功能不起作用
-
@Newcoder "message": "ReferenceError: decry is not defined", "filename": "stacksnippets.net/js", "lineno": 27, "colno": 26 这就是我在运行你的 sn-p
标签: javascript html encryption aes