long blogs

进一步有进一步惊喜


  • Home
  • Archive
  • Tags
  •  

© 2025 long

Theme Typography by Makito

Proudly published with Hexo

C-AES加解密

Posted at 2020-09-06 C 加解密 

什么是AES

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
void printData(uint8_t *data,int32_t len){
int j = 1;
for (int i=0;i<len;i++){
printf("%2x ",(uint8_t)data[i]);
if(j % 16 == 0){
printf("\n");
}
j++;
}
}
// 加密数据
void encryptConfigFile(){
char *filename = "./long_plain.txt";
char content[1024*10];
char encrypt[1024*10];
size_t filesize;
FILE *fp = fopen(filename,"r");
filesize = fread(content,1,1024*10,fp);
content[filesize] = '\0';
printf("\n=========================\n");
printf("file content:\n%s",content);
printf("\n=========================\n");
printf("\n 文件大小: %lu %lu\n",filesize,strlen(content));
printf("\n==============从文件读取到的数据===================\n");
printData(content,filesize);
printf("\n================================================\n");
// 加密数据
uint8_t *dataforencrypt = (uint8_t *)calloc(1,strlen(content)+AES_BLOCK_SIZE);
if (NULL == dataforencrypt){
dbg("无法分配内存");
return;
}
memcpy(dataforencrypt,content,strlen(content));
size_t padding_len = strlen(content);
pkcs5padding((uint8_t * const)dataforencrypt,(size_t * const)&padding_len,AES_BLOCK_SIZE);
size_t encrypt_len = 0;
char key[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
char iv[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};

if (!aes_cbc_encrypt(dataforencrypt,padding_len,encrypt,&encrypt_len,key,sizeof(key),iv,sizeof(iv))){
dbg("加密失败");
}
fclose(fp);
printf("\n================加密后数据==========\n");
printData(encrypt,encrypt_len);
printf("\n===================================\n");

fp = fopen("long_encrypt.txt","w+");
// 第一个字节写入2,后面是加密数据。然后写入文件
uint8_t *write_data = NULL;
write_data = calloc(1,encrypt_len+1);
write_data[0] = 2;
uint8_t *tmp = &write_data[1];
memcpy(tmp,encrypt,encrypt_len);
fwrite(write_data,encrypt_len+1,1,fp);
printf("\n===============写入文件数据==============\n");
printData(write_data,encrypt_len+1);
printf("\n===========================================\n");
printf("\n");
printf("加密写入文件成功\n");
fclose(fp);
free(write_data);
}
// 解密文件
void decryptConfigFile(){
char *filename = "./long_encrypt.txt";
uint8_t content[10*1024];
size_t filesize;
FILE *fp = fopen(filename,"r");
filesize = fread(content,1,1024*10,fp);
// 取出第一个字节,
uint8_t version = content[0];
printf("version = %d\n",version);
printf("\n=============读取到文件内容=============\n");
printData(content,filesize);
printf("\n========================================\n");
// 获得待解密的数据
uint8_t *decrypt_data = NULL;
uint8_t *tmp = &content[1];
decrypt_data = calloc(1,filesize-1);
// 读出后面的数据
memcpy(decrypt_data,tmp,filesize-1);
char key[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
char iv[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
//
printf("\n=========待解密数据============\n");
printData(decrypt_data,filesize-1);
printf("\n==============================\n");
// 解密
uint8_t decrypt[10*1024];
size_t decrypt_len;
if (!aes_cbc_decrypt(decrypt_data,filesize-1,decrypt,&decrypt_len,key,sizeof(key),iv,sizeof(iv))){
dbg("解密失败\n");
return;
}
dbg("\n===========解密后的数据================\n");
printData(decrypt,decrypt_len);
printf("\n====================================\n");
pkcs5unpadding(decrypt,&decrypt_len);
printf("\n===========unpadding之后的数据==========================\n");
printData(decrypt,decrypt_len);
printf("\n===============================================\n");

decrypt[decrypt_len] = '\0';
printf("解密后数据:\n%s\n",decrypt);
fclose(fp);
fp = fopen("long_decrypt.txt","w+");
fwrite(decrypt,decrypt_len,1,fp);
fclose(fp);
printf("\n解密后数据写入文件成功\n");
}

Share 

 Previous post: linux自动更换壁纸 Next post: C-宏注入及其应用 

© 2025 long

Theme Typography by Makito

Proudly published with Hexo