1. 程式人生 > >【加密/解密】Botan 中的 AES 加密演算法例項

【加密/解密】Botan 中的 AES 加密演算法例項

AES 演算法的金鑰和分塊大小可以是128,192,256位.
例如,AES-128演算法加密後的密文的長度是 16位元組的整數倍.
若明文長度小於16位元組,則密文長度為16位元組;
若明文長度等於16位元組,則密文長度為32位元組.

如果採用 AES-256, 則金鑰長度必須是 256位.

MD5 雜湊演算法的輸出是128位
SHA-256 雜湊演算法的輸出是256位

  1. <pre name="code"class="cpp">#include <iostream>  
  2. #include <string>
  3. usingnamespace std;  
  4. #include <botan/botan.h>
  5. usingnamespace Botan;  
  6. string cryptoAES128(string input,string passphrase,Cipher_Dir opt);  
  7. string cryptoAES256(string input,string passphrase,Cipher_Dir opt);  
  8. int main()  
  9. {  
  10.     char src[800]="中華人民共和國";  
  11.     string input;  
  12.     string key="zhankunlin";  
  13.     string output;  
  14.     constchar *chars=NULL;  
  15.     int length=0;  
  16.     for(int i=strlen(src); i<sizeof(src)-1; i++)  
  17.     {  
  18.         if( i%10 == 0)  
  19.             src[i]=0;  
  20.         else
  21.             src[i]='A'+i%26;  
  22.     }     
  23.     string x(src,sizeof(src));  
  24.     input=x;  
  25.     cout<<"-------- 測試 AES-128 加密 -----"<<endl;  
  26.     cout<<"[明文]"
    <<endl<<input<<endl;  
  27.     output = cryptoAES128(input, key, ENCRYPTION);//加密
  28.     chars = output.data();  
  29.     length = output.size();  
  30.     cout<<"[密文]"<<endl;  
  31.     for(int i=0; i<length; i++)  
  32.         printf("%*.*X ",-2,2,chars[i]);  
  33.     cout<<endl;  
  34.     cout<<"密文大小: "<<length<<endl;//16的倍數
  35.     //模擬C/S結構的傳送端和接收端
  36.     //傳送端構造報文:報文前4個位元組是報文長度,為明文;後面跟上密文.
  37.     int msgLen=length+4;  
  38.     char msgLenChars[4]={};  
  39.     char *message=newchar[msgLen];  
  40.     memset(message,0,msgLen);  
  41.     sprintf(msgLenChars,"%*.*d",-4,4,msgLen);  
  42.     memmove(message,msgLenChars,4);  
  43.     memmove(message+4,chars,length);//密文
  44.     //傳送 message
  45.     //接收端接收
  46.     memmove(msgLenChars,message,4);  
  47.     sscanf(msgLenChars,"%d",&msgLen);  
  48.     char *message2=newchar[msgLen-4];//密文
  49.     memset(message2,0,msgLen-4);  
  50.     memmove(message2,message+4,msgLen-4);  
  51.     string str(message2,msgLen-4);//密文
  52.     output=str;  
  53.     output = cryptoAES128(output, key, DECRYPTION);//解密
  54.     if(output =="")  
  55.         cout<<"DECRYPTION failed"<<endl;  
  56.     cout<<"[明文]"<<endl<<output<<endl;  
  57.     cout<<"--------------------------------"<<endl;  
  58.     cout<<"-------- 測試 AES-256 加密 -----"<<endl;  
  59.     cout<<"[明文]"<<endl<<input<<endl;  
  60.     output = cryptoAES256(input, key, ENCRYPTION);//加密
  61.     chars = output.data();  
  62.     length = output.size();  
  63.     cout<<"[密文]"<<endl;  
  64.     for(int i=0; i<length; i++)  
  65.         printf("%*.*X ",-2,2,chars[i]);  
  66.     cout<<endl;  
  67.     cout<<"密文大小: "<<length<<endl;//32的倍數
  68.     output = cryptoAES256(output, key, DECRYPTION);//解密
  69.     cout<<"[明文]"<<endl<<output<<endl;  
  70.     cout<<"--------------------------------"<<endl;  
  71.     return 0;  
  72. }  
  73. //
  74. // Parameters
  75. /// string input  輸入位元組串
  76. /// string passphrase 用於生成128位金鑰的字串,本函式使用MD5生成128位長度的金鑰
  77. /// Cipher_Dir opt  加密或解密操作
  78. ///                 ENCRYPTION 加密
  79. ///                 DECRYPTION 解密
  80. /// @return 返回操作後的位元組串. 若output等於空串,表示解密或者解密失敗.
  81. //
  82. string cryptoAES128(string input,string passphrase,Cipher_Dir opt) {  
  83.     HashFunction* hash = get_hash("MD5"); //MD5演算法可以將任意長度的位元組串轉換為128位長度的位元組串
  84.     SymmetricKey key = hash->process(passphrase); //產生128位的位元組串作為金鑰
  85.     SecureVector<byte> raw_iv = hash->process('0'+ passphrase); //字串相加,然後對結果做MD5,生成128位的位元組串
  86.     InitializationVector iv(raw_iv, 16); //初始化向量
  87.     //AES-128是演算法名稱,分塊大小是128bit; CBC是演算法模式.
  88.     //AES演算法金鑰和分塊大小可以是128,192,256位.
  89.     //AES演算法模式還包括: ECB,CFB,OFB,CTR等.
  90.     Pipe pipe(get_cipher("AES-128/CBC", key, iv, opt));   
  91.     //Pipe pipe(get_cipher("AES-128/CBC", key, opt)); 
  92.     try{  
  93.         pipe.process_msg(input); //encryption or decryption. 
  94.     }  
  95.     catch(Botan::Decoding_Error &e)  
  96.     { //解密失敗,丟擲一個Botan::Decoding_Error型別的異常
  97.         cout<< e.what() <<endl;  
  98.         string output="";  
  99.         return output;  
  100.     }  
  101.     string output=pipe.read_all_as_string();  
  102.     return output;  
  103. }  
  104. //
  105. // Parameters
  106. /// string input  輸入位元組串
  107. /// string passphrase 用於生成256位金鑰的字串,本函式使用SHA-256生成256位長度的金鑰
  108. /// Cipher_Dir opt  加密或解密操作
  109. ///                 ENCRYPTION 加密
  110. ///                 DECRYPTION 解密
  111. /// @return 返回操作後的位元組串. 若output等於空串,表示解密或者解密失敗.
  112. //
  113. string cryptoAES256(string input,string passphrase,Cipher_Dir opt) {  
  114.     HashFunction* hash = get_hash("SHA-256"); //SHA256演算法可以將任意長度的位元組串轉換為256位長度的位元組串
  115.     SymmetricKey key = hash->process(passphrase); //產生256位的位元組串作為金鑰
  116.     SecureVector<byte> raw_iv = hash->process('0'+ passphrase);   
  117.     InitializationVector iv(raw_iv, 16); //初始化向量,可能會丟擲異常 Botan::Invalid_IV_Length
  118.     //AES-256是演算法名稱,分塊大小是256bit; CBC是演算法模式.
  119.     //AES演算法金鑰和分塊大小可以是128,192,256位.
  120.     //AES演算法模式還包括: ECB,CFB,OFB,CTR等.
  121.     Pipe pipe(get_cipher("AES-256/CBC", key, iv, opt));   
  122.     try{  
  123.         pipe.process_msg(input); //encryption or decryption. 
  124.     }  
  125.     catch(Botan::Decoding_Error &e)  
  126.     {  
  127.         cout<< e.what() <<endl;  
  128.         string output="";  
  129.         return output;  
  130.     }  
  131.     string output=pipe.read_all_as_string();  
  132.     return output;  
  133. }</pre><br>  
  134. <pre></pre>  
  135. <br>  
  136. <br>  
  137. <br>  
  138. <br>  
  139. <br>  
  140. <br>