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

AES 算法的密钥和分块大小可以是128,192,256位.
例如,AES-128算法加密后的密文的长度是 16字节的整数倍.
若明文长度小于16字节,则密文长度为16字节;
若明文长度等于16字节,则密文长度为32字节.

如果采用 AES-256, 则密钥长度必须是 256位.

MD5 哈希算法的输出是128位
SHA-256 哈希算法的输出是256位

[cpp]  view plain copy
  1. <pre name="code" class="cpp">#include <iostream>  
  2. #include <string>  
  3. using namespace std;  
  4.   
  5. #include <botan/botan.h>  
  6. using namespace Botan;  
  7.   
  8. string cryptoAES128(string input,string passphrase,Cipher_Dir opt);  
  9. string cryptoAES256(string input,string passphrase,Cipher_Dir opt);  
  10.   
  11. int main()  
  12. {  
  13.     char src[800]="中华人民共和国";  
  14.     string input;  
  15.     string key="zhankunlin";  
  16.     string output;  
  17.     const char *chars=NULL;  
  18.     int length=0;  
  19.   
  20.     for(int i=strlen(src); i<sizeof(src)-1; i++)  
  21.     {  
  22.         if( i%10 == 0)  
  23.             src[i]=0;  
  24.         else  
  25.             src[i]='A'+i%26;  
  26.     }     
  27.       
  28.     string x(src,sizeof(src));  
  29.     input=x;  
  30.       
  31.     cout<<"-------- 测试 AES-128 加密 -----"<<endl;  
  32.     cout<<"[明文]"<<endl<<input<<endl;  
  33.     output = cryptoAES128(input, key, ENCRYPTION);//加密  
  34.     chars = output.data();  
  35.     length = output.size();  
  36.     cout<<"[密文]"<<endl;  
  37.     for(int i=0; i<length; i++)  
  38.         printf("%*.*X ",-2,2,chars[i]);  
  39.     cout<<endl;  
  40.     cout<<"密文大小: "<<length<<endl;//16的倍数  
  41.   
  42.     //模拟C/S结构的发送端和接收端  
  43.     //发送端构造报文:报文前4个字节是报文长度,为明文;后面跟上密文.  
  44.     int msgLen=length+4;  
  45.     char msgLenChars[4]={};  
  46.     char *message=new char[msgLen];  
  47.     memset(message,0,msgLen);  
  48.     sprintf(msgLenChars,"%*.*d",-4,4,msgLen);  
  49.     memmove(message,msgLenChars,4);  
  50.     memmove(message+4,chars,length);//密文  
  51.   
  52.     //发送 message  
  53.   
  54.     //接收端接收  
  55.     memmove(msgLenChars,message,4);  
  56.     sscanf(msgLenChars,"%d",&msgLen);  
  57.     char *message2=new char[msgLen-4];//密文  
  58.     memset(message2,0,msgLen-4);  
  59.     memmove(message2,message+4,msgLen-4);  
  60.     string str(message2,msgLen-4);//密文  
  61.     output=str;  
  62.   
  63.     output = cryptoAES128(output, key, DECRYPTION);//解密  
  64.     if(output =="")  
  65.         cout<<"DECRYPTION failed"<<endl;  
  66.     cout<<"[明文]"<<endl<<output<<endl;  
  67.   
  68.     cout<<"--------------------------------"<<endl;  
  69.   
  70.   
  71.     cout<<"-------- 测试 AES-256 加密 -----"<<endl;  
  72.     cout<<"[明文]"<<endl<<input<<endl;  
  73.     output = cryptoAES256(input, key, ENCRYPTION);//加密  
  74.     chars = output.data();  
  75.     length = output.size();  
  76.     cout<<"[密文]"<<endl;  
  77.     for(int i=0; i<length; i++)  
  78.         printf("%*.*X ",-2,2,chars[i]);  
  79.     cout<<endl;  
  80.     cout<<"密文大小: "<<length<<endl;//32的倍数  
  81.     output = cryptoAES256(output, key, DECRYPTION);//解密  
  82.     cout<<"[明文]"<<endl<<output<<endl;  
  83.   
  84.     cout<<"--------------------------------"<<endl;  
  85.   
  86.   
  87.   
  88.     return 0;  
  89. }  
  90.   
  91. //  
  92. // Parameters  
  93. /// string input  输入字节串  
  94. /// string passphrase 用于生成128位密钥的字符串,本函数使用MD5生成128位长度的密钥  
  95. /// Cipher_Dir opt  加密或解密操作  
  96. ///                 ENCRYPTION 加密  
  97. ///                 DECRYPTION 解密  
  98. /// @return 返回操作后的字节串. 若output等于空串,表示解密或者解密失败.  
  99. //  
  100. string cryptoAES128(string input,string passphrase,Cipher_Dir opt) {  
  101.     HashFunction* hash = get_hash("MD5"); //MD5算法可以将任意长度的字节串转换为128位长度的字节串  
  102.     SymmetricKey key = hash->process(passphrase); //产生128位的字节串作为密钥  
  103.     SecureVector<byte> raw_iv = hash->process('0'+ passphrase); //字符串相加,然后对结果做MD5,生成128位的字节串  
  104.     InitializationVector iv(raw_iv, 16); //初始化向量  
  105.   
  106.     //AES-128是算法名称,分块大小是128bit; CBC是算法模式.  
  107.     //AES算法密钥和分块大小可以是128,192,256位.  
  108.     //AES算法模式还包括: ECB,CFB,OFB,CTR等.  
  109.     Pipe pipe(get_cipher("AES-128/CBC", key, iv, opt));   
  110.     //Pipe pipe(get_cipher("AES-128/CBC", key, opt));   
  111.   
  112.     try{  
  113.         pipe.process_msg(input); //encryption or decryption.   
  114.     }  
  115.     catch(Botan::Decoding_Error &e)  
  116.     { //解密失败,抛出一个Botan::Decoding_Error类型的异常  
  117.         cout<< e.what() <<endl;  
  118.         string output="";  
  119.         return output;  
  120.     }  
  121.       
  122.     string output=pipe.read_all_as_string();  
  123.   
  124.     return output;  
  125. }  
  126.   
  127.   
  128. //  
  129. // Parameters  
  130. /// string input  输入字节串  
  131. /// string passphrase 用于生成256位密钥的字符串,本函数使用SHA-256生成256位长度的密钥  
  132. /// Cipher_Dir opt  加密或解密操作  
  133. ///                 ENCRYPTION 加密  
  134. ///                 DECRYPTION 解密  
  135. /// @return 返回操作后的字节串. 若output等于空串,表示解密或者解密失败.  
  136. //  
  137. string cryptoAES256(string input,string passphrase,Cipher_Dir opt) {  
  138.     HashFunction* hash = get_hash("SHA-256"); //SHA256算法可以将任意长度的字节串转换为256位长度的字节串  
  139.     SymmetricKey key = hash->process(passphrase); //产生256位的字节串作为密钥  
  140.     SecureVector<byte> raw_iv = hash->process('0'+ passphrase);   
  141.     InitializationVector iv(raw_iv, 16); //初始化向量,可能会抛出异常 Botan::Invalid_IV_Length  
  142.   
  143.     //AES-256是算法名称,分块大小是256bit; CBC是算法模式.  
  144.     //AES算法密钥和分块大小可以是128,192,256位.  
  145.     //AES算法模式还包括: ECB,CFB,OFB,CTR等.  
  146.     Pipe pipe(get_cipher("AES-256/CBC", key, iv, opt));   
  147.   
  148.     try{  
  149.         pipe.process_msg(input); //encryption or decryption.   
  150.     }  
  151.     catch(Botan::Decoding_Error &e)  
  152.     {  
  153.         cout<< e.what() <<endl;  
  154.         string output="";  
  155.         return output;  
  156.     }  
  157.   
  158.     string output=pipe.read_all_as_string();  
  159.   
  160.     return output;  
  161. }</pre><br>  
  162. <pre></pre>  
  163. <br>  
  164. <br>  
  165. <br>  
  166. <br>  
  167. <br>  
  168. <br>  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值