2021-09-10
作者:互联网
QT三方库 Openssl Des 算法实现
DECcpp文件
#include "DES.h"
#include "HEX.h"
enum Except{ EXCEP_ZERO,EXCEP_ONE};
QDES::QDES()
{
}
//3Des_cbc加密
QString QDES::m3Des_cbc_encrypt(char *clText,char *key1,char *key2,char *key3)
{
int i = 0;
int len = 0;
int nlen = 0;
char ch = '\0';
// char *key1 = "4041424344454647"; /* 原始密钥, 十六进制字符串 */
// char *key2 = "48494A4B4C4D4E4F"; /* 原始密钥, 十六进制字符串 */
// char *key3 = "4041424344454647"; /* 原始密钥, 十六进制字符串 */
// char *data = "01820003000000000000000000000000"; /* 原始明文, 十六进制字符串 */
char *data = clText;
unsigned char src[64] = {0};
unsigned char out[64] = {0};
//unsigned char tmp[64] = {0};
unsigned char *ptr = NULL;
unsigned char block[8] = {0};
DES_key_schedule ks1, ks2, ks3;
DES_cblock ivec;
DES_cblock ivsetup = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
/* 设置密码表 */
ptr = p->hex2bin(key1, strlen(key1), &nlen);
memcpy(block, ptr, sizeof(block));
free(ptr);
DES_set_key_unchecked((const_DES_cblock *)block, &ks1);
ptr = p->hex2bin(key2, strlen(key2), &nlen);
memcpy(block, ptr, sizeof(block));
free(ptr);
DES_set_key_unchecked((const_DES_cblock *)block, &ks2);
ptr = p->hex2bin(key3, strlen(key3), &nlen);
memcpy(block, ptr, sizeof(block));
free(ptr);
DES_set_key_unchecked((const_DES_cblock *)block, &ks3);
ptr = p->hex2bin(data, strlen(data), &nlen);
memcpy(src, ptr, nlen);
free(ptr);
len = (nlen / 8 + (nlen % 8 ? 1: 0)) * 8;
ch = 8 - nlen % 8;
memset(src + nlen, ch, (8 - nlen % 8) % 8);
memcpy(ivec, ivsetup, sizeof(ivsetup));
/* 按照8字节数据进行加密,length=8 */
for (i = 0; i < len; i += 8) {
DES_ede3_cbc_encrypt(src + i, out + i, 8, &ks1, &ks2, &ks3, &ivec, DES_ENCRYPT);
}
const char *result[24];
//const char *mR = new char;
printf("加密后数据: ");
for (i = 0; i < len; i++) {
// printf("%02X" , *(out + i));
result[i] = p->bin2hex((out + i),1);
}
QString mValue;
int valLen = strlen((char *)result);
for (int j=0;j<len;j++)
{
QString temp(result[j]);
mValue += temp;
}
printf(clText);
return mValue;
}
//3Des_cbc加密
QString QDES::m3Des_cbc_nokey_encrypt(char *clText, char *LKey, char *RKey)
{
int i = 0;
int len = 0;
int nlen = 0;
char ch = '\0';
char *key1 = LKey; /* 原始密钥, 十六进制字符串 */
char *key2 = RKey; /* 原始密钥, 十六进制字符串 */
char *key3 = LKey; /* 原始密钥, 十六进制字符串 */
// char *data = "01820003000000000000000000000000"; /* 原始明文, 十六进制字符串 */
char *data = clText;
unsigned char src[64] = {0};
unsigned char out[64] = {0};
//unsigned char tmp[64] = {0};
unsigned char *ptr = NULL;
unsigned char block[8] = {0};
DES_key_schedule ks1, ks2, ks3;
DES_cblock ivec;
DES_cblock ivsetup = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
/* 设置密码表 */
ptr = p->hex2bin(key1, strlen(key1), &nlen);
memcpy(block, ptr, sizeof(block));
free(ptr);
DES_set_key_unchecked((const_DES_cblock *)block, &ks1);
ptr = p->hex2bin(key2, strlen(key2), &nlen);
memcpy(block, ptr, sizeof(block));
free(ptr);
DES_set_key_unchecked((const_DES_cblock *)block, &ks2);
ptr = p->hex2bin(key3, strlen(key3), &nlen);
memcpy(block, ptr, sizeof(block));
free(ptr);
DES_set_key_unchecked((const_DES_cblock *)block, &ks3);
ptr = p->hex2bin(data, strlen(data), &nlen);
memcpy(src, ptr, nlen);
free(ptr);
len = (nlen / 8 + (nlen % 8 ? 1: 0)) * 8;
ch = 8 - nlen % 8;
memset(src + nlen, ch, (8 - nlen % 8) % 8);
memcpy(ivec, ivsetup, sizeof(ivsetup));
/* 按照8字节数据进行加密,length=8 */
for (i = 0; i < len; i += 8) {
DES_ede3_cbc_encrypt(src + i, out + i, 8, &ks1, &ks2, &ks3, &ivec, DES_ENCRYPT);
}
const char *result[24];
//const char *mR = new char[];
printf("加密后数据: ");
for (i = 0; i < len; i++) {
// printf("%02X" , *(out + i));
result[i] = p->bin2hex((out + i),1);
}
QString mValue;
//int valLen = strlen((char *)result);
for (int j=0;j<len;j++)
{
QString temp(result[j]);
mValue += temp;
}
printf(clText);
return mValue;
}
//3Des_ecb加密
QString QDES::m3Des_ecb_encrypt(QString clText, QString key)
{
QString mValue;
QT_TRY
{
int i = 0;
int len = 0;
int nlen = 0;
char ch = '\0';
// char *key1 = "0000000000000000"; /* 原始密钥, 十六进制字符串 */
// char *key2 = "0000000000000000"; /* 原始密钥, 十六进制字符串 */
// char *key3 = "0000000000000000"; /* 原始密钥, 十六进制字符串 */
// char *data = "12345678123456781234567812345678"; /* 原始明文, 十六进制字符串 */
QString lKey,rKey;
lKey = key.left(16);
rKey = key.right(16);
char *key1 = lKey.toLatin1().data(); /* 原始密钥, 十六进制字符串 */
char *key2 = rKey.toLatin1().data(); /* 原始密钥, 十六进制字符串 */
char *key3 = lKey.toLatin1().data(); /* 原始密钥, 十六进制字符串 */
char *data = clText.toLatin1().data(); /* 原始明文, 十六进制字符串 */
unsigned char src[64] = {0};
unsigned char out[64] = {0};
unsigned char tmp[64] = {0};
unsigned char *ptr = NULL;
unsigned char block[8] = {0};
DES_key_schedule ks1, ks2, ks3;
/* 设置密码表 */
ptr = p->hex2bin(key1, strlen(key1), &nlen);
memcpy(block, ptr, sizeof(block));
free(ptr);
DES_set_key_unchecked((const_DES_cblock *)block, &ks1);
ptr = p->hex2bin(key2, strlen(key2), &nlen);
memcpy(block, ptr, sizeof(block));
free(ptr);
DES_set_key_unchecked((const_DES_cblock *)block, &ks2);
ptr = p->hex2bin(key3, strlen(key3), &nlen);
memcpy(block, ptr, sizeof(block));
free(ptr);
DES_set_key_unchecked((const_DES_cblock *)block, &ks3);
ptr = p->hex2bin(data, strlen(data), &nlen);
memcpy(src, ptr, nlen);
free(ptr);
len = (nlen / 8 + (nlen % 8 ? 1: 0)) * 8;
ch = 8 - nlen % 8;
memset(src + nlen, ch, (8 - nlen % 8) % 8);
printf("加密前数据: ");
for (i = 0; i < len; i++) {
printf("%02X", *(src + i));
}
printf("\n");
for (i = 0; i < len; i += 8) {
DES_ecb3_encrypt((const_DES_cblock *)(src + i), (const_DES_cblock *)(out + i), &ks1, &ks2, &ks3, DES_ENCRYPT);
}
const char *result[79] ;
/*把解密后的数据转成16进制数*/
for (i = 0; i < len; i++) {
result[i] = p->bin2hex((out + i),1);
}
/*再把解密的数据装成CString*/
for (int j=0;j<len;j++)
{
QString temp(result[j]);
mValue += temp;
}
return mValue;
}
QT_CATCH(Except ex)
{
if(ex == EXCEP_ZERO) QT_RETHROW;
return mValue;
}
}
//3Des_ecb解密
QString QDES::m3Des_ecb_decrypt(QString clText, QString key)
{
QString mValue;
QT_TRY
{
int i = 0;
int len = 0;
int nlen = 0;
char ch = '\0';
// char *key1 = "0000000000000000"; /* 原始密钥, 十六进制字符串 */
// char *key2 = "0000000000000000"; /* 原始密钥, 十六进制字符串 */
// char *key3 = "0000000000000000"; /* 原始密钥, 十六进制字符串 */
// char *data = "12345678123456781234567812345678"; /* 原始明文, 十六进制字符串 */
QString lKey,rKey;
lKey = key.left(16);
rKey = key.right(16);
char *key1 = lKey.toLatin1().data(); /* 原始密钥, 十六进制字符串 */
char *key2 = rKey.toLatin1().data(); /* 原始密钥, 十六进制字符串 */
char *key3 = lKey.toLatin1().data(); /* 原始密钥, 十六进制字符串 */
char *data = clText.toLatin1().data(); /* 原始明文, 十六进制字符串 */
unsigned char src[64] = {0};
unsigned char out[64] = {0};
unsigned char tmp[64] = {0};
unsigned char *ptr = NULL;
unsigned char block[8] = {0};
DES_key_schedule ks1, ks2, ks3;
/* 设置密码表 */
ptr = p->hex2bin(key1, strlen(key1), &nlen);
memcpy(block, ptr, sizeof(block));
free(ptr);
DES_set_key_unchecked((const_DES_cblock *)block, &ks1);
ptr = p->hex2bin(key2, strlen(key2), &nlen);
memcpy(block, ptr, sizeof(block));
free(ptr);
DES_set_key_unchecked((const_DES_cblock *)block, &ks2);
ptr = p->hex2bin(key3, strlen(key3), &nlen);
memcpy(block, ptr, sizeof(block));
free(ptr);
DES_set_key_unchecked((const_DES_cblock *)block, &ks3);
ptr = p->hex2bin(data, strlen(data), &nlen);
memcpy(src, ptr, nlen);
free(ptr);
len = (nlen / 8 + (nlen % 8 ? 1: 0)) * 8;
ch = 8 - nlen % 8;
memset(src + nlen, ch, (8 - nlen % 8) % 8);
printf("加密前数据: ");
for (i = 0; i < len; i++) {
printf("%02X", *(src + i));
}
printf("\n");
for (i = 0; i < len; i += 8) {
DES_ecb3_encrypt((const_DES_cblock *)(src + i), (const_DES_cblock *)(out + i), &ks1, &ks2, &ks3, DES_DECRYPT);
}
QString mValue;
const char *result[128] ;
/*把解密后的数据转成16进制数*/
for (i = 0; i < len; i++) {
result[i] = p->bin2hex((out + i),1);
}
/*再把解密的数据装成CString*/
for (int j=0;j<len;j++)
{
QString temp(result[j]);
mValue += temp;
}
return mValue;
}
QT_CATCH(Except ex)
{
if(ex == EXCEP_ZERO) QT_RETHROW;
return mValue;
}
}
//QDes_cbc加密
QString QDES::mDes_cbc_encrypt(char *clText, char *key)
{
QString mValue;
QT_TRY
{
DES_key_schedule ks;
DES_cblock ivec = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
int i = 0;
int len = 0;
int nlen = 0;
// char *data = "848200001082CD0CACFA92293B800000"; /* 原始明文, 十六进制字符串 */
// char *okey = "D527FB5519C316C9"; /* 原始密钥, 十六进制字符串 */
char *data = clText; /* 原始明文, 十六进制字符串 */
char *okey = key; /* 原始密钥, 十六进制字符串 */ //赋值
unsigned char ch = '\0';
unsigned char *ptr = NULL;
unsigned char src[16] = {0}; /* 补齐后的明文, data补齐后的1/2长度 */
unsigned char dst[16] = {0}; /* 解密后的明文, data补齐后的1/2长度 */
unsigned char block[8] = {0};
/* 设置密码表 */
ptr = p->hex2bin(okey, strlen(okey), &nlen);
memcpy(block, ptr, 8);
free(ptr);
DES_set_key_unchecked((const_DES_cblock*)block, &ks);
/* 分析补齐明文所需空间及补齐填充数据 */
len = strlen((char *)data);
ptr = p->hex2bin(data, len, &nlen);
len = (nlen / 8 + (nlen % 8 ? 1: 0)) * 8;
memcpy(src, ptr, len);
free(ptr);
ch = 8 - nlen % 8;
memset(src + nlen, ch, 8 - nlen % 8);
printf("加密之前: ");
for (i = 0; i < len; i++) {
printf("%.2X", *(src + i));
}
printf("\n");
/* 加密块链式加密 */
DES_ncbc_encrypt(src, dst, sizeof(src), &ks, &ivec, DES_ENCRYPT);
// unsigned char *result[16] ;
const char *result[36] ;
printf("加密之后: ");
int j=0;
/*把解密后的结果转成16进制数*/
for (i = 0; i < len; i++) {
result[i] = p->bin2hex((dst + i),1);
}
/*再把解密的数据转成CString*/
// int valLen = strlen((char *)result);
for (int j=0;j<len;j++)
{
QString temp(result[j]);
mValue += temp;
}
free(src);
free(dst);
return mValue;
}
QT_CATCH(Except ex)
{
if(ex == EXCEP_ZERO) QT_RETHROW;
return mValue;
}
}
//QDes_cbc解密
QString QDES::mDes_cbc_decrypt(char *clText, char *key)
{
QString mValue;
QT_TRY
{
DES_key_schedule ks;
DES_cblock ivec = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
int i = 0;
int len = 0;
int nlen = 0;
// char *data = "848200001082CD0CACFA92293B800000"; /* 原始明文, 十六进制字符串 */
// char *okey = "D527FB5519C316C9"; /* 原始密钥, 十六进制字符串 */
char *data = clText; /* 原始明文, 十六进制字符串 */
char *okey = key; /* 原始密钥, 十六进制字符串 */ //赋值
unsigned char ch = '\0';
unsigned char *ptr = NULL;
unsigned char src[16] = {0}; /* 补齐后的明文, data补齐后的1/2长度 */
unsigned char dst[16] = {0}; /* 解密后的明文, data补齐后的1/2长度 */
unsigned char block[8] = {0};
/* 设置密码表 */
ptr = p->hex2bin(okey, strlen(okey), &nlen);
memcpy(block, ptr, 8);
free(ptr);
DES_set_key_unchecked((const_DES_cblock*)block, &ks);
/* 分析补齐明文所需空间及补齐填充数据 */
len = strlen((char *)data);
ptr = p->hex2bin(data, len, &nlen);
len = (nlen / 8 + (nlen % 8 ? 1: 0)) * 8;
memcpy(src, ptr, len);
free(ptr);
ch = 8 - nlen % 8;
memset(src + nlen, ch, 8 - nlen % 8);
printf("加密之前: ");
for (i = 0; i < len; i++) {
printf("%.2X", *(src + i));
}
printf("\n");
/* 加密块链式加密 */
DES_ncbc_encrypt(src, dst, sizeof(src), &ks, &ivec, DES_DECRYPT);
// unsigned char *result[16] ;
const char *result[128] ;
printf("加密之后: ");
/*把解密后的结果转成16进制数*/
for (i = 0; i < len; i++) {
result[i] = p->bin2hex((dst + i),1);
}
/*再把解密的数据装成CString*/
// int valLen = strlen((char *)result);
for (int j=0;j<len;j++)
{
QString temp(result[j]);
mValue += temp;
}
// free(src);
free(dst);
return mValue;
}
QT_CATCH(Except ex)
{
if(ex == EXCEP_ZERO) QT_RETHROW;
return mValue;
}
}
DES.h文件
#ifndef QDES_H
#define QDES_H
#include <QMainWindow>
#include <QObject>
#include <QWidget>
#include <openssl/ssl.h>
#include <openssl/sha.h>
#include <openssl/aes.h>
#include <openssl/des.h>
#include <openssl/hmac.h>
#include "HEX.h"
class QDES
{
public:
QDES(void);
~QDES(void);
QString m3Des_cbc_encrypt(char *clText,char *key1,char *key2,char *key3);
QString m3Des_cbc_nokey_encrypt(char *clText,char *LKey,char *RKey);
QString m3Des_ecb_encrypt(QString clText,QString key);
QString m3Des_ecb_decrypt(QString clText,QString key);
QString mDes_cbc_encrypt(char *clText,char *key);
QString mDes_cbc_decrypt(char *clText,char *key);
QHEX *p = new QHEX;
};
#endif // QDES_H
标签:10,DES,09,nlen,char,2021,data,ptr,block 来源: https://blog.csdn.net/weixin_45976607/article/details/120225790