当前位置:网站首页>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
边栏推荐
- Leetcode006 -- find the longest common prefix in the string array
- AQS源码阅读
- 跨境电商 | Facebook 和 Instagram:哪个社交媒体更适合你?
- 简单的拖拽物体到物品栏
- COM in wine (2) -- basic code analysis
- Spell it! Two A-level universities and six B-level universities have abolished master's degree programs in software engineering!
- Wine (COM) - basic concept
- Other problems encountered in debugging fingerprints
- selenium模式下切换窗口,抓取数据的实现
- List< Map> Replication: light copy and deep copy
猜你喜欢
Sword finger offer: the median in the data stream (priority queue large top heap small top heap leetcode 295)
Spark FAQ sorting - must see before interview
Wechat payment function
跨境电商 | Facebook 和 Instagram:哪个社交媒体更适合你?
Installation and deployment of Flink and wordcount test
C language: spoof games
Practice and exploration of knowledge map visualization technology in meituan
CLion+OpenCV identify ID number - detect ID number
Flink's important basics
View, modify and delete [database] table
随机推荐
Record the blind injection script
C language: spoof games
Recommended scheme of national manufactured electronic components
Alibaba tip: it is better to create threads manually
Special topic of data intensive application system design
AQS source code reading
Mac 进入mysql终端命令
Recommended scheme for national production of electronic components of wireless keyboard
leetcode002--将有符号整数的数字部分反转
Innovation training (VII) FBV view & CBV view
Spell it! Two A-level universities and six B-level universities have abolished master's degree programs in software engineering!
Sword finger offer: push in and pop-up sequence of stack
getprop 属性
unity摄像机旋转带有滑动效果(自转)
Unity3D 实用技巧 - 理论知识库(一)
How can continuous integration (CI) / continuous delivery (CD) revolutionize automated testing
Innovation training (V) mid term inspection
redis和mysql区别
What is the meaning of load balancing
La caméra Unity tourne avec la souris