#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(unsigned char *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((unsigned char*)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(unsigned char),ctLen, fp1); fflush(fp1); char strAsii[1000]={0}; hextoasii((unsigned char*)ct, ctLen, strAsii); printf("\n%s\n", strAsii); //memset(pt2, 0, 33);
AES_set_decrypt_key(key, 16*16, &k); AES_cbc_encrypt((unsigned char*)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((unsigned char*)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;
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 i=0; char strAsii[5]; //int ilen=strUnEncryptSrc.length();
int ctLen = (ilen%16==0)?ilen:(ilen/16+1)*16; AES_set_encrypt_key((unsigned char*)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 ((unsigned char*)str, (unsigned char*)strAes, sizeof(str)); AESDecrypt (strUnAes,strAes); printf("\n"); printf(str); printf(strUnAes); } //---------------------------------------------------------- void hextoasii(unsigned char *bytes, int byteNum, char *strAsii) { int i=0; for(i=0; i<byteNum; i++) { sprintf(strAsii,"%02X", bytes[i]); strAsii += 2; } *strAsii = '\n'; }
|