I'm trying to encrypt and decrypt in JavaScript using TripleDES.js. Here is my code snippet:
Encryption Method:
encrypt_string = function (plainData) {
var encrypted = CryptoJS.TripleDES.encrypt(plainData, "My Secret Key");
alert("Encrypted: " + encrypted);
}
Decryption Method:
decrypt_string = function (cipherData) {
var decrypted = CryptoJS.TripleDES.decrypt(cipherData, "My Secret Key");
alert("Decrypted: " + decrypted);
}
While passing plainData
as Gokul Nath
, to encrypt_string()
, the alert message shows:
U2FsdGVkX1/huVhh9IQhJF72gcs26f1l0+hNSsWEXsc=
While passing cipherData
as U2FsdGVkX1/huVhh9IQhJF72gcs26f1l0+hNSsWEXsc=
, to decrypt_string()
, the alert message shows:
476f6b756c204e617468
Question: How to get the original plain data while decrypting?
476f6b756c204e617468
is the hex representation of the original string:
47 6f 6b 75 6c 20 4e 61 74 68
G o k u l |/ N a t h
|
+-> this is a space
You can use something like decrypted.toString(CryptoJS.enc.Latin1)
to get the string value, as per:
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/tripledes.js"></script>
<script>
var encrypted = CryptoJS.TripleDES.encrypt("Gokul Nath", "My Secret Key");
var decrypted = CryptoJS.TripleDES.decrypt(encrypted, "My Secret Key");
alert("Decrypted: " + decrypted.toString(CryptoJS.enc.Latin1));
</script>
which gives: