Openssl aes加解密例程
原文链接: http://blog.csdn.net/itmes/article/details/7714854
假设我们已经下载了 openssl的源码,并成功编译,设置好了编程环境。
我们现在来看看如何调用openssl的API。
openssl对aes加密算法进行了良好的封装,调用接口简单明了,见如下例程
#include <memory.h>
#include <stdio.h>
#include <stdlib.h>
#include <openssl/aes.h>
#pragma comment(lib,"libeay32.lib")
int main(int argc, char **argv)
{
unsigned char buf[16];
memset(buf,1,sizeof(buf));
unsigned char buf2[16];
unsigned char buf3[16];
//测试用的key是全零
unsigned char aes_keybuf[32];
memset(aes_keybuf,0,sizeof(aes_keybuf));
AES_KEY aeskey;
AES_set_encrypt_key(aes_keybuf,256,&aeskey);
AES_encrypt(buf,buf2,&aeskey);
AES_set_decrypt_key(aes_keybuf,256,&aeskey);
AES_decrypt(buf2,buf3,&aeskey);
if(memcmp(buf,buf3,sizeof(buf))==0)
printf("test success\r\n");
else
printf("test fail\r\n");
}