JAVA 3DES 加密/解密 32位密钥
2016-02-18 16:16
495 查看
使用3DES 加密/解密
在java中3DES密钥都是24字节的,当使用16字节密钥时24字节密钥后8位为16自己密钥补位获得的。
例如 16字节密钥 8字节字节1 8字节2
24字节密钥 8字字节1 8字节2 8字节1
场景为:
用测试密钥值:“00000000000000000000000000000000” 加密数据 0000010000030200 加密后F35F77221C054EF0
在java中3DES密钥都是24字节的,当使用16字节密钥时24字节密钥后8位为16自己密钥补位获得的。
例如 16字节密钥 8字节字节1 8字节2
24字节密钥 8字字节1 8字节2 8字节1
场景为:
用测试密钥值:“00000000000000000000000000000000” 加密数据 0000010000030200 加密后F35F77221C054EF0
package com.easylife.support.iccard.common; /* 字符串 DESede(3DES) 加密 */ import java.security.*; import java.util.ArrayList; import java.util.List; import javax.crypto.*; import javax.crypto.spec.SecretKeySpec; public class ThreeDes { private static final String Algorithm = "DESede"; // 定义 加密算法,可用 // DES,DESede,Blowfish private static final byte[] keyBytes = { 0x00, 0x00, 0x00, 0x00, (byte) 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x00, (byte) 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x00, 0x00, 0x00, 0x00, (byte) 0x00 }; // 24字节的密钥 // keybyte为加密密钥,长度为24字节 // src为被加密的数据缓冲区(源) public static byte[] encryptMode(byte[] src) { try { // 生成密钥 SecretKey deskey = new SecretKeySpec(keyBytes, Algorithm); // 加密 Cipher c1 = Cipher.getInstance(Algorithm); c1.init(Cipher.ENCRYPT_MODE, deskey); return c1.doFinal(src); } catch (java.security.NoSuchAlgorithmException e1) { e1.printStackTrace(); } catch (javax.crypto.NoSuchPaddingException e2) { e2.printStackTrace(); } catch (java.lang.Exception e3) { e3.printStackTrace(); } return null; } // keybyte为加密密钥,长度为24字节 // src为加密后的缓冲区 public static byte[] decryptMode(byte[] src) { try { // 生成密钥 SecretKey deskey = new SecretKeySpec(keyBytes, Algorithm); // 解密 Cipher c1 = Cipher.getInstance(Algorithm); c1.init(Cipher.DECRYPT_MODE, deskey); return c1.doFinal(src); } catch (java.security.NoSuchAlgorithmException e1) { e1.printStackTrace(); } catch (javax.crypto.NoSuchPaddingException e2) { e2.printStackTrace(); } catch (java.lang.Exception e3) { e3.printStackTrace(); } return null; } // 转换成十六进制字符串 public static String byte2hex(byte[] b) { String hs = ""; String stmp = ""; for (int n = 0; n < b.length; n++) { stmp = (java.lang.Integer.toHexString(b & 0XFF)); if (stmp.length() == 1) hs = hs + "0" + stmp; else hs = hs + stmp; if (n < b.length - 1) hs = hs + ":"; } return hs.toUpperCase(); } /** * 字符串3DES加密 * * @param str * @return 加密的字符串 */ public static String jiami(String str) { // 获取字符串 List<String> strs = new ArrayList<String>(); for (int i = 1; i <= str.length() / 2; i++) { strs.add(str.substring((i - 1) * 2, i * 2)); } byte[] l = new byte[8]; for (int i = 0; i < strs.size(); i++) { int aaa = MZCardCommon.strToInt(strs.get(i).toUpperCase()); l[i] = (byte) aaa; } byte[] encodedl = encryptMode(l); String encodedS = byte2hex(encodedl); // System.out.println("加密后的字符串:" + encodedS); encodedS = MZCardCommon.subData(encodedS); // System.out.println("加密后处理的字符串:" + encodedS); return encodedS; } /** * 字符串3DES解密 * * @param str * @return 解密字符串 */ public static String jiemi(String str) { // 获取字符串 List<String> strs = new ArrayList<String>(); for (int i = 1; i <= str.length() / 2; i++) { strs.add(str.substring((i - 1) * 2, i * 2)); } byte[] b1 = new byte[8]; for (int i = 0; i < strs.size(); i++) { int aaa = MZCardCommon.strToInt(strs.get(i).toUpperCase()); b1[i] = (byte) aaa; } // byte[] encodedl = encryptMode(MZCardCommon.byteMerger(b1, b2)); byte[] encodedl = encryptMode(b1); String encodedS = byte2hex(encodedl); // System.out.println("解密后的字符串:" + encodedS); encodedS = MZCardCommon.subData(encodedS); // System.out.println("解密后处理的字符串:" + encodedS); return encodedS; } public static void main(String[] args) { // 添加新安全算法,如果用JCE就要把它添加进去 // Security.addProvider(new com.sun.crypto.provider.SunJCE()); // // final byte[] keyBytes = { 0x00, 0x00, 0x00, 0x00, (byte) 0x00, 0x00, // 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x00, (byte) 0x00, // 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x00, 0x00, 0x00, 0x00, // (byte) 0x00 }; // 24字节的密钥 // String szSrc = "0000010000030200"; // // System.out.println("加密前的字符串:" + szSrc); // byte[] a = { 0x00, 0x00, 0x01, 0x00, (byte) 0x00, 0x03, 0x02, 0x00 }; // byte[] aa = { 0, 0, 1, 0, 0, 3, 2, 0 }; // // 1160010098968120 // byte[] b = { 0x11, 0x60, 0x01, 0x00, (byte) 0x98, (byte) 0x96, // (byte) 0x81, 0x20 }; // byte[] bb = { 17, 96, 1, 0, (byte) 152, (byte) 150, (byte) 129, 32 }; // byte[] encodeda = encryptMode(a); // byte[] encodedaa = encryptMode(aa); // System.out.println("16加密后的字符串:" + byte2hex(encodeda)); // System.out.println("10加密后的字符串:" + byte2hex(encodedaa)); // byte[] b1 = { (byte) 0xF3, 0x5F, 0x77, 0x22, (byte) 0x1C, 0x05, 0x4E, // (byte) 0xF0}; // byte[] b2={0x7E, 0x42 ,0x28,0x22 ,0x77 ,0x36 ,0x66 ,(byte) 0xC0}; // // byte[] srcBytes = decryptMode(MZCardCommon.byteMerger(b1, b2)); // System.out.println("解密后的字符串:" + byte2hex(srcBytes)); System.out.println("*************加密 开始**************"); System.out.println(jiami("0000010000030200")); System.out.println(jiami("1160010098968120")); System.out.println(jiami("1002088000000000")); System.out.println("*************加密 结束**************"); System.out.println("*************解密 开始**************"); System.out.println(jiemi("F35F77221C054EF0")); System.out.println(jiemi("172BF0FD90E194D8")); System.out.println(jiemi("D59B5CE930D9723C")); System.out.println("*************解密 结束**************"); } }
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树