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


    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
    本文链接:https://blog.csdn.net/zxdscn/article/details/78104659
             <!--一个博主专栏付费入口结束-->
            <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-d284373521.css">
                                        <div id="content_views" class="markdown_views">
                    <!-- flowchart 箭头图标 勿删 -->
                    <svg xmlns="http://www.w3.org/2000/svg" style="display: none;">
                        <path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path>
                    </svg>
                                            <ol>
    
  • 为了使C端与java端的3des加解密互通,我们一般使用“DESede/ECB/NoPadding”加密模式;
  • 而在我们java端,我们都知道3des的密钥都是24字节的,而C端是16字节,此处为重点:我们java端的密钥组成为16字节密钥 + 其前8个字节组成24字节密钥
  • 请看代码:
  • 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
    • 3
    • 4
    • 5
    • 6
    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
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
            <link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-526ced5128.css" rel="stylesheet">
                </div>
  • 相关阅读:
    将aspx页面编译成dll
    Jquery 验证数字
    c#反编译生成DLL过程
    c#进制转换
    Spring Mvc 实例
    wamp phpMyAdmin error #1045
    Tomcat相关知识点总结(jsp)
    Java ---学习笔记(泛型)
    Java IO ---学习笔记(文件操作与随机访问文件)
    Java IO ---学习笔记(字符流)
  • 原文地址:https://www.cnblogs.com/zx-blog/p/11849856.html
  • Copyright © 2020-2023  润新知