当前位置:网站首页>Use AES encryption - reuse the wisdom of predecessors
Use AES encryption - reuse the wisdom of predecessors
2022-04-23 04:52:00 【Dream full stack program ape】
Use AES encryption - Reuse the wisdom of predecessors
One . The header file is as follows :
uint32_t encrypt_get_size(uint32_t size);
int32_t encrypt(uint8_t* data,
uint32_t data_size,
uint8_t* enc_data,
uint32_t* enc_data_size);
int32_t decrypt(uint8_t* enc_data,
uint32_t enc_data_size,
uint8_t** data,
uint32_t* data_size);
Two . Encryption code :
AES Algorithm to 16 Byte alignment , If not 16 Bytes , It needs to be supplemented ,
ad locum AES_BLOCK_SIZE Namely 16, stay openssl/aes.h It has a definition
typedef struct
{
uint32_t data_size;
uint32_t key[AES_BLOCK_SIZE];
uint32_t iv[AES_BLOCK_SIZE];
uint8_t data[AES_BLOCK_SIZE];
}
uint32_t encrypt_get_size(uint32_t size)
{
uint32_t block_num = 0;
uint32_t en_data_size = 0;
if ((size % AES_BLOCK_SIZE) != 0)
{
block_num = size / AES_BLOCK_SIZE + 1;
en_data_size = block_num * AES_BLOCK_SIZE;
}
else
{
en_data_size = size;
}
}
int32_t encrypt(uint8_t* data,
uint32_t data_size,
uint8_t* enc_data,
uint32_t* enc_data_size)
{
AES_KEY key;
int ret = 0;
uint32_t length = 0;
uint32_t block_num = 0;
enc_data_t* obj = NULL;
obj = (enc_data_t*) enc_data;
obj->data_len = data_size;
memcpy(obj->data, data, data_size);
ret = AES_set_encrypt_key(obj->key, 128, &key);
if (ret != 0)
{
printf("AES_set_encrypt_key failed %d \n", ret);
return ret;
}
if ((data_size % AES_BLOCK_SIZE) != 0)
{
block_num = data_size / AES_BLOCK_SIZE + 1;
length = block_num * AES_BLOCK_SIZE;
}
else
{
length = data_size;
}
AES_cbc_encrypt(obj->data, obj->data, length, &key, obj->iv, AES_ENCRYPT);
*enc_data_size = sizeof(enc_data_t) + length;
return ret;
}
3、 ... and . Decryption code :
int decrypt(uint8_t* enc_data,
uint32_t enc_data_size,
uint8_t** data,
uint32_t* data_size)
{
AES_KEY key;
int ret = 0;
enc_data_t* obj = NULL;
uint8_t* tmp_data = NULL;
obj = (enc_data_t*) enc_data;
const uint32_t total_size = obj->data_len + sizeof(enc_data_t);
ret = AES_set_decrypt_key(obj->key, 128, &key);
if (ret != 0)
{
printf("AES_set_encrypt_key failed %d \n", ret);
return ret;
}
AES_cbc_encrypt(obj->data,
obj->data,
(enc_data_size - sizeof(enc_data_t)),
&key,
obj->iv,
AES_DECRYPT);
tmp_data = (uint8_t*) malloc(obj->data_len);
if (NULL == tmp_data)
{
printf("allocate failed \n");
return -1;
}
memset(tmp_data, 0, obj->data_len);
memcpy(tmp_data, obj->data, obj->data_len);
if (NULL == *data)
{
*data = tmp_data;
}
*data_size = obj->data_len;
return 0;
}
Four . Intelligent analysis of code :
Here's a question , If encryption and decryption are in different scenarios , That is to say, you need to save the encrypted data into a file ,
then , From this secret document , Decrypt the data ; But the problem here is , You need to know before encryption
The size of the data , Some people say , This can be saved in a global variable ; But there will be such a scene , You may be in
When starting up, you need to load the data of this ciphertext, get and decrypt , What to do at this time ? The average person's ending method is as follows :
When saving encrypted data , Save two pieces of data in the form of array append , The first segment of data is data with a fixed length (4 Bytes ), To store the length of the original data , The second segment of data is the encrypted secret text data ; This is also the case when loading and parsing encrypted data. It is divided into two steps , The first step is to read a fixed length (4 Bytes ) The data of , Is the size of the original data , That is, the size of the decrypted data , The second step , Is to read the saved secret text . This method is also feasible , It's just a hassle . In fact, a good solution is to use the above method , Define a data structure internally , Do this by parsing fixed data structures
版权声明
本文为[Dream full stack program ape]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220554572081.html
边栏推荐
- 用LCR表完美测试无线充电系统中的线圈
- 信息学奥赛一本通 1955:【11NOIP普及组】瑞士轮 | OpenJudge 4.1 4363:瑞士轮 | 洛谷 P1309 [NOIP2011 普及组] 瑞士轮
- Innovation training (V) mid term inspection
- What is the meaning of load balancing
- 负载均衡简介
- Innovation training (VII) FBV view & CBV view
- The object needs to add additional attributes. There is no need to add attributes in the entity. The required information is returned
- Installation and deployment of Flink and wordcount test
- Leetcode002 -- inverts the numeric portion of a signed integer
- getprop 属性
猜你喜欢

Shanghai Hangxin technology sharing 𞓜 overview of safety characteristics of acm32 MCU

Opencv + clion face recognition + face model training

Innovation training (IV) preliminary preparation - server
![[winui3] write an imitation Explorer file manager](/img/3e/62794f1939da7f36f7a4e9dbf0aa7a.png)
[winui3] write an imitation Explorer file manager

使用model.load_state_dict()时,出现AttributeError: ‘str‘ object has no attribute ‘copy‘

View analysis of scenic spots in ArcGIS

DIY is an excel version of subnet calculator

泰克示波器DPO3054自校准SPC失败维修

AQS源码阅读

DIY 一个 Excel 版的子网计算器
随机推荐
Opencv + clion face recognition + face model training
[WinUI3]編寫一個仿Explorer文件管理器
Detailed explanation of the differences between TCP and UDP
C language: spoof games
Learning Android V from scratch - UI
General enumeration constant class
Innovation training (II) task division
redis数据类型有哪些
【数据库】MySQL多表查询(一)
Arduino UNO r3+LCD1602+DHT11
泰克示波器DPO3054自校准SPC失败维修
Innovation training (V) configuration information
Innovation training (IV) preliminary preparation - server
独立站运营 | FaceBook营销神器——聊天机器人ManyChat
Record the ThreadPoolExecutor main thread waiting for sub threads
Unity3D 实用技巧 - 理论知识库(一)
Excel uses the functions of replacement, sorting and filling to comprehensively sort out financial data
AQS源码阅读
Customize the navigation bar at the top of wechat applet (adaptive wechat capsule button, flex layout)
Alibaba tip: it is better to create threads manually