Android C、C++与java端3DES互通

  1. 为了使C端与java端的3des加解密互通,我们一般使用“DESede/ECB/NoPadding”加密模式;
  2. 而在我们java端,我们都知道3des的密钥都是24字节的,而C端是16字节,此处为重点:我们java端的密钥组成为16字节密钥 + 其前8个字节组成24字节密钥
  3. 请看代码:
private static byte[] fillTo24(byte[] key) {
        if (null != key && key.length == 16) {
            return Util.pinJie2(key, Util.subBytes(key, 0, 8));
        }
        return null;
    }
  1. 全部代码为:
public class DES3Utils {
    // 向量
    public final static String iv = "12345678";
    // 3des加密
    public static final String algorithm = "DESede";

    // 加密 src为源数据的字节数组
    public static byte[] encryptBy3DES(byte[] src, byte[] key) {
        return init(src, key, true, 0);
    }

    // 解密函数
    public static byte[] decryptBy3DES(byte[] src, byte[] key) {
        return init(src, key, false, 0);
    }

    /**
     * @param src 需要加密的文字
     * @return 加密后的文字
     * @throws Exception 加密失败
     */
    public static byte[] encryptBy3DESCBC(byte[] src, byte[] key) {
        byte[] fillTo24 = fillTo24(key);
        if (null == fillTo24)
            return null;
        try {
            SecretKey deskey = new SecretKeySpec(fillTo24, algorithm);
            Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
            IvParameterSpec ips = new IvParameterSpec(iv.getBytes());
            cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);
            return cipher.doFinal(src);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 3DES解密
     *
     * @param encryptText 加密文本
     * @return
     * @throws Exception
     */
    public static byte[] decryptBy3DESCBC(byte[] encryptText, byte[] key) {
        byte[] fillTo24 = fillTo24(key);
        if (null == fillTo24)
            return null;
        try {
            SecretKey deskey = new SecretKeySpec(fillTo24, algorithm);
            Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
            IvParameterSpec ips = new IvParameterSpec(iv.getBytes());
            cipher.init(Cipher.DECRYPT_MODE, deskey, ips);
            return cipher.doFinal(encryptText);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @param src 需要加密的文字
     * @return 加密后的文字
     * @throws Exception 加密失败
     */
    public static byte[] encryptBy3DESECB(byte[] src, byte[] key) {
        return init(src, key, true, 1);
    }

    /**
     * 3DES解密
     *
     * @param encryptText 加密文本
     * @return
     * @throws Exception
     */
    public static byte[] decryptBy3DESECB(byte[] encryptText, byte[] key) {
        return init(encryptText, key, false, 1);
    }

    private static byte[] init(byte[] data, byte[] key, boolean mode, int type) {
        byte[] fillTo24 = fillTo24(key);
        if (null == fillTo24)
            return null;
        String types = null;
        try {
            SecretKey deskey = new SecretKeySpec(fillTo24, algorithm);
            switch (type) {
                case 0:
                    types = "DESede";
                    break;
                case 1:
                    types = "DESede/ECB/NoPadding";
                    break;
                case 2:
                    types = "DESede/ECB/PKCS5Padding";
                    break;
            }
            Cipher cipher = Cipher.getInstance(types);
            if (mode)
                cipher.init(Cipher.ENCRYPT_MODE, deskey);
            else
                cipher.init(Cipher.DECRYPT_MODE, deskey);
            return cipher.doFinal(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    private static byte[] fillTo24(byte[] key) {
        if (null != key && key.length == 16) {
            return Util.pinJie2(key, Util.subBytes(key, 0, 8));
        }
        return null;
    }
 }
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值