JS 如何解密 Java 的 DES 加密的字符?

2017-11-01 15:32:33 +08:00
 xoxo419

现在需要在微信小程序用 JS 解密 JAVA 加密后的字符串. 如何可以用 JS 代码来解密?

public static String encrypt(String xmlStr) {
        byte[] encrypt = null;

        try {
            // 取需要加密内容的 utf-8 编码。
            encrypt = xmlStr.getBytes("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        // 取 MD5Hash 码,并组合加密数组
        byte[] md5Hasn = null;
        try {
            md5Hasn = EncryptUtil.MD5Hash(encrypt, 0, encrypt.length);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 组合消息体
        byte[] totalByte = EncryptUtil.addMD5(md5Hasn, encrypt);

        // 取密钥和偏转向量
        byte[] key = new byte[8];
        byte[] iv = new byte[8];
        getKeyIV(EncryptUtil.key, key, iv);



        SecretKeySpec deskey = new SecretKeySpec(key, "DES");



        IvParameterSpec ivParam = new IvParameterSpec(iv);

        // 使用 DES 算法使用加密消息体
        byte[] temp = null;
        try {
            temp = EncryptUtil.DES_CBC_Encrypt(totalByte, deskey, ivParam);
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 使用 Base64 加密后返回
        return new BASE64Encoder().encode(temp);
    }

    public static String decrypt(String xmlStr) throws Exception {
        // base64 解码
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] encBuf = null;
        try {
            encBuf = decoder.decodeBuffer(xmlStr);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 取密钥和偏转向量
        byte[] key = new byte[8];
        byte[] iv = new byte[8];
        getKeyIV(EncryptUtil.key, key, iv);

        SecretKeySpec deskey = new SecretKeySpec(key, "DES");
        IvParameterSpec ivParam = new IvParameterSpec(iv);

        // 使用 DES 算法解密
        byte[] temp = null;
        try {
            temp = EncryptUtil.DES_CBC_Decrypt(encBuf, deskey, ivParam);
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 进行解密后的 md5Hash 校验
        byte[] md5Hash = null;
        try {
            md5Hash = EncryptUtil.MD5Hash(temp, 16, temp.length - 16);
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 进行解密校检
        for (int i = 0; i < md5Hash.length; i++) {
            if (md5Hash[i] != temp[i]) {
                // System.out.println(md5Hash[i] + "MD5 校验错误。" + temp[i]);
                throw new Exception("MD5 校验错误。");
            }
        }

        // 返回解密后的数组,其中前 16 位 MD5Hash 码要除去。
        return new String(temp, 16, temp.length - 16, "utf-8");
    }

完整代码 java-des 类

2480 次点击
所在节点    程序员
3 条回复
applesstt
2017-11-01 15:45:45 +08:00
Karblue
2017-11-01 17:41:35 +08:00
ajax 请求服务端解密。解密最好不要放在客户端。
xoxo419
2017-11-02 08:17:36 +08:00
@Karblue #2 我也是这样觉得的. 但现在的需求就是要客户端做解密.

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/402641

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX