使用Openssl的AES加密算法

 

原文连接: http://blog.csdn.net/yasi_xi/article/details/13997337php

Openssl是很常见的C接口的库,我的以为易用。如下是AES加密的使用备忘。若是你有必定的密码学基础,那么就很好理解。代码是从网上弄下来的(原始地址已经忘记了),而后在尝试的过程当中改了一点东西。其它的cbc、cfb、ecb加密方式的用法都是相似的,只是函数名有点区别,就不一一列举了。算法

【yasi】IV: Initialization Vector,即初始化向量数组

1、接口简介app

 

[cpp] view plaincopy在CODE上查看代码片派生到个人代码片
 
  1. //设置加密密钥,使用字符缓冲区  
  2. int AES_set_encrypt_key(  
  3.         const unsigned char *userKey,  
  4.         const int bits,  
  5.         AES_KEY *key);  
  6.    
  7. //设置解密密钥,一样适用字符缓冲区  
  8. int AES_set_decrypt_key(  
  9.         const unsigned char *userKey,  
  10.         const int bits,  
  11.         AES_KEY *key);  
  12.    
  13. //加解密的接口,经过最后的enc来区分是加密仍是解密操做  
  14. //每次执行AES_cbc_encrypt后,iv(向量)会被更新,  
  15. //因此须要本身保存它。  
  16. void AES_cbc_encrypt(  
  17.         const unsigned char *in,  
  18.         unsigned char *out,  
  19.         const unsigned long length,  
  20.         const AES_KEY *key,  
  21.         unsigned char *ivec,  
  22.         const int enc);  
?

 

2、一个简单的Makefiledom

【yasi】针对CentOS环境,作了修改ide

 

[plain] view plaincopy在CODE上查看代码片派生到个人代码片
 
  1. LNK_OPT = -g -L/usr/lib64/ -lssl  
  2.   
  3. all:  
  4.         rm -f codec  
  5.         g++ -g aes_codec.cpp -o codec $(LNK_OPT)  
  6.   
  7. clean:  
  8.         rm -f codec  

3、示例代码
【yasi】对源连接的代码作了点修改:随机明码 改 静态明码

 

