DES ECB解密说明
1、后台接收的报文
[2020-08-12 16:04:15.037]# RECV HEX FROM 117.136.81.93 :51423>
C4 FC 35 4A 83 E1 71 F2 0D C0 FC B1 01 72 A2 DE D3 42 8C C8 39 0E EA 17 E8 24 4A 0A 0C 71 70 5B A4 06 FA FB 27 94 96 D9 AD E5 BB B6 29 05 D8 DF BE D9 CD F5 BB 33 CA 42 64 F3 6C 82 AB C4 D6 9D 2F E0 61 45 C2 20 49 02
2、接收报文后去空格得到字符串
String miwen = "C4FC354A83E171F20DC0FCB10172A2DED3428CC8390EEA17"
+ "E8244A0A0C71705BA406FAFB279496D9ADE5BBB62905D8DF"
+ "BED9CDF5BB33CA4264F36C82ABC4D69D2FE06145C2204902";
3、密文字符串转成对应的byte数组,注意这里不是string.getBytes()方法
byte[] _miwen = hexString2Bytes(miwen);
4、调用解密方法,得到明文的byte数组,秘钥是12345678
byte[] decryResult = DES.decrypt(_miwen, password);
/**
* 解密
*
* @param src byte[]
* @param password String
* @return byte[]
* @throws Exception
*/
public static byte[] decrypt(byte[] src, String password) throws Exception {
// DES算法要求有一个可信任的随机数源
SecureRandom random = new SecureRandom();
// 创建一个DESKeySpec对象
DESKeySpec desKey = new DESKeySpec((password.getBytes()));
// 创建一个密匙工厂
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
// 将DESKeySpec对象转换成SecretKey对象
SecretKey securekey = keyFactory.generateSecret(desKey);
// Cipher对象实际完成解密操作
Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
// 用密匙初始化Cipher对象
cipher.init(Cipher.DECRYPT_MODE, securekey, random);
// 真正开始解密操作
return cipher.doFinal(src);
}
5、得到的明文再转string进行数据解析
6、关键步骤输出,分别打印了密文和明文的byte数组,已转hex输出
7、下面的是DES ECB解密程序的java实现DEMO
package com.trq.nengyuan;
import java.security.SecureRandom;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.SecretKeyFactory;
import javax.crypto.SecretKey;
import javax.crypto.Cipher;
import java.util.*;
/**
* 注意:DES加密和解密过程中,密钥长度都必须是8的倍数
*/
public class DES {
public DES() {
}
// 测试
public static void main(String args[]) {
// 待加密内容
//String str = "0123456789";
String strmingwen = "eb9100481380013800009c4a01000301010804050226c6cf7e0002b7000000000000295cef41711d4b4400000000000000000000000041008ec12d0e560440fc2db000000000ac6e";
// 密码,长度要是8的倍数
String password = "12345678";
// 测试密文,长度要是8的倍数
// String miwen = "C4FC354A83E171F20DC0FCB10172A2DED3428CC8390EEA17"
// + "E8244A0A0C71705BA406FAFB279496D9ADE5BBB62905D8DF"
// + "BED9CDF5BB33CA4264F36C82ABC4D69D2FE06145C2204902";
// 在解密前,将字符串的密文转换成16进制的字节数组
// byte[] _miwen = hexString2Bytes(miwen);
try {
// 1.明文 DES ECB加密测试
// 在加密前,将需要加密的明文转换成16进制字节数组
byte[] _mingwenTest = hexString2Bytes(strmingwen);
// DES ECB加密
byte[] encrptResult = DES.encrypt(_mingwenTest, password);
System.out.println("--------------- DES ECB 加密测试 -----------------");
System.out.println("加密前:----" + strmingwen);
// System.out.println("加密后:----" + new String(encrptResult));
System.out.println("加密后:----" + byteToHex(encrptResult));
// 2.密文 DES ECB解密测试
// 测试密文,长度要是8的倍数
byte[] _miwen = encrptResult;
System.out.println("--------------- DES ECB 解密测试 -----------------");
System.out.println("解密前:----" + byteToHex(_miwen));
// DES ECB解密
byte[] decryResult = DES.decrypt(_miwen, password);
// System.out.println("解密后:----" + new String(decryResult));
System.out.println("解密后:----" + byteToHex(decryResult));
} catch (Exception e1) {
e1.printStackTrace();
}
}
/**
* 加密
*
* @param datasource byte[]
* @param password String
* @return byte[]
*/
public static byte[] encrypt(byte[] datasource, String password) {
try {
SecureRandom random = new SecureRandom();
DESKeySpec desKey = new DESKeySpec(password.getBytes());
// 创建一个密匙工厂,然后用它把DESKeySpec转换成
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey securekey = keyFactory.generateSecret(desKey);
// Cipher对象实际完成加密操作
Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
// 用密匙初始化Cipher对象
cipher.init(Cipher.ENCRYPT_MODE, securekey, random);
// 现在,获取数据并加密
// 正式执行加密操作
return cipher.doFinal(addZero(datasource));
} catch (Throwable e) {
e.printStackTrace();
}
return null;
}
/**
* 解密
*
* @param src byte[]
* @param password String
* @return byte[]
* @throws Exception
*/
public static byte[] decrypt(byte[] src, String password) throws Exception {
// DES算法要求有一个可信任的随机数源
SecureRandom random = new SecureRandom();
// 创建一个DESKeySpec对象
DESKeySpec desKey = new DESKeySpec((password.getBytes()));
// 创建一个密匙工厂
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
// 将DESKeySpec对象转换成SecretKey对象
SecretKey securekey = keyFactory.generateSecret(desKey);
// Cipher对象实际完成解密操作
Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
// 用密匙初始化Cipher对象
cipher.init(Cipher.DECRYPT_MODE, securekey, random);
// 真正开始解密操作
return cipher.doFinal(src);
}
public static byte[] getKey(byte[] keyRule) {
SecretKeySpec key = null;
byte[] keyByte = keyRule;
System.out.println(keyByte.length);
// 创建一个空的八位数组,默认情况下为0
byte[] byteTemp = new byte[8];
// 将用户指定的规则转换成八位数组
int i = 0;
for (; i < byteTemp.length && i < keyByte.length; i++) {
byteTemp[i] = keyByte[i];
}
key = new SecretKeySpec(byteTemp, "DES");
return key.getEncoded();
}
public static byte[] addZero(byte[] data) {
byte[] dataByte = data;
if (data.length % 8 != 0) {
byte[] temp = new byte[8 - data.length % 8];
dataByte = byteMerger(data, temp);
}
return dataByte;
}
// java 合并两个byte数组
// System.arraycopy()方法
public static byte[] byteMerger(byte[] bt1, byte[] bt2) {
byte[] bt3 = new byte[bt1.length + bt2.length];
System.arraycopy(bt1, 0, bt3, 0, bt1.length);
System.arraycopy(bt2, 0, bt3, bt1.length, bt2.length);
return bt3;
}
/**
* byte数组转hex
* @param bytes
* @return
*/
public static String byteToHex(byte[] bytes){
String strHex = "";
StringBuilder sb = new StringBuilder("");
for (int n = 0; n < bytes.length; n++) {
strHex = Integer.toHexString(bytes[n] & 0xFF);
sb.append((strHex.length() == 1) ? "0" + strHex : strHex); // 每个字节由两个字符表示,位数不够,高位补0
}
return sb.toString().trim();
}
/**
* @Title:hexString2Bytes
* @Description:16进制字符串转字节数组
* @param src
* 16进制字符串
* @return 字节数组
* @throws
*/
public static byte[] hexString2Bytes(String src) {
int l = src.length() / 2;
byte[] ret = new byte[l];
for (int i = 0; i < l; i++) {
ret[i] = Integer.valueOf(src.substring(i * 2, i * 2 + 2), 16).byteValue();
}
return ret;
}
}
运行结果如下: