【问题标题】:I am unable to Decrypt my text but I am able to encrypt can someone please check my code我无法解密我的文本,但我可以加密有人可以检查我的代码吗
【发布时间】: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


【解决方案1】:

你犯了两个错误

X decry.toString(CryptoJS.enc.Utf8);
O y.toString(CryptoJS.enc.Utf8);

X const text1 = document.getElementById('inputText').value;
O const text1 = document.getElementById('demo1').innerHTML;

您对两个元素使用相同的 id,这是不好的做法。

这是工作代码。

<input type="text" id="inputText" placeholder="Enter a Text">
<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>

<script src="crypto-js.js"></script>
<script>
    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 = y.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('demo1').innerHTML;
        const password2 = document.getElementById('inputPassword').value;
        var x1 = decrypt(text1, password2);
        document.getElementById("demo2").innerHTML = x1;
    }
</script>

【讨论】:

  • 非常感谢,这只是一个变量错误哇
  • 变量错误,我也有这个问题,有时候修复它比任何算法错误、逻辑错误都需要更长的时间,所以这对我来说不再是小错误,被认为是严重错误:)
  • 是的,我明白了,严重错误?
【解决方案2】:

您好,感谢大家的回答,我已经解决了这个问题,现在它是一个可变错误。 源码在github:https://github.com/iArchitSharma/Encrypt-Decrypt-Text

欢迎任何形式的贡献

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-12
    • 2023-03-04
    • 2016-09-27
    • 2012-04-09
    • 2023-04-02
    相关资源
    最近更新 更多