Search code examples
javascriptencryptioncryptographycryptojs

Can't decrypt password using CryptoJS


I am trying to make an encryption using CryptoJS. The encryption seems to work fine, but when I decrypt the password, I do not get the initial plain text password.

Here is my JavaScript code:

<script type="text/javascript">
var password = "";
function noSsl()
{
    $(document).ready(function()
    {
        $("#dialog").dialog({ show: 'fade', hide: 'fade' });
    });
}
function savePassword()
{
    password = document.getElementById("ap").value;
    alert("Initial password: " + password);
    enPassword = CryptoJS.AES.encrypt(password, <?php echo '"'.$currentAdk.'"'; ?>);
    $(document).ready(function()
    {
        $("#ap").slideToggle("slow");
        $("#sp").slideToggle("slow");
        $("#sp").remove();
    });
    alert("Encrypted: " + enPassword);
    var decrypted = CryptoJS.AES.decrypt(enPassword, <?php echo '"'.$currentAdk.'"'; ?>)
    alert("Decrypted: " + decrypted);
}
</script>

Here are the results:

enter image description here

enter image description here

enter image description here


Solution

  • To get the decrypted, plaintext password using CryptoJS, you need to use:

    var decryptedPlaintextPassword = CryptoJS.AES.decrypt(enPassword, <?php echo '"'.$currentAdk.'"'; ?>).toString(CryptoJS.enc.Utf8);