[cpp] view plaincopy在CODE上查看代码片派生到个人代码片
 
  1. #include <stdio.h>  
  2. #include <string.h>  
  3. #include <openssl/aes.h>  
  4. #include <openssl/rand.h>  
  5.   
  6. /* file testaes.cpp */  
  7.   
  8. static void hexdump(  
  9.                 FILE *f,  
  10.                 const char *title,  
  11.                 const unsigned char *s,  
  12.                 int l)  
  13. {  
  14.     int n = 0;  
  15.   
  16.     fprintf(f, "%s", title);  
  17.     for (; n < l; ++n) {  
  18.         if ((n % 16) == 0) {  
  19.                 fprintf(f, "\n%04x", n);  
  20.         }  
  21.         fprintf(f, " %02x", s[n]);  
  22.     }  
  23.   
  24.     fprintf(f, "\n");  
  25. }  
  26.   
  27. int main(int argc, char **argv)  
  28. {  
  29.         //128bits key.  
  30.         unsigned char   rkey[16];  
  31.         //Internal key.  
  32.         AES_KEY         key;  
  33.   
  34.         //Testdata.  
  35.         // [yasi] Make static content instead of random text  
  36.         unsigned char   plaintext[AES_BLOCK_SIZE * 4] =  
  37.         {  
  38.                 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'i', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'i',  
  39.                 '0', '1', '2', '3', '4', '5', '6', '7', '0', '1', '2', '3', '4', '5', '6', '7',  
  40.                 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'i', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'i',  
  41.                 '0', '1', '2', '3', '4', '5', '6', '7', '0', '1', '2', '3', '4', '5', '6', '7'  
  42.         };  
  43.         unsigned char   ciphertext[AES_BLOCK_SIZE * 4];  
  44.         unsigned char   checktext[AES_BLOCK_SIZE * 4];  
  45.   
  46.         //Init vector.  
  47.         unsigned char   iv[AES_BLOCK_SIZE * 4];  
  48.         //Save vector.  
  49.         unsigned char   saved_iv[AES_BLOCK_SIZE * 4];  
  50.   
  51.         int nr_of_bits = 0;  
  52.         int nr_of_bytes = 0;  
  53.   
  54.         //Zeror buffer.  
  55.         memset(ciphertext, 0, sizeof ciphertext);  
  56.         memset(checktext, 0, sizeof checktext);  
  57.   
  58.         //Generate random  
  59.         RAND_pseudo_bytes(rkey, sizeof rkey);  
  60.         RAND_pseudo_bytes(saved_iv, sizeof saved_iv);  
  61.   
  62.         hexdump(stdout, "== rkey ==",  
  63.                         rkey,  
  64.                         sizeof(rkey));  
  65.         hexdump(stdout, "== iv ==",  
  66.                         saved_iv,  
  67.                         sizeof(saved_iv));  
  68.         printf("\n");  
  69.   
  70.         hexdump(stdout, "== plaintext ==",  
  71.                         plaintext,  
  72.                         sizeof(plaintext));  
  73.         printf("\n");  
  74.   
  75.         //Entrypt  
  76.         memcpy(iv, saved_iv, sizeof(iv));  
  77.         nr_of_bits = 8 * sizeof(rkey);  
  78.         AES_set_encrypt_key(rkey, nr_of_bits, &key);  
  79.         nr_of_bytes = sizeof(plaintext);  
  80.         AES_cbc_encrypt(plaintext,  
  81.                         ciphertext,  
  82.                         nr_of_bytes,  
  83.                         &key,  
  84.                         iv,  
  85.                         AES_ENCRYPT);  
  86.   
  87.         hexdump(stdout, "== ciphertext ==",  
  88.                         ciphertext,  
  89.                         sizeof(ciphertext));  
  90.         printf("\n");  
  91.         // [yasi] iv is changed in encryption  
  92.         hexdump(stdout, "== iv changed ==",  
  93.                         iv,  
  94.                         sizeof(iv));  
  95.         printf("\n");  
  96.   
  97.         //Decrypt  
  98.         memcpy(iv, saved_iv, sizeof(iv));       // [yasi] without this line, decrypt will fail because iv is changed in encryption  
  99.         nr_of_bits = 8 * sizeof(rkey);  
  100.         AES_set_decrypt_key(rkey, nr_of_bits, &key);  
  101.         nr_of_bytes = sizeof(ciphertext);  
  102.   
  103.         AES_cbc_encrypt(ciphertext,  
  104.                         checktext,  
  105.                         nr_of_bytes,  
  106.                         &key, iv,  
  107.                         AES_DECRYPT);  
  108.         hexdump(stdout, "== checktext ==",  
  109.                         checktext,  
  110.                         sizeof(checktext));  
  111.         printf("\n");  
  112.   
  113.         return 0;  
  114. }  
 
【yasi 运行结果】
[plain] view plaincopy在CODE上查看代码片派生到个人代码片
 
  1. [root@ampcommons02 aes-codec]# ./codec  
  2. == rkey ==  
  3. 0000 81 ac 9b 38 1c 02 c5 c8 1d 7c a0 3f 87 be f2 c6  
  4. == iv ==  
  5. 0000 34 a2 f1 f5 f3 93 76 32 cd 77 ad fb c8 82 f2 1b  
  6. 0010 f3 cc 51 f6 35 f3 49 8d 44 97 8c 2c 89 50 0d d7  
  7. 0020 68 21 d7 2f 0a 90 29 c1 dd c9 39 bc 7c 4f 18 2f  
  8. 0030 04 cc 42 5e 84 8e fe a9 c5 49 00 9f 30 55 94 c0  
  9.   
  10. == plaintext ==  
  11. 0000 61 62 63 64 65 66 67 69 61 62 63 64 65 66 67 69  
  12. 0010 30 31 32 33 34 35 36 37 30 31 32 33 34 35 36 37  
  13. 0020 61 62 63 64 65 66 67 69 61 62 63 64 65 66 67 69  
  14. 0030 30 31 32 33 34 35 36 37 30 31 32 33 34 35 36 37  
  15.   
  16. == ciphertext ==  
  17. 0000 c4 63 72 29 21 28 7b a2 27 24 4a e4 bb 95 1a d1  
  18. 0010 b8 13 0e 77 0c 8a a4 09 2f ca 85 43 41 b5 5b d5  
  19. 0020 a3 60 92 58 5b dd 45 0c e2 62 af f9 43 81 d7 06  
  20. 0030 41 8e 85 28 3e eb 72 b5 ee 84 8c 27 7e 67 20 f6  
  21.   
  22. == iv changed ==  
  23. 0000 41 8e 85 28 3e eb 72 b5 ee 84 8c 27 7e 67 20 f6  
  24. 0010 f3 cc 51 f6 35 f3 49 8d 44 97 8c 2c 89 50 0d d7  
  25. 0020 68 21 d7 2f 0a 90 29 c1 dd c9 39 bc 7c 4f 18 2f  
  26. 0030 04 cc 42 5e 84 8e fe a9 c5 49 00 9f 30 55 94 c0  
  27.   
  28. == checktext ==  
  29. 0000 61 62 63 64 65 66 67 69 61 62 63 64 65 66 67 69  
  30. 0010 30 31 32 33 34 35 36 37 30 31 32 33 34 35 36 37  
  31. 0020 61 62 63 64 65 66 67 69 61 62 63 64 65 66 67 69  
  32. 0030 30 31 32 33 34 35 36 37 30 31 32 33 34 35 36 37  
