【AES】使用OpenSSL库的AES加解密

来源:互联网 发布:贵州广电网络有线电视 编辑:程序博客网 时间:2024/04/28 18:52

AesTest.cpp

[cpp] view plain copy
 print?
  1. #include <stdio.h>  
  2. #include <string.h>  
  3. #include <sys/types.h>  
  4. #include <sys/stat.h>  
  5. #include <fcntl.h>  
  6. #include <errno.h>  
  7. #include <stdlib.h>  
  8. #include <unistd.h>  
  9. #include <openssl/aes.h>  
  10.   
  11. //g++ -g -o -Wall -m64 AesTest AesTest.cpp -lssl -lcrypto  
  12. //g++ -g -o -Wall AesTest AesTest.cpp -lssl -lcrypto  
  13.   
  14. int main(int argc, char **argv)  
  15. {//由于与直接对接用的char,那么加解密要强制转换  
  16.     char Source[1024];  
  17.     char *InputData=NULL;  
  18.     char *EncryptData=NULL;  
  19.     char *DecryptData=NULL;  
  20.       
  21.     unsigned char Key[AES_BLOCK_SIZE+1];    //建议用unsigned char  
  22.     unsigned char ivec[AES_BLOCK_SIZE];     //建议用unsigned char  
  23.     AES_KEY AesKey;  
  24.       
  25.     int DataLen=0,SetDataLen=0, i;  
  26.   
  27.     memset(Source, 0x00, sizeof(Source));  
  28.     strcpy(Source, "1234567890abcde");  //要加密的数据  
  29.     DataLen = strlen(Source);  
  30.   
  31.     memset(Key, 0x00, sizeof(Key));  
  32.     memcpy(Key, "0123456789abcdef", AES_BLOCK_SIZE);  
  33.   
  34.  // set the encryption length  
  35.     SetDataLen = 0;  
  36.     if ((DataLen%AES_BLOCK_SIZE) == 0)  
  37.     {  
  38.         SetDataLen = DataLen;  
  39.     }  
  40.     else  
  41.     {  
  42.         SetDataLen = ((DataLen/AES_BLOCK_SIZE)+1) * AES_BLOCK_SIZE;  
  43.     }  
  44.     printf("SetDataLen:%d...\n", SetDataLen);   //取16的倍数  
  45.       
  46.     InputData = (char *)calloc(SetDataLen+1, sizeof(char));  
  47.     if(InputData == NULL)   //注意要SetDataLen+1  
  48.     {  
  49.         fprintf(stderr, "Unable to allocate memory for InputData\n");  
  50.         exit(-1);  
  51.     }  
  52.     memcpy(InputData, Source, DataLen);  
  53.       
  54.     EncryptData = (char *)calloc(SetDataLen+1, sizeof(char));  
  55.     if(EncryptData == NULL) //注意要SetDataLen+1  
  56.     {  
  57.         fprintf(stderr, "Unable to allocate memory for EncryptData\n");  
  58.         exit(-1);  
  59.     }  
  60.       
  61.     DecryptData = (char *)calloc(SetDataLen+1, sizeof(char));  
  62.     if(DecryptData == NULL) //注意要SetDataLen+1  
  63.     {  
  64.         fprintf(stderr, "Unable to allocate memory for DecryptData\n");  
  65.         exit(-1);  
  66.     }  
  67.   
  68.     memset(&AesKey, 0x00, sizeof(AES_KEY));  
  69.     if(AES_set_encrypt_key(Key, 128, &AesKey) < 0)  
  70.     {//设置加密密钥  
  71.         fprintf(stderr, "Unable to set encryption key in AES...\n");  
  72.         exit(-1);  
  73.     }  
  74.   
  75.     for(i=0; i<AES_BLOCK_SIZE; i++)  
  76.     {//必须要有  
  77.         ivec[i] = 0;  
  78.     }  
  79.     //加密  
  80.     AES_cbc_encrypt((unsigned char *)InputData, (unsigned char *)EncryptData,   
  81.         SetDataLen, &AesKey, ivec, AES_ENCRYPT);     
  82.   
  83.     memset(&AesKey, 0x00, sizeof(AES_KEY));  
  84.     if(AES_set_decrypt_key(Key, 128, &AesKey) < 0)  
  85.     {//设置解密密钥  
  86.         fprintf(stderr, "Unable to set encryption key in AES...\n");  
  87.         exit(-1);  
  88.     }  
  89.   
  90.     for(i=0; i<AES_BLOCK_SIZE; i++)  
  91.     {//必须要有  
  92.         ivec[i] = 0;  
  93.     }  
  94.     //解密  
  95.     AES_cbc_encrypt((unsigned char *)EncryptData, (unsigned char *)DecryptData,   
  96.         SetDataLen, &AesKey, ivec, AES_DECRYPT);   
  97.   
  98.     printf("DecryptData:%s...\n", (char *)DecryptData);  
  99.   
  100.     if(InputData != NULL)  
  101.     {  
  102.         free(InputData);  
  103.         InputData = NULL;  
  104.     }  
  105.       
  106.     if(EncryptData != NULL)  
  107.     {  
  108.         free(EncryptData);  
  109.         EncryptData = NULL;  
  110.     }  
  111.       
  112.     if(DecryptData != NULL)  
  113.     {  
  114.         free(DecryptData);  
  115.         DecryptData = NULL;  
  116.     }  
  117.   
  118.     exit(0);  
  119. }  

Makefile

[cpp] view plain copy
 print?
  1. PROC_INC1=/usr/include  
  2.   
  3. INC_DIR=-I$(PROC_INC1)  
  4.   
  5. LIB_DIR=   
  6.   
  7. #-lssl -lcrypto,是openssl的两个库  
  8. All_LIB=-lssl -lcrypto  
  9.   
  10.   
  11. CC=g++  
  12.   
  13. default: AesTest  
  14.   
  15. AesTest:AesTest.o  
  16.     $(CC) -o $@ $^ $(INC_DIR) $(LIB_DIR) $(All_LIB)  
  17.   
  18. .cpp.o:  
  19.     $(CC) -c -g -Wall -D_GNU_SOURCE $(INC_DIR) $<  
  20.   
  21. clean:  
  22.     rm -f *.o AesTest  
附: OpenSSL支持多种不同的加密算法
加密:
AES, Blowfish, Camellia, SEED, CAST-128, DES, IDEA, RC2, RC4, RC5, Triple DES, GOST 28147-89[4]
散列函数:
MD5, MD2, SHA-1, SHA-2, RIPEMD-160, MDC-2, GOST R 34.11-94[4]
公开密钥加密:
RSA, DSA, Diffie–Hellman key exchange, Elliptic curve, GOST R 34.10-2001[4]
原创粉丝点击