AES加密教程
说明:主要参考:https://blog.csdn.net/nanfeibuyi/article/details/125474445 的文章,然后添加到QT中实现了AES加密和MD5的校验。为了防止自己忘记所以还是把代码黏贴出来。
实例代码
demo_aes.h
//基于QT做的DEMO
#ifndef DEMO_AES_H
#define DEMO_AES_H
#include <QMainWindow>
#include <string.h>
#include <ctype.h> //isprint
#include <QDebug>
#include <stdio.h>
#include <string.h>
#include "MD5.h"
#include "aes.h"
QT_BEGIN_NAMESPACE
namespace Ui {
class DEMO_AES; }
QT_END_NAMESPACE
class DEMO_AES : public QMainWindow
{
Q_OBJECT
public:
DEMO_AES(QWidget *parent = nullptr);
~DEMO_AES();
//结构体
typedef struct EEPROM_HANDLE
{
int InitNumber ; //可以使用次数
int InitMode ; //型号 1 2 3 4
char Description[64] ; //描述字符42 www.zhitangtech.com"
int InitState ; //初始化标志
int CRCDate ; //CRC
}EEPROMStruct;
EEPROMStruct EepromDate; //eeprom结构体
EEPROMStruct EepromDate_encrypt; //eeprom结构体 加密后数据
EEPROMStruct EepromDate_decrypt; //eeprom结构体 解密数据
AESInfo_t aesInfo; //设置加密方式、密匙
//初始化向量, 固定长度16个, 当mode=AES_MODE_CBC时用到
unsigned char IV[4*Nb] = {
0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x30,0x41,0x42,0x43,0x44,0x45,0x46};
unsigned char key[16] = {
0}; //key
/**********************************************************************
** 函数名称: AES_Init()
** 功能描述: AES 初始化
** 输 入: 无
** 输 出: 无
** 函数说明:
***********************************************************************/
void AES_Init(void);
/**********************************************************************
** 函数名称: AES_Encrypt()
** 功能描述: AES加密数据
** 输 入: 无
** 输 出: 无
** 函数说明:
***********************************************************************/
void AES_Encrypt(unsigned char *sourceMsg ,unsigned char * encryptMsg);
/**********************************************************************
** 函数名称: AES_Decrypt()
** 功能描述: AES解密数据
** 输 入: 无
** 输 出: 无
** 函数说明:
***********************************************************************/
void AES_Decrypt(unsigned char *encryptMsg,unsigned char *decryptMsg);
/**********************************************************************
** 函数名称: Comm_Crc16ValueCal()
** 功能描述: CRC校验
** 输 入: 无
** 输 出: 无
** 函数说明:
***********************************************************************/
uint16_t Comm_Crc16ValueCal(const uint8_t* data , uint16_t length);
private:
Ui::DEMO_AES *ui;
void PrintData(const char *head, unsigned char *data, unsigned int len);
};
#endif // DEMO_AES_H
demo_aes.cpp
#include "demo_aes.h"
#include "ui_demo_aes.h"
DEMO_AES::DEMO_AES(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::DEMO_AES)
{
ui->setupUi(this);
// AES_Init();
unsigned int len;
char ZT_Mes[] = "www.baidu.com"; //特有信息
char ZT_ID[] = "12783678f8e7a7902189231618161730"; //唯一ID
strcat(ZT_Mes,ZT_ID);
//秘钥,根据实际情况自己定义,AES128 用16字节、AES192 用24字节、AES256 用32字节 //唯一ID+智瑭科技特有信息生成 key
// PrintData("ZT_Mes", (unsigned char*)ZT_Mes, strlen((char *)ZT_Mes));
// unsigned char key[16] = {0};
MD5((unsigned char *)ZT_Mes,strlen(ZT_Mes),(unsigned char *)key);
// for(int i = 0 ; i < 16 ; i++)printf("%02x ",key[i]);
//初始化向量, 固定长度16个, 当mode=AES_MODE_CBC时用到
// unsigned char IV[4*Nb] = {0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x30,0x41,0x42,0x43,0x44,0x45,0x46};
//要加密的内容
unsigned char sourceMsg[] = "Hdedeeeello AES 128/192/256";
char data[64] = "www.baidu.com";
EepromDate.InitState = 1;
EepromDate.InitNumber = 300;
EepromDate.InitMode = 4;
strcpy(EepromDate.Description,data) ; //使用字符函数复制
EepromDate.CRCDate = Comm_Crc16ValueCal((uint8_t *)&EepromDate,sizeof( EepromDate) );
PrintData("CRCDate", (unsigned char *)&EepromDate.CRCDate, sizeof( EepromDate.CRCDate));
//加密后的内容
unsigned char encryptMsg[sizeof(sourceMsg)+16] = {
0};
//解密后的内容
unsigned char decryptMsg[sizeof(sourceMsg)+16] = {
0};
//设置加密方式、密匙
AESInfo_t aesInfo;
// = {
// .type = AES128,
// .mode = AES_MODE_CBC,
// .key = key,
// .pIV = IV
// };
aesInfo.type = AES128;
aesInfo.mode = AES_MODE_CBC;
aesInfo.key = key;
aesInfo.pIV = IV;
printf("Build %s %s\r\n", __DATE__, __TIME__); //编译时间
PrintData("sourceMsg", (unsigned char *)&EepromDate, sizeof( EepromDate));
// //初始化
AESInit(&aesInfo);
// AES_Init();
//加密
// AES_Encrypt(sourceMsg,encryptMsg);
len = AESEncrypt(&aesInfo, (unsigned char *)&EepromDate, (unsigned char *)&EepromDate_encrypt, sizeof( EepromDate));
PrintData("encryptMsg", (unsigned char *)&EepromDate_encrypt, sizeof(EepromDate_encrypt));
//解密
// AES_Decrypt(encryptMsg,decryptMsg);
len = AESDecrypt(&aesInfo, (unsigned char *)&EepromDate_decrypt, (unsigned char *)&EepromDate_encrypt, len);
PrintData("decryptMsg", (unsigned char *)&EepromDate_decrypt, sizeof( EepromDate_decrypt));
fflush(stdout); //刷新缓冲区
}
DEMO_AES::~DEMO_AES()
{
delete ui;
}
void DEMO_AES::AES_Init()
{
//设置加密方式、密匙
aesInfo.type = AES128;
aesInfo.mode = AES_MODE_CBC;
aesInfo.key = key;
aesInfo.pIV = IV;
//初始化
AESInit(&aesInfo);
}
void DEMO_AES::AES_Encrypt( unsigned char *sourceMsg, unsigned char *encryptMsg)
{
//加密
AESEncrypt(&aesInfo, sourceMsg, encryptMsg, sizeof(sourceMsg));
}
void DEMO_AES::AES_Decrypt( unsigned char *encryptMsg, unsigned char *decryptMsg)
{
//解密
AESDecrypt(&aesInfo, decryptMsg, encryptMsg, sizeof(encryptMsg) );
}
uint16_t DEMO_AES::Comm_Crc16ValueCal(const uint8_t *data, uint16_t length)
{
uint16_t crcValue = 0xFFFF;
uint16_t i;
wh