69,373
社区成员
发帖
与我相关
我的任务
分享
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <openssl/aes.h>
void main(){
char *key_string = "123456";
AES_KEY aes;
int i = 0;
char * out1 = malloc(16);
char * out2 = malloc(16);
memset(out1,0,16);
memset(out2,0,16);
if (AES_set_encrypt_key(key_string, 128, &aes) < 0) {
fprintf(stderr, "Unable to set encryption key in AES\n");
exit(-1);
}
char temp[16] = "ABCDEDDFDSGFRSFG";
AES_encrypt(temp,out1,&aes);
for(i= 0;i < 16;i+=2){
printf("%x",out1[i]);
}
printf("\n");
AES_decrypt(out1,out2,&aes);
printf("temp=%s\n,out2 = %s\n",temp,out2);
}
void main()
{
char *key_string = "123456";
AES_KEY aes;
int i = 0;
unsigned char out1[16];
unsigned char out2[16];
memset(out1,0,16);
memset(out2,0,16);
if (AES_set_encrypt_key((unsigned char*)key_string, 128, &aes) < 0) {
fprintf(stderr, "Unable to set encryption key in AES\n");
exit(-1);
}
char temp[16] = "ABCDEDDFDSGFRSF";
AES_encrypt((unsigned char*)temp,out1,&aes);
for(i= 0;i < 16;i+=2){
printf("%x",out1[i]);
}
printf("\n");
if (AES_set_decrypt_key((unsigned char*)key_string, 128, &aes) < 0) {
fprintf(stderr, "Unable to set encryption key in AES\n");
exit(-1);
}
AES_decrypt(out1,out2,&aes);
printf("temp=%s\nout2=%s\n",temp,out2);
}
兄台,你没发现你少了点什么吗?
解码虽然用同一个密钥,但是要生成解码密钥才行,你少调用了这个:AES_set_decrypt_key
另外,楼上说你溢出也是对的,上面的代码,我跑过了,OK!