AES 加密解密API
来源:互联网 发布:java随机数生成1到10 编辑:程序博客网 时间:2024/05/22 01:03
linux下AES-CBC128的加密程序测试源码
http://idsips.blog.163.com/blog/static/480012722012398048394/
AES加密和解密——使用openssl编程
http://www.lovelucy.info/openssl-aes-encryption.htmlAES_cbc_encrypt
http://blog.sina.com.cn/s/blog_608a9fa70100dk6x.html
AES_cbc_encrypt and Perl Crypt::CBC Incompatibility
hex2bin bin2hex
http://www.opensubscriber.com/message/openssl-users@openssl.org/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<stdio.h>#include <string.h>#include "stdafx.h"#include <openssl/aes.h>#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<ctLen; i++) { memset(strAsii, 0, 5); memcpy(strAsii, strUnEncryptSrc, 2); sscanf(strAsii,"%X",&nCipherHex); cipher[i]= nCipherHex; strUnEncryptSrc += 2; } AES_set_decrypt_key(strEncryptKey, 16*16,&k); AES_cbc_encrypt((unsignedchar*)cipher,(unsigned char*) strEncryptDes, ctLen,&k, (unsigned char*)iv1, AES_DECRYPT); //hextoasii(cipher, ctLen, strEncryptDes); return strEncryptDes;}//-----------------------------------------------------------char* AESEncrypt( unsigned char* strUnEncryptSrc,unsigned char* strEncryptDes,int ilen){ //string strEncryptDes; unsignedchar cipher[10000]= {0};// 密文 unsignedchar iv1[16+1]= {"|bc~!f947j*$m_op"};// 16+1,向量 unsignedchar strEncryptKey[]= ")*^&$*kdjfkdjfjdsjfska!*&^%$#@";// 128bits key (应该是真正的随机数才好) AES_KEY k; int i=0; char strAsii[5]; //int ilen=strUnEncryptSrc.length(); int ctLen= (ilen%16==0)?ilen:(ilen/16+1)*16; AES_set_encrypt_key((unsignedchar*)strEncryptKey, 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(strUnEncryptSrc,(unsigned char*)cipher, ilen,&k, (unsigned char*)iv1, AES_ENCRYPT); for(i=0; i<ctLen; i++) { memset(strAsii, 0, 5); sprintf(strAsii,"%02X", cipher[i]); memcpy(&strEncryptDes[i*2], strAsii, 2); } //hextoasii(cipher, ctLen, strEncryptDes); return(char*)strEncryptDes;}//-----------------------------------------------------------void aes_hex(void){ char str[]= "hello world\n"; char strAes[10000]= {0}; char strUnAes[10000]={0}; AESEncrypt ((unsignedchar*)str,(unsigned char*)strAes,sizeof(str)); AESDecrypt (strUnAes,strAes); printf("\n"); printf(str); printf(strUnAes);}//----------------------------------------------------------void hextoasii(unsignedchar *bytes,int byteNum,char *strAsii){ int i=0; for(i=0; i<byteNum; i++) { sprintf(strAsii,"%02X", bytes[i]); strAsii += 2; } *strAsii ='\n';} AES_cbc_encrypt源码http://bbs.chinaunix.net/thread-948004-1-1.htmlvoid AES_cbc_encrypt(const unsigned char *in, unsigned char *out, const unsigned long length, const AES_KEY *key, unsigned char *ivec, const int enc) { unsigned long n; unsigned long len = length; unsigned char tmp[AES_BLOCK_SIZE]; const unsigned char *iv = ivec; assert(in && out && key && ivec); assert((AES_ENCRYPT == enc)||(AES_DECRYPT == enc)); if (AES_ENCRYPT == enc) { while (len >= 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); } }}
- AES 加密解密API
- 加密解密。(aes, java)
- AES加密解密类
- AES 加密/解密备忘
- java,AES加密,解密
- AES 加密解密 c#
- AES加密解密程序。
- AES加密和解密
- java AES加密解密
- java AES加密解密
- AES加密解密 应用
- php AES加密解密
- AES加密解密
- AES加密与解密
- java AES加密解密
- c# aes 加密解密
- java AES 加密/解密
- MySQL AES加密解密
- Effective Java总结
- Internet,缤纷世界,内藏无限烦恼
- _ITERATOR_DEBUG_LEVEL
- 数据结构之红黑树
- Android中获取系统内存信息以及进程信息-----ActivityManager的使用
- AES 加密解密API
- mysql show processlist 使用
- Android中Cursor关闭的问题
- 八大排序算法总结
- 开发Node.js的Windows版C/C++原生扩展(node_0.10和node_0.12)
- Android 地区语言和简写对照表
- 服务器防止CC攻击方法
- POJ3349 #no
- MIT算法导论——第六讲.Order statistics and Median