我无法解密我的文本,但我可以加密 有人可以检查我的代码吗

I am unable to Decrypt my text but I am able to encrypt can someone please check my code

加密有效但解密根本不起作用,我无法发现我的错误

在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>

你犯了两个错误

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>

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

欢迎任何形式的贡献