openssl AES加密


此代码不涉及ECB和CBC等关联加密


#include <stdio.h>  
#include <string.h>  
#include <stdlib.h> 

#include <openssl/aes.h>

#include <openssl/applink.c>  
#pragma comment(lib, "libeay32.lib")  
#pragma comment(lib, "ssleay32.lib") 

int main ()
{
	//aes是块加密,每块16Byte(128bit)
	char input[] = "0000000000000000";
	unsigned char output[256];
	unsigned  char decode[64];
	//密钥,长度可选3种128bit,192bit,256bit,不管密钥长度多长加密出来的结果都是128bit
	//这里用的是128bit的密钥长度
	char userKey[]="1234567890123456";
	AES_KEY key;

	AES_set_encrypt_key((unsigned char*)userKey, 128, &key);//构造加密密钥

	AES_encrypt((unsigned char *)input, output, &key);

	//30303030303030303030303030303030 加密结果: 93bc0fabf6c85e9e1c53d78885373dc7
	
	AES_encrypt((unsigned char *)input,output,&key);

	AES_set_decrypt_key((unsigned char*)userKey, 128, &key);//构造解密密钥
	AES_decrypt(output,decode,&key);

	return 0;
}



posted on 2016-10-21 15:12  阮減显  阅读(215)  评论(0编辑  收藏  举报

导航