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

New coder

Encryption is working but decryption is not working at all and I am not able to spot my mistake

In javascript I have four functions:

First two encrypt and decrypt the text with or without key, Probably there is nothing wrong in first two functions

In third and fourth function I am taking input from html page and by storing them in variable I am encrypting and decrypting them

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>

Denis Turgenev

You made two mistakes

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;

You are using same id for two elements, and it's bad practice.

This is working code.

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Can someone explain me why I am not able to check special characters in my string using ASCII values?

I am not able to use jAlert in my code ?

I am not able to delete items in my todo list made with react, please help me with error in my code

Can someone please help me with the code I am writing? (Java)

I am not able to get data from my localhost using this code. Please help me. I am getting nil

I am not able to getdownloadurl() from firebase. Please anybody help here's my code and error. (I am using Fragment)

I am unable to execute my code in PHP

How can I optimize my code such that I am able to use a loop to plot histograms in subplots?

Why am I not able to pickup items in my text based game?

Why i am not able to change the dummy variable here in my code?

I am currently removing jquery from my code and I am not able to convert this Jquery to Javascript

I'm not able to load my HTML page using ngroute module from angularjs. My code looks fine to me. What mistake am I making?Please review the code

Can someone spot the errors in my code? I don't know what part am I missing and what I messed up

I am not able to receive emails on my VPS

i am not able to get cookie in my Servlet

I am not able to submit my form

I am trying to encrypt a text file using vigenere cipher and xor in c++ but my it will not encrypt the file

Can someone please explain why I am getting this compilation error?

Can someone please tell me where am I wrong in this python code?

Why am I unable to read my new text file?

Why am I unable to list all my accounts with this code?

What is wrong with my code. I am unable to draw on the screen

why am i still unable to push my code to github?

I am unable to understand the output to my c++ code

I am unable to get my VBA code to save to a created folder

Why am i unable to use this simple code in my kotlin repl

i am unable to login and can not validate my user

Why my page is not redirecting to Google if I am giving correct password also? Please see my code

my code seems perfect but showing wrong output. can someone help me what wrong am i doing here?