可见,encryption以后,IV当即被修改了。因此,为了能正确decrypt,在decrypt时,必须使用先前encrypt时的IV,即代码第98行。
 
【第二次运行的结果】
[plain] view plaincopy在CODE上查看代码片派生到个人代码片
 
  1. [root@ampcommons02 aes-codec]# ./codec  
  2. == rkey ==  
  3. 0000 29 68 75 4d a5 9e 83 9a ed f8 ec bc 2e b8 09 7e  
  4. == iv ==  
  5. 0000 b8 21 09 de 8f 58 6e be 73 be a7 10 fb 91 87 65  
  6. 0010 65 9c d7 0e 4c 88 d2 65 ae de 0b 49 40 c7 75 df  
  7. 0020 19 69 53 0b 11 5d ac e7 08 f6 ae df 16 66 e0 13  
  8. 0030 75 41 f7 bb be 56 a1 dd a7 3e fb 4e 5d 9e e4 a2  
  9.   
  10. == plaintext ==  
  11. 0000 61 62 63 64 65 66 67 69 61 62 63 64 65 66 67 69  
  12. 0010 30 31 32 33 34 35 36 37 30 31 32 33 34 35 36 37  
  13. 0020 61 62 63 64 65 66 67 69 61 62 63 64 65 66 67 69  
  14. 0030 30 31 32 33 34 35 36 37 30 31 32 33 34 35 36 37  
  15.   
  16. == ciphertext ==  
  17. 0000 5e 85 9c e8 65 d6 3b f9 03 9a a0 b5 78 bd f6 d4  
  18. 0010 11 70 94 c1 c3 78 9a 1d 12 9a 84 48 3a 70 88 13  
  19. 0020 d6 b5 bf c6 e8 e1 76 dc 3c b9 b0 4e b9 bb c4 74  
  20. 0030 35 3d ac fc 29 e3 a0 64 d7 76 ab 76 c7 af dd 39  
  21.   
  22. == iv changed ==  
  23. 0000 35 3d ac fc 29 e3 a0 64 d7 76 ab 76 c7 af dd 39  
  24. 0010 65 9c d7 0e 4c 88 d2 65 ae de 0b 49 40 c7 75 df  
  25. 0020 19 69 53 0b 11 5d ac e7 08 f6 ae df 16 66 e0 13  
  26. 0030 75 41 f7 bb be 56 a1 dd a7 3e fb 4e 5d 9e e4 a2  
  27.   
  28. == checktext ==  
  29. 0000 61 62 63 64 65 66 67 69 61 62 63 64 65 66 67 69  
  30. 0010 30 31 32 33 34 35 36 37 30 31 32 33 34 35 36 37  
  31. 0020 61 62 63 64 65 66 67 69 61 62 63 64 65 66 67 69  
  32. 0030 30 31 32 33 34 35 36 37 30 31 32 33 34 35 36 37  
可见,两次运行,虽然encrypt的明码是同样的,但encrypt出来的结果是不一样的。随机明码 改 静态明码 的目的就是为了比较两次encrypt的结果。即,相同的明码用不一样的IV作encrypt,结果是不一样的。
 
