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

Stella981
• 阅读 585
版权声明:本文为博主原创文章,遵循 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>
    
    点赞
    收藏
    评论区
    推荐文章
    blmius blmius
    2年前
    MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
    文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
    Wesley13 Wesley13
    2年前
    java将前端的json数组字符串转换为列表
    记录下在前端通过ajax提交了一个json数组的字符串,在后端如何转换为列表。前端数据转化与请求varcontracts{id:'1',name:'yanggb合同1'},{id:'2',name:'yanggb合同2'},{id:'3',name:'yang
    待兔 待兔
    2星期前
    手写Java HashMap源码
    HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
    Stella981 Stella981
    2年前
    AndroidStudio封装SDK的那些事
    <divclass"markdown\_views"<!flowchart箭头图标勿删<svgxmlns"http://www.w3.org/2000/svg"style"display:none;"<pathstrokelinecap"round"d"M5,00,2.55,5z"id"raphael
    Stella981 Stella981
    2年前
    PhoneGap设置Icon
    参考:http://cordova.apache.org/docs/en/latest/config\_ref/images.html通过config.xml中的<icon标签来设置Icon<iconsrc"res/ios/icon.png"platform"ios"width"57"height"57"densi
    Wesley13 Wesley13
    2年前
    00:Java简单了解
    浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
    Stella981 Stella981
    2年前
    Elasticsearch源码导入IDEA
    <divid"article\_content"class"article\_contentclearfix"<divclass"articlecopyright"<spanclass"creativecommons"<arel"license"href"http://creativecommons.org/licen
    Wesley13 Wesley13
    2年前
    MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
    背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
    Python进阶者 Python进阶者
    6个月前
    Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
    大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这