由于项目需要,h5需要进行des的加解密,所以对js的加解密研究了一番;现小结如下:

js进行des解密要与java端的加解密一致;

java端的des加密代码如下:


public static String encrypt(String message, String key) throws Exception {
    Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");

    DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));

    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
    IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));
    cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);

    return encodeBase64(cipher.doFinal(message.getBytes("UTF-8")));
}

三个关键因素:DES/CBC/PKCS5Padding中,cbc:代表模式,pkcs5padding代表一个属性;

对应于js端,也有如cbc,pkcs5padding或pkcs7padding(与pkcs5padding是一样的);着重注意cbc模式;


除此外,还有一个值,iv值要特别注意;当为cbc模式,iv向量要保持一致;

废话不多说,js代码粘贴如下:


<html>
<head>
<#include "/includes/baseInclude.ftl">
<#include "/common/base_css.ftl">
<#include "/common/base_js.ftl" >
<#assign pop=JspTaglibs["/WEB-INF/taglib/pop-taglib.tld"]>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <title>JS设置DES加密处理</title>
<#-- <script src="js/rollups/tripledes.js"></script>
 <script src="js/components/mode-ecb.js"></script>-->
    <script>
        //DES 解密 加密
        function encryptByDES(message, key) {
            console.log(CryptoJS.mode.CBC);
            var keyHex = CryptoJS.enc.Utf8.parse(key);
            var iv = CryptoJS.enc.Utf8.parse(key);
            var encrypted = CryptoJS.DES.encrypt(message, keyHex, {
                iv:iv,
                mode: CryptoJS.mode.CBC,
                padding: CryptoJS.pad.Pkcs7
            });
            return encrypted.toString();
        }
        //DES 解密

        function decryptByDES(ciphertext, key) {
            var keyHex = CryptoJS.enc.Utf8.parse(key);
            var iv=CryptoJS.enc.Utf8.parse(key);
            // direct decrypt ciphertext
            var decrypted = CryptoJS.DES.decrypt({
                ciphertext: CryptoJS.enc.Base64.parse(ciphertext)
            }, keyHex, {
                iv:iv,
                mode: CryptoJS.mode.CBC,
                padding: CryptoJS.pad.Pkcs7
            });
            return decrypted.toString(CryptoJS.enc.Utf8);
        }
    </script>
    <script>
        //加密
        function encryptStr() {
            var strKey = $.trim($('#key').val());
            var strMsg = $.trim($('#text1').val());
            $('#text2').val(encryptByDES(strMsg, strKey));
        }
        //解密

        function decryptStr() {
            var strKey = $.trim($('#key').val());
            var ciphertext = $.trim($('#text2').val());
            $('#text3').val(decryptByDES(ciphertext, strKey));
        }
    </script>
</head>

<body>
<h1>JS设置DES加密处理</h1>
<label>key</label>
<input type="text" value='12345678' id="key" />
<div>
    <textarea rows="10" id="text1" placeholder="请输入需要加密的字符">abcde12345这个中文!@#!@$#%$#^%(":''")[]=_-</textarea>
    <input type="button" value="加密" οnclick="encryptStr();" />
    <br>
    <textarea id="text2" rows="10"></textarea>
    <input type="button" value="解密" οnclick="decryptStr();" />
    <textarea id="text3"></textarea>
</div>
</body>
</html>
注意:得引入cryptoJs 的文件;core-min.js  des相关的js文件;


另外注意,解密的des文件,js端要进行replace "\n"才能得到跟java端一致的源文件;