【屏蔽第98行从新编译后的执行结果】
[plain] view plaincopy在CODE上查看代码片派生到个人代码片
 
  1. [root@ampcommons02 aes-codec]# ./codec  
  2. == rkey ==  
  3. 0000 69 ef eb 49 25 5a c2 5e 0d 77 8a cb e6 fe ad 1d  
  4. == iv ==  
  5. 0000 8e 05 8c 50 2f 69 9d fb 64 3e cd e6 2d 38 26 1c  
  6. 0010 6e 21 00 e6 32 3f c6 ca 93 8b c1 e3 47 9a bd 81  
  7. 0020 d7 e5 0b 63 dc f8 9d 1f 13 49 35 25 70 4e 64 c2  
  8. 0030 ea 0c d0 78 e7 6c 65 64 41 4d db 2b 50 4d b4 06  
  9.   
  10. == plaintext ==  
  11. 0000 61 62 63 64 65 66 67 69 61 62 63 64 65 66 67 69  
  12. 0010 30 31 32 33 34 35 36 37 30 31 32 33 34 35 36 37  
  13. 0020 61 62 63 64 65 66 67 69 61 62 63 64 65 66 67 69  
  14. 0030 30 31 32 33 34 35 36 37 30 31 32 33 34 35 36 37  
  15.   
  16. == ciphertext ==  
  17. 0000 a4 ed ba 4b 9f e9 74 bd 6d f6 03 76 79 9f 17 4f  
  18. 0010 0c cd f3 b8 da 69 44 81 c9 f2 8b 03 83 0d 9d 77  
  19. 0020 12 48 ea 46 3f eb 58 fd 48 c5 cc 2d 74 6c 99 4f  
  20. 0030 93 bd 0d 06 f3 55 40 11 cb e7 d4 29 3b 8f 15 76  
  21.   
  22. == iv changed ==  
  23. 0000 93 bd 0d 06 f3 55 40 11 cb e7 d4 29 3b 8f 15 76  
  24. 0010 6e 21 00 e6 32 3f c6 ca 93 8b c1 e3 47 9a bd 81  
  25. 0020 d7 e5 0b 63 dc f8 9d 1f 13 49 35 25 70 4e 64 c2  
  26. 0030 ea 0c d0 78 e7 6c 65 64 41 4d db 2b 50 4d b4 06  
  27.   
  28. == checktext ==  
  29. 0000 7c da e2 32 b9 5a ba 83 ce bb 7a ab 73 d1 54 03  
  30. 0010 30 31 32 33 34 35 36 37 30 31 32 33 34 35 36 37  
  31. 0020 61 62 63 64 65 66 67 69 61 62 63 64 65 66 67 69  
  32. 0030 30 31 32 33 34 35 36 37 30 31 32 33 34 35 36 37  
可见,decrypt时使用和encrypt时不一样的IV,不能正确decrypt出先前的明码。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
【yasi】Openssl官方wiki,《EVP Symmetric Encryption and Decryption》中,有个的简单例子,整理以下(Linux下运行):(代码下载)
algo_aes.h
[cpp] view plaincopy在CODE上查看代码片派生到个人代码片
 
  1. #ifndef ALGO_AES_H  
  2. #define ALGO_AES_H  
  3.   
  4. int encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key,  
  5.   unsigned char *iv, unsigned char *ciphertext);  
  6.   
  7. int decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key,  
  8.   unsigned char *iv, unsigned char *plaintext);  
  9.   
  10. #endif  

