当前位置:网站首页>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 -- execution process and principle of a statement
- Wechat payment function
- Leetcode003 -- judge whether an integer is a palindrome number
- Learning Android from scratch -- baseactivity and activitycollector
- Solve valueerror: argument must be a deny tensor: 0 - got shape [198602], but wanted [198602, 16]
- PHP+MySQL 制作留言板
- Field injection is not recommended using @ Autowired
- 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
- js 判断数字字符串中是否含有字符
猜你喜欢
Details related to fingerprint payment
2022/4/22
持续集成(CI)/持续交付(CD)如何彻底改变自动化测试
MySQL -- execution process and principle of a statement
Set Chrome browser background to eye protection (eye escort / darkreader plug-in)
Eight misunderstandings that should be avoided in data visualization
PHP+MySQL 制作留言板
PIP3 installation requests Library - the most complete pit sorting
使用model.load_state_dict()时,出现AttributeError: ‘str‘ object has no attribute ‘copy‘
Innovation training (IV) preliminary preparation - server
随机推荐
Eight misunderstandings that should be avoided in data visualization
Unity3d practical skills - theoretical knowledge base (I)
Windows remote connection to redis
Excel uses the functions of replacement, sorting and filling to comprehensively sort out financial data
[WinUI3]編寫一個仿Explorer文件管理器
Getprop property
What's the difference between error and exception
Innovation training (IV) preliminary preparation - server
泰克示波器DPO3054自校准SPC失败维修
【数据库】MySQL多表查询(一)
Pixel mobile phone brick rescue tutorial
Progress of innovation training (IV)
Recommended scheme of national manufactured electronic components
redis和mysql区别
Informatics Aosai yibentong 1212: letters | openjudge 2.5 156: Letters
Informatics Olympiad 1955: [11noip popularization group] Swiss round | openjudge 4.1 4363: Swiss round | Luogu p1309 [noip2011 popularization group] Swiss round
Leetcode006 -- find the longest common prefix in the string array
Code007 -- determine whether the string in parentheses matches
Simply drag objects to the item bar
js 判斷數字字符串中是否含有字符