一、方案详细说明
更新内容: 报文添加加密功能
使用终端: RTU
加密方式: DES加密
DES加密模式: ECB模式
填充方式: zeropadding
二、简单测试案例
1、示例1:
密文(hex): 8bb47a0cf0a9626d2b166ab8314d8fb5
密码: 12345678
对应明文: 0123456789
2、示例2:
密文(hex): ff0d7d28f151afcb5c9c0bed5868c88b1530b0aec5f424d81f4c3e9dbc520c59e90fbfb9913e3c1b7e6cd019df36016de09a41a1b9b4bf265f6a7d528e394f39d38377496afb736d9555bb58a1e67357188fbd94a9d5ca52bee263f693ac45c72f5cb2e7a220696ed2c134f03a8135b2d9b3d4e0deb60708474aa8116f476cc20c6da503b069dcd418a76ed28d157ade3d7595a98bff809d
密码: 12345678
明文: EB910048648961079140002C01000001010704050225A5C1B1010000000000000063413AE13A42D8F5C343A40C2943B4D5A2406AE148413051C99E90FF97412C85F200000000CA7B
三、后台解析程序
约定密码: 12345678
解密程序: 前期已提供测试,备份在文件夹中。
package com.ybu.des;
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;
/**
* 注意:DES加密和解密过程中,密钥长度都必须是8的倍数
*/
public class DES {
public DES() {
}
// 测试
public static void main(String args[]) {
// 待加密内容
String str = "0123456789";
// 密码,长度要是8的倍数
String password = "12345678";
byte[] result = DES.encrypt(str.getBytes(), password);
// System.out.println("加密后:----" + new String(result));
System.out.println("加密后:----" + byteToHex(result));
// 直接将如上内容解密
try {
byte[] decryResult = DES.decrypt(result, password);
System.out.println("解密后:----" + new String(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();
}
}
可以参考博客C/C++使用openssl进行摘要和加密解密(md5, sha256, des, rsa)
openssl里面有很多用于摘要哈希、加密解密的算法,方便集成于工程项目,被广泛应用于网络报文中的安全传输和认证。下面以des的api简单使用作为例子。
算法介绍
des: https://en.wikipedia.org/wiki/Data_Encryption_Standard
工程配置
编译openssl库,得到头文件include和链接库lib和dll
配置包含头文件目录和库目录
工程中设置链接指定的lib:libssl.lib,libcrypto.lib
将对应的dll拷贝到exe执行目录:libcrypto-1_1.dll, libssl-1_1.dll
#ifndef _DES_ECB_H
#define _DES_ECB_H
#include
typdedef std::string String;
#include "openssl/des.h"
// DES加密 ECB模式
static String encrypt_des_ecb(const String& mingwen, const String& keystr)
{
String strCipherText;
DES_cblock keyEncrypt;
memset(keyEncrypt, 0, 8);
memcpy(keyEncrypt, keystr.c_str(), keystr.size() >= 8 ? 8 : keystr.size());
DES_key_schedule keySchedule; //密钥表
DES_set_key_unchecked(&keyEncrypt, &keySchedule); //设置密钥,且不检测密钥奇偶性
// 循环加密,每8字节一次
const_DES_cblock inputText;
DES_cblock outputText;
Uint8Array vecCiphertext;
uint8 tmp[8];
Uint8Array vecFull((mingwen.size() + 7) / 8 * 8, 0);
memcpy(vecFull.data(), mingwen.data(), mingwen.size());
for (int i = 0; i < vecFull.size() / 8; i++)
{
memcpy(inputText, vecFull.data() + i * 8, 8);
DES_ecb_encrypt(&inputText, &outputText, &keySchedule, DES_ENCRYPT); //加密
memcpy(tmp, outputText, 8);
std::copy(tmp, tmp + 8, std::back_inserter(vecCiphertext));
}
strCipherText.clear();
strCipherText.assign(vecCiphertext.begin(), vecCiphertext.end());
return strCipherText;
}
// DES解密 ECB模式
static String decrypt_des_ecb(const String& miwen, const String& keystr)
{
String clearText;
DES_cblock keyEncrypt;
memset(keyEncrypt, 0, 8);
memcpy(keyEncrypt, keystr.c_str(), keystr.size() >= 8 ? 8 : keystr.size());
DES_key_schedule keySchedule; //密钥表
DES_set_key_unchecked(&keyEncrypt, &keySchedule); //设置密钥,且不检测密钥奇偶性
// 循环解密,每8字节一次
const_DES_cblock inputText;
DES_cblock outputText;
Uint8Array vecCleartext;
uint8 tmp[8];
Uint8Array vecFull((miwen.size() + 7) / 8 * 8, 0);
memcpy(vecFull.data(), miwen.data(), miwen.size());
for (int i = 0; i < vecFull.size() / 8; i++)
{
memcpy(inputText, vecFull.data() + i * 8, 8);
DES_ecb_encrypt(&inputText, &outputText, &keySchedule, DES_DECRYPT); //解密
memcpy(tmp, outputText, 8);
std::copy(tmp, tmp + 8, std::back_inserter(vecCleartext));
}
clearText.clear();
clearText.assign(vecCleartext.begin(), vecCleartext.end());
return clearText;
}
#endif
#include
#include "DES.h"
// 转换hex到字符串显示
String hex2str(const char* buff, const size_t buffsize, const char* sep = "", bool is_case = false) {
String out;
char ch[4];
const char* fmt = is_case ? "%02x" : "%02X";
for (size_t i = 0; i < buffsize; i++) {
sprintf(ch, fmt, buff[i] & 0xFF);
if (out.empty()) {
out = ch;
}
else {
out += sep;
out += ch;
}
}
return out;
}
int main(int argc, char* argv[])
{
// 原始明文
// 待加密内容
String mingwenText = "0123456789";
std::cout << "原始字符串为:" << mingwenText << std::endl;
// des
std::cout << "=== des ecb加解密 ===" << std::endl;
// 密码,长度要是8的倍数
std::string desKey = "12345678";
String miwen = DES::encrypt_des_ecb(mingwenText, desKey);
String miwenStr = hex2str(miwen.data(), miwen.size());
std::cout << "加密后的字符串: " << miwenStr << std::endl;
String mingwen = DES::decrypt_des_ecb(miwen, desKey);
int lastPos = mingwen.find_first_of('\0');
mingwen = mingwen.substr(0, lastPos);
std::cout << "解密后的字符串: " << mingwen << std::endl;
system("pause");
return 0;
}