algo_aes.c
[cpp] view plaincopy在CODE上查看代码片派生到个人代码片
 
  1. #include <stdlib.h>  
  2. #include <stdio.h>  
  3. #include "algo_aes.h"  
  4. #include <openssl/evp.h>  
  5.   
  6. void handleErrors(void)  
  7. {  
  8.   ERR_print_errors_fp(stderr);  
  9.   abort();  
  10. }  
  11.   
  12. int encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key,  
  13.   unsigned char *iv, unsigned char *ciphertext)  
  14. {  
  15.   EVP_CIPHER_CTX *ctx;  
  16.   
  17.   int len;  
  18.   
  19.   int ciphertext_len;  
  20.   
  21.   /* Create and initialise the context */  
  22.   if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors();  
  23.   
  24.   /* Initialise the encryption operation. IMPORTANT - ensure you use a key 
  25.    * and IV size appropriate for your cipher 
  26.    * In this example we are using 256 bit AES (i.e. a 256 bit key). The 
  27.    * IV size for *most* modes is the same as the block size. For AES this 
  28.    * is 128 bits */  
  29.   if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv))  
  30.     handleErrors();  
  31.   
  32.   /* Provide the message to be encrypted, and obtain the encrypted output. 
  33.    * EVP_EncryptUpdate can be called multiple times if necessary 
  34.    */  
  35.   if(1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len))  
  36.     handleErrors();  
  37.   ciphertext_len = len;  
  38.   
  39.   /* Finalise the encryption. Further ciphertext bytes may be written at 
  40.    * this stage. 
  41.    */  
  42.   if(1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len)) handleErrors();  
  43.   ciphertext_len += len;  
  44.   
  45.   /* Clean up */  
  46.   EVP_CIPHER_CTX_free(ctx);  
  47.   
  48.   return ciphertext_len;  
  49. }  
  50.   
  51. int decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key,  
  52.   unsigned char *iv, unsigned char *plaintext)  
  53. {  
  54.   EVP_CIPHER_CTX *ctx;  
  55.   
  56.   int len;  
  57.   
  58.   int plaintext_len;  
  59.   
  60.   /* Create and initialise the context */  
  61.   if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors();  
  62.   
  63.   /* Initialise the decryption operation. IMPORTANT - ensure you use a key 
  64.    * and IV size appropriate for your cipher 
  65.    * In this example we are using 256 bit AES (i.e. a 256 bit key). The 
  66.    * IV size for *most* modes is the same as the block size. For AES this 
  67.    * is 128 bits */  
  68.   if(1 != EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv))  
  69.     handleErrors();  
  70.   
  71.   /* Provide the message to be decrypted, and obtain the plaintext output. 
  72.    * EVP_DecryptUpdate can be called multiple times if necessary 
  73.    */  
  74.   if(1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len))  
  75.     handleErrors();  
  76.   plaintext_len = len;  
  77.   
  78.   /* Finalise the decryption. Further plaintext bytes may be written at 
  79.    * this stage. 
  80.    */  
  81.   if(1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len)) handleErrors();  
  82.   plaintext_len += len;  
  83.   
  84.   /* Clean up */  
  85.   EVP_CIPHER_CTX_free(ctx);  
  86.   
  87.   return plaintext_len;  
  88. }  

main.c
[cpp] view plaincopy在CODE上查看代码片派生到个人代码片
 
  1. #include "algo_aes.h"  
  2. #include <stdio.h>  
  3. #include <string.h>  
  4. //#include <openssl/evp.h>  
  5.   
  6. int main(int arc, char *argv[])  
  7. {  
  8.   /* Set up the key and iv. Do I need to say to not hard code these in a 
  9.    * real application? :-) 
  10.    */  
  11.   
  12.   /* A 256 bit key */  
  13.   unsigned char *key = "01234567890123456789012345678901";  
  14.   
  15.   /* A 128 bit IV */  
  16.   unsigned char *iv = "01234567890123456";  
  17.   
  18.   /* Message to be encrypted */  
  19.   unsigned char *plaintext =  
  20.     "The quick brown fox jumps over the lazy dog1234";  
  21.   
  22.   /* Buffer for ciphertext. Ensure the buffer is long enough for the 
  23.    * ciphertext which may be longer than the plaintext, dependant on the 
  24.    * algorithm and mode 
  25.    */  
  26.   unsigned char ciphertext[64];  
  27.   
  28.   /* Buffer for the decrypted text */  
  29.   unsigned char decryptedtext[64];  
  30.   
  31.   int decryptedtext_len, ciphertext_len;  
  32.   
  33.   /* Initialise the library */  
  34. /*  ERR_load_crypto_strings(); 
  35.   OpenSSL_add_all_algorithms(); 
  36.   OPENSSL_config(NULL);*/  
  37.   
  38.   printf("Plaintext is:\n%s~\n", plaintext);  
  39.   
  40.   /* Encrypt the plaintext */  
  41.   ciphertext_len = encrypt(plaintext, strlen(plaintext), key, iv,  
  42.     ciphertext);  
  43.   
  44.   /* Do something useful with the ciphertext here */  
  45.   printf("Ciphertext is %d bytes long:\n", ciphertext_len);  
  46.   BIO_dump_fp(stdout, ciphertext, ciphertext_len);  
  47.   
  48.   /* Decrypt the ciphertext */  
  49.   decryptedtext_len = decrypt(ciphertext, ciphertext_len, key, iv,  
  50.     decryptedtext);  
  51.   
  52.   /* Add a NULL terminator. We are expecting printable text */  
  53.   decryptedtext[decryptedtext_len] = '\0';  
  54.   
  55.   /* Show the decrypted text */  
  56.   printf("Decrypted text is:\n");  
  57.   printf("%s~\n", decryptedtext);  
  58.   
  59.   /* Clean up */  
  60.   EVP_cleanup();  
  61.   ERR_free_strings();  
  62.   
  63.   return 0;  
  64. }  
