当前位置:网站首页>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
边栏推荐
- 【数据库】MySQL单表查询
- ApplicationContext injection bean
- Leetcode004 -- Roman numeral to integer
- What is a blocking queue? What is the implementation principle of blocking queue? How to use blocking queue to implement producer consumer model?
- Innovation training (V) configuration information
- Eight misunderstandings that should be avoided in data visualization
- Recommended scheme for national production of electronic components for wireless charging
- AQS source code reading
- COM in wine (2) -- basic code analysis
- Simply drag objects to the item bar
猜你喜欢

Arduino UNO r3+LCD1602+DHT11
![View, modify and delete [database] table](/img/a2/fcb38f2006772a1ec45cab520620ba.png)
View, modify and delete [database] table

Customize the navigation bar at the top of wechat applet (adaptive wechat capsule button, flex layout)

【数据库】MySQL基本操作(基操~)

Recommended scheme of national manufactured electronic components for intelligent electronic scales

MySQL -- execution process and principle of a statement

【数据库】MySQL多表查询(一)

【数据库】MySQL单表查询

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

Spark small case - RDD, spark SQL
随机推荐
Analysis of POM files
2022/4/22
Innovation training (10)
Solutions to the failure of sqoop connection to MySQL
selenium模式下切换窗口,抓取数据的实现
Flink's important basics
The last day of 2021 is the year of harvest.
js 判斷數字字符串中是否含有字符
Shanghai Hangxin technology sharing 𞓜 overview of safety characteristics of acm32 MCU
AQS源码阅读
使用model.load_state_dict()时,出现AttributeError: ‘str‘ object has no attribute ‘copy‘
[winui3] write an imitation Explorer file manager
Spell it! Two A-level universities and six B-level universities have abolished master's degree programs in software engineering!
Innovation training (XI) airline ticket crawling company information
Case of using stream load to write data to Doris
What is a data island? Why is there still a data island in 2022?
Learning Android from scratch -- Introduction
AQS source code reading
Last day of 2017
CLion+OpenCV identify ID number - detect ID number