首页 > 技术文章 > 为什么 CryptoJS DES 加密的结果和 Java DES 不一样?(转)

nuannuan7362 2020-06-30 19:16 原文

最近需要对数据进行加密/解密, 因此选用了CryptoJS库, 对数据做DES算法的加密/解密

首选查看官方示例, 将密文进行Base64编码, 掉进一个大坑

<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/tripledes.js"></script>
<script>
    var encrypted = CryptoJS.DES.encrypt("Message", "Secret Passphrase");
    // ciphertext changed every time you run it
    // 加密的结果不应该每次都是一样的吗?
    console.log(encrypted.toString(), encrypted.ciphertext.toString(CryptoJS.enc.Base64));
    var decrypted = CryptoJS.DES.decrypt(encrypted, "Secret Passphrase");
    console.log(decrypted.toString(CryptoJS.enc.Utf8));
</script>

对这些加密算法不了解, 只能求助Google
des encrypion: js encrypted value does not match the java encrypted value

In cryptoJS you have to convert the key to hex and useit as word just like above (otherwise it will be considered as passphrase)

For the key, when you pass a string, it’s treated as a passphrase and used to derive an actual key and IV. Or you can pass a WordArray that represents the actual key.

原来是我指定key的方式不对, 直接将字符串做为参数, 想当然的以为这就是key, 其实不然, CryptoJS会根据这个字符串算出真正的key和IV(各种新鲜名词不解释, 问我也没用, 我也不懂 -_-“)

那么我们只需要将key和iv对应的字符串转成CryptoJS的WordArray类型, 在DES加密时做为参数传入即可, 这样对Message这个字符串加密, 每次得到的密文都是YOa3le0I+dI=

var keyHex = CryptoJS.enc.Utf8.parse('abcd1234');
var ivHex = CryptoJS.enc.Utf8.parse('inputvec');
var encrypted = CryptoJS.DES.encrypt('Message', keyHex, { iv: ivHex });

这样是不是就万事OK了? 哪有, 谁知道这坑是一个接一个啊.
我们再试试Java这边的DES加密是不是和这个结果一样, 具体实现请参考Simple Java Class to DES Encrypt Strings

果真掉坑里了, Java通过DES加密Message这个字符串得到的结果是8dKft9vkZ4I=和CryptoJS算出来的不一样啊…亲

继续求助Google
C# and Java DES Encryption value are not identical

SunJCE provider uses ECB as the default mode, and PKCS5Padding as the default padding scheme for DES.(JCA Doc)
This means that in the case of the SunJCE provider,
Cipher c1 = Cipher.getInstance(“DES/ECB/PKCS5Padding”);
and
Cipher c1 = Cipher.getInstance(“DES”);
are equivalent statements.

原来是CryptoJS进行DES加密时, 默认的模式和padding方式和Java默认的不一样造成的, 必须使用ECB mode和PKCS5Padding, 但是CryptoJS中只有Pkcs7, 不管了, 试试看…

<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/tripledes.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/mode-ecb.js"></script>
<script>
    var keyHex = CryptoJS.enc.Utf8.parse('abcd1234');
    var encrypted = CryptoJS.DES.encrypt('Message', keyHex, {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7
    });
console.log(encrypted.toString(),encrypted.ciphertext.toString(CryptoJS.enc.Base64));
</script>

咦…使用Pkcs7能得到和Java DES一样的结果了, 哇塞…好神奇
那我们试试统一Java也改成Cipher.getInstance(“DES/ECB/PKCS7Padding”)试试, 结果得到一个大大的错误
Error:java.security.NoSuchAlgorithmException: Cannot find any provider supporting DES/ECB/PKCS7Padding

没办法, 继续Google

java.security.NoSuchAlgorithmException: Cannot find any provider supporting AES/ECB/PKCS7PADDING

I will point out that PKCS#5 and PKCS#7 actually specify exactly the same type of padding (they are the same!), but it’s called #5 when used in this context.

推荐阅读