Search code examples
javascriptencryptioncryptojs

Ciphertext is not converting to plain text and is not being alerted


I am not able to decrypt a ciphertext. I have to test that my decryption is working properly or not. So, I created a simple html file which take cipher text and than convert it into plain text.

I just here hardcoding the value and than converting ciphertext into plain text.

When I tried it it was not working at all. I don't understand what is the issue.

This is my code

<!DOCTYPE html>
<html>
<head>
    <script src="tripledes.js"></script>
    <script src="mode-ecb.js"></script>
    <style type="text/css">
<script type="text/javascript">

        function decryptByDES(aHJHDJSHJhjsak=, highishjdhsjhjs) {
            var keyHex = CryptoJS.enc.Utf8.parse(highishjdhsjhjs);

            var decrypted = CryptoJS.DES.decrypt({
                ciphertext: CryptoJS.enc.Base64.parse(aHJHDJSHJhjsak=)
            }, keyHex, {
                mode: CryptoJS.mode.ECB,
                padding: CryptoJS.pad.Pkcs7
            });

            return decrypted.toString(CryptoJS.enc.Utf8);
            alert ( decrypted);
        }


    </script>
</head>
<body>

    <div class="maindiv">
        <div>
            <label for="name">Message:</label>
            <input type="text" id="msg" name="msg" />
        </div>
        <div>
            <label for="mail">Key:</label>
            <input type="text" id="key" name="key" />
        </div>

        <div class="button">
            <button onclick="decryptByDES()">View</button>
        </div>
    </div>
</body>
</html>

and my mode-ecb.js file is

/*
CryptoJS v3.1.2
code.google.com/p/crypto-js
(c) 2009-2013 by Jeff Mott. All rights reserved.
code.google.com/p/crypto-js/wiki/License
*/
/**
 * Electronic Codebook block mode.
 */
CryptoJS.mode.ECB = (function () {
    var ECB = CryptoJS.lib.BlockCipherMode.extend();

    ECB.Encryptor = ECB.extend({
        processBlock: function (words, offset) {
            this._cipher.encryptBlock(words, offset);
        }
    });

    ECB.Decryptor = ECB.extend({
        processBlock: function (words, offset) {
            this._cipher.decryptBlock(words, offset);
        }
    });

    return ECB;
}());

I need to show my decrypted text in an alert. But nothing is happening.


Solution

  • I'm not familiar with CryptoJS, but... It looks like you need to move the alert before the return decrypted.toString(CryptoJS.enc.Utf8); line, as the alert won't get called once the function returns.

    Also, it would be better practice to make your key and cipher text variable strings, then call it from the button passing in those variables (although you may want to store your key in the javascript, and only pass in the cipherTextString).

    <script type="text/javascript">
        function decryptByDES(cipherTextString, keyString) {
            var keyHex = CryptoJS.enc.Utf8.parse(keyString);
    
            var decrypted = CryptoJS.DES.decrypt({
                ciphertext: CryptoJS.enc.Base64.parse(cipherTextString)
            }, keyHex, {
                mode: CryptoJS.mode.ECB,
                padding: CryptoJS.pad.Pkcs7
            });
    
            var decryptedStringified = decrypted.toString(CryptoJS.enc.Utf8);
    
            alert(decryptedStringified);
    
            return decryptedStringified;
        }
    </script>
    

    And then call it from your button, passing in the correct variables:

    <button onclick="decryptByDES('aHJHDJSHJhjsak=', 'highishjdhsjhjs');">View</button>