账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    PHP 3DES 如何兼容 JAVA?
    63
    0

    本人PHP一枚,最近对接JAVA,在使用3DES加密算法时发现加密的结果对不上,所以来请教一下。

    相关代码

    package com.test.utils;
    import javax.crypto.Cipher;
    import javax.crypto.SecretKey;
    import javax.crypto.SecretKeyFactory;
    import javax.crypto.spec.DESedeKeySpec;
    import java.security.SecureRandom;
    import java.security.spec.KeySpec;
    /**
     * Des3工具类
     */
    public class Des3Utils {
        /**
         * @Description: DES3 解密
         * @param key
         * @param src
         * @return String
         */
        public static String decryptEde(String key, String src) {
            String plainText = null;
            try {
                byte[] keyBytes = new byte[24];
                asciiToBcdBytes(key, keyBytes, Math.min(32, key.length()) / 2);
    
                for (int i = 0; i < 8; ++i)
                    keyBytes[16 + i] = keyBytes[i];
    
                // 从原始密匙数据创建一个DESKeySpec对象
                KeySpec dks = new DESedeKeySpec(keyBytes);
    
                // 创建一个密匙工厂,然后用它把DESKeySpec对象转换成
                // 一个SecretKey对象
                SecretKey secKey = SecretKeyFactory.getInstance("DESede").generateSecret(dks);
    
                // Cipher对象实际完成解密操作
                Cipher cipher = Cipher.getInstance("DESede/ECB/NoPadding");
    
                // DES算法要求有一个可信任的随机数源
                SecureRandom sr = new SecureRandom();
    
                // 用密匙初始化Cipher对象
                cipher.init(Cipher.DECRYPT_MODE, secKey, sr);
    
                int count = (src.length() + 1) / 2;
                byte[] inputBytes = new byte[count];
                asciiToBcdBytes(src, inputBytes, count);
    
                // 正式执行解密操作
                byte[] decryptBytes = cipher.doFinal(inputBytes);
    
                int olen = decryptBytes.length - 1;
                for (; decryptBytes[olen] == 0 && olen >= 0; --olen) {
                }
    
                plainText = new String(decryptBytes, 0, olen + 1);
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            return plainText;
        }
    
        private static void asciiToBcdBytes(String str, byte[] hex, int count) {
            byte[] inputBytes = str.getBytes();
            for (int i = 0, j = 0; j < inputBytes.length && i < count; ++i) {
                byte v = inputBytes[j];
                ++j;
                if (v <= 0x39){
                    hex[i] = (byte) (v - 0x30);
                } else {
                    hex[i] = (byte) (v - 0x41 + 10);
                }
    
                hex[i] <<= 4;
    
                if (j >= inputBytes.length) {
                    break;
                }
    
                v = inputBytes[j];
                ++j;
    
                if (v <= 0x39) {
                    hex[i] += (byte) (v - 0x30);
                } else {
                    hex[i] += (byte) (v - 0x41 + 10);
                }
            }
        }
    
        /**
         * @Description: DES3 加密
         * @param key
         * @param src
         * @return String
         */
        public static String encryptEde(String key, String src) {
            String plainText = null;
            try {
                byte[] keyBytes = new byte[24];
                asciiToBcdBytes(key, keyBytes, Math.min(32, key.length()) / 2);
    
                for (int i = 0; i < 8; ++i)
                    keyBytes[16 + i] = keyBytes[i];
    
                // 从原始密匙数据创建一个DESKeySpec对象
                KeySpec dks = new DESedeKeySpec(keyBytes);
    
                // 创建一个密匙工厂,然后用它把DESKeySpec对象转换成
                // 一个SecretKey对象
                SecretKey secKey = SecretKeyFactory.getInstance("DESede").generateSecret(dks);
    
                // Cipher对象实际完成解密操作
                Cipher cipher = Cipher.getInstance("DESede/ECB/NoPadding");
    
                // DES算法要求有一个可信任的随机数源
                SecureRandom sr = new SecureRandom();
    
                // 用密匙初始化Cipher对象
                cipher.init(Cipher.ENCRYPT_MODE, secKey, sr);
    
                byte[] srcBytes = src.getBytes("UTF-8");
                int srcLen = srcBytes.length;
                int encLen = ((srcLen % 8) == 0) ? srcLen : ((srcLen / 8 + 1) * 8);
    
                byte[] encBytes = new byte[encLen];
                System.arraycopy(srcBytes, 0, encBytes, 0, srcLen);
    
                // 正式执行解密操作
                byte[] encryptBytes = cipher.doFinal(encBytes);
                plainText = bcdBytesToAscii(encryptBytes, encLen);
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            return plainText;
        }
    
        private static String bcdBytesToAscii(byte[] hex, int count) {
            byte[] ascii = new byte[2 * count];
            int t;
            for (int i = 0; i < count; i++) {
                t = hex[i] & 0xf0;
                t = t >> 4;
                if (t <= 9)
                    t += '0';
                else if (t >= 10 && t <= 15)
                    t += 'A' - 10;
                else
                    t = '0';
                ascii[2 * i] = (byte) t;
    
                t = hex[i] & 0x0f;
                if (t <= 9)
                    t += '0';
                else if (t >= 10 && t <= 15)
                    t += 'A' - 10;
                else
                    t = '0';
                ascii[2 * i + 1] = (byte) t;
            }
    
            return (new String(ascii));
        }
    }

    本人目前使用的是 php 3DES加密如何兼容Java 中的代码。

    3
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 10 元积分
        全部回答
    • 0
    • 九宫衔蝉 普通会员 1楼
      502 Bad Gateway

      502 Bad Gateway


      nginx
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部