http://idsips.blog.163.com/blog/static/480012722012398048394/
http://blog.sina.com.cn/s/blog_608a9fa70100dk6x.html
AES_cbc_encrypt and Perl Crypt::CBC Incompatibility
hex2bin bin2hex
http://www.opensubscriber.com/message/[email protected]/10974596.html
openssl 1.0.1
http://fossies.org/dox/openssl-1.0.1c/aes__misc_8c_source.html
AES 加密解密实例
http://blog.chinaunix.net/uid-26553782-id-3087173.html
#include
#include
#include "stdafx.h"
#include
#define INTERFACE"eth0"
#define PAD_SIZE 32
/*
*需要密钥key[16+1],向量iv1[16+1],明文pt1[]
*/
void hextoasii(unsignedchar *bytes,int byteNum,char *strAsii);
//----------------------------------------------------------
int aes_test(void)
{
unsigned char key[]= ")*^&$*kdjfkdjfjdsjfska!*&^%$#@";// 128bits key (应该是真正的随机数才好)
char pt1[]= "aaaaaaaaaaaaaaaaab";// 明文, 16位的时候 成功。
pt1[5] = 0x0;
char ct[1000]= {0};// 密文
char pt2[1000]={0};// 解密后的明文
AES_KEY k;
int ilen=sizeof(pt1);
int ctLen =(ilen%16==0)?ilen:(ilen/16+1)*16;
//|bc~!f947j*$m_op
unsigned char iv1[16+1]= {"|bc~!f947j*$m_op"};// 16+1,加密用
unsigned char iv2[16+1]= {"|bc~!f947j*$m_op"};// 16+1,解密用
int i,j;
AES_set_encrypt_key(key, 16*16,&k);
/*
void AES_cbc_encrypt(
const unsigned char *in,
unsigned char *out,
const unsigned long length, //in 的长度
const AES_KEY *key, //it will be changed during the encrypt&decrypt process, so it required to be reset each time
unsigned char *ivec, //向量
const int enc);
*/
AES_cbc_encrypt((unsignedchar*)pt1,(unsigned char*)ct, ilen,&k, (unsigned char*)iv1, AES_ENCRYPT);
printf("encrypt:%s\n", ct);
FILE *fp1= fopen("aes_encrypt.txt","w");
fwrite(ct,sizeof(unsignedchar),ctLen, fp1);
fflush(fp1);
char strAsii[1000]={0};
hextoasii((unsignedchar*)ct, ctLen, strAsii);
printf("\n%s\n", strAsii);
//memset(pt2, 0, 33);
AES_set_decrypt_key(key, 16*16,&k);
AES_cbc_encrypt((unsignedchar*)ct,(unsigned char*)pt2, ctLen,&k, (unsigned char*)iv2, AES_DECRYPT);
printf("before: %s\n", pt1);
printf("after : %s\n", pt2);
if (memcmp(pt1, pt2, ilen)==0)
puts("AES CBC mode ok");
else puts("AES CBC mode err");
return 0;
}
//-----------------------------------------------------------
char* AESDecrypt(
char* strEncryptDes,
const char* strUnEncryptSrc
)
{
unsigned char cipher[10000]= {0};// 密文
unsigned char iv1[16+1]= {"|bc~!f947j*$m_op"};// 16+1,向量
unsigned char strEncryptKey[]= ")*^&$*kdjfkdjfjdsjfska!*&^%$#@";// 128bits key (应该是真正的随机数才好)
AES_KEY k;
int ilen=strlen(strUnEncryptSrc);
int ctLen = ilen/2;
int i=0, nCipherHex=0;
char strAsii[5];
for(i=0; i= AES_BLOCK_SIZE) {
for(n=0; n < AES_BLOCK_SIZE; ++n)
out[n] = in[n] ^ iv[n];
AES_encrypt(out, out, key);
iv = out;
len -= AES_BLOCK_SIZE;
in += AES_BLOCK_SIZE;
out += AES_BLOCK_SIZE;
}
if (len) {
for(n=0; n < len; ++n)
out[n] = in[n] ^ iv[n];
for(n=len; n < AES_BLOCK_SIZE; ++n)
out[n] = iv[n];
AES_encrypt(out, out, key);
iv = out;
}
memcpy(ivec,iv,AES_BLOCK_SIZE);
} else if (in != out) {
while (len >= AES_BLOCK_SIZE) {
AES_decrypt(in, out, key);
for(n=0; n < AES_BLOCK_SIZE; ++n)
out[n] ^= iv[n];
iv = in;
len -= AES_BLOCK_SIZE;
in += AES_BLOCK_SIZE;
out += AES_BLOCK_SIZE;
}
if (len) {
AES_decrypt(in,tmp,key);
for(n=0; n < len; ++n)
out[n] = tmp[n] ^ iv[n];
iv = in;
}
memcpy(ivec,iv,AES_BLOCK_SIZE);
} else {
while (len >= AES_BLOCK_SIZE) {
memcpy(tmp, in, AES_BLOCK_SIZE);
AES_decrypt(in, out, key);
for(n=0; n < AES_BLOCK_SIZE; ++n)
out[n] ^= ivec[n];
memcpy(ivec, tmp, AES_BLOCK_SIZE);
len -= AES_BLOCK_SIZE;
in += AES_BLOCK_SIZE;
out += AES_BLOCK_SIZE;
}
if (len) {
memcpy(tmp, in, AES_BLOCK_SIZE);
AES_decrypt(tmp, out, key);
for(n=0; n < len; ++n)
out[n] ^= ivec[n];
for(n=len; n < AES_BLOCK_SIZE; ++n)
out[n] = tmp[n];
memcpy(ivec, tmp, AES_BLOCK_SIZE);
}
}
}