Mekefile:
[plain] view plaincopy在CODE上查看代码片派生到个人代码片
 
  1. OBJ_DIR = ./obj  
  2. BIN_DIR = ./bin  
  3. SRC_DIR = ./  
  4. OBJS = \  
  5.     $(OBJ_DIR)/algo_aes.o \  
  6.     $(OBJ_DIR)/main.o  
  7. TARGET = $(BIN_DIR)/main  
  8. INC_OPT = -I./  
  9. LNK_OPT = -lssl   
  10.   
  11. $(TARGET) : clean chkobjdir chkbindir $(OBJS)  
  12.     gcc -g -o $@ $(OBJS) $(LNK_OPT)  
  13.   
  14. $(OBJ_DIR)/%.o : $(SRC_DIR)/%.c  
  15.     gcc -g $(INC_OPT) -c -o $@ $<  
  16.   
  17. chkobjdir :  
  18.     @if test ! -d $(OBJ_DIR) ; \  
  19.     then \  
  20.         mkdir $(OBJ_DIR) ; \  
  21.     fi  
  22.   
  23. chkbindir :  
  24.     @if test ! -d $(BIN_DIR) ; \  
  25.     then \  
  26.         mkdir $(BIN_DIR) ; \  
  27.     fi  
  28.   
  29. clean :  
  30.     -rm -f $(TARGET)  
  31.     -rm -rf $(OBJ_DIR)  
运行结果:
[plain] view plaincopy在CODE上查看代码片派生到个人代码片
 
  1. Plaintext is:  
  2. The quick brown fox jumps over the lazy dog1234~  
  3. Ciphertext is 48 bytes long:  
  4. 0000 - e0 6f 63 a7 11 e8 b7 aa-9f 94 40 10 7d 46 80 a1   .oc.......@.}F..  
  5. 0010 - 17 99 43 80 ea 31 d2 a2-99 b9 53 02 d4 39 b9 70   ..C..1....S..9.p  
  6. 0020 - e9 29 d5 a8 03 bd 71 31-b8 c3 6f 3d 39 3a 3d 3d   .)....q1..o=9:==  
  7. Decrypted text is:  
  8. The quick brown fox jumps over the lazy dog1234~  

注意:参考

AES算法的块(block)的长度固定为16字节。假设一个字符串在AES加密前的长度为cleanLen,加密后的长度为cipherLen,则两者有下面的关系,其中的“/”是整除。wordpress

cipherLen = (clearLen/16 + 1) * 16函数

好比:(注意第二行,即便48恰好能被16整除,也要额外追加一个16字节的块)
clearLen cipherLen 
47 48
48 64
49 64
可见,对于AES算法:
1)加密后的长度>=加密前的长度
2)解密后的长度<=解密前的长度
这对于写代码时的指导意义在于:
1)假如咱们要作AES加密的字符串为“The quick brown fox jumps over the lazy dog”,它的长度(不带/0)为43字节,则咱们能够计算出加密后的串的长度为 (43 / 16 + 1) * 16 = 48 字节,因而就能够申明加密后的数组以下
unsigned char ciphertextp[48];
2)假如咱们知道AES加密后的串的长度为64字节,那么就能够申明解密后的数组以下
unsigned char decryptedtext[64];