如何使用TripleDES在CryptoJS中获取原始字符串?

时间:2014-12-11 08:44:13

标签: javascript encryption cryptojs tripledes

我正在尝试使用TripleDES.js在JavaScript中加密和解密。这是我的代码片段:

加密方法:

encrypt_string = function (plainData) {

   var encrypted = CryptoJS.TripleDES.encrypt(plainData, "My Secret Key");

   alert("Encrypted: " + encrypted);

}

解密方法:

decrypt_string = function (cipherData) {

   var decrypted = CryptoJS.TripleDES.decrypt(cipherData, "My Secret Key");

   alert("Decrypted: " + decrypted);

}

plainData作为Gokul Nath传递给encrypt_string()时,警告消息显示:

U2FsdGVkX1/huVhh9IQhJF72gcs26f1l0+hNSsWEXsc=

cipherData作为U2FsdGVkX1/huVhh9IQhJF72gcs26f1l0+hNSsWEXsc=传递给decrypt_string()时,警告消息显示:

476f6b756c204e617468

问题: 如何在解密时获取原始普通数据?

1 个答案:

答案 0 :(得分:4)

476f6b756c204e617468是原始字符串的十六进制表示形式:

47 6f 6b 75 6c 20  4e 61 74 68
G  o  k  u  l  |/  N  a  t  h
               |
               +-> this is a space

您可以使用decrypted.toString(CryptoJS.enc.Latin1)之类的内容来获取字符串值,如下所示:

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

给出:

enter image description here