【加密/解密】Botan 中的 AES 加密演算法例項
阿新 • • 發佈:2019-02-04
AES 演算法的金鑰和分塊大小可以是128,192,256位.
例如,AES-128演算法加密後的密文的長度是 16位元組的整數倍.
若明文長度小於16位元組,則密文長度為16位元組;
若明文長度等於16位元組,則密文長度為32位元組.
如果採用 AES-256, 則金鑰長度必須是 256位.
MD5 雜湊演算法的輸出是128位
SHA-256 雜湊演算法的輸出是256位
例如,AES-128演算法加密後的密文的長度是 16位元組的整數倍.
若明文長度小於16位元組,則密文長度為16位元組;
若明文長度等於16位元組,則密文長度為32位元組.
如果採用 AES-256, 則金鑰長度必須是 256位.
MD5 雜湊演算法的輸出是128位
SHA-256 雜湊演算法的輸出是256位
- <pre name="code"class="cpp">#include <iostream>
- #include <string>
- usingnamespace std;
-
#include <botan/botan.h>
- usingnamespace Botan;
- string cryptoAES128(string input,string passphrase,Cipher_Dir opt);
- string cryptoAES256(string input,string passphrase,Cipher_Dir opt);
- int main()
- {
- char src[800]="中華人民共和國";
- string input;
- string key="zhankunlin";
- string output;
-
constchar *chars=NULL;
- int length=0;
- for(int i=strlen(src); i<sizeof(src)-1; i++)
- {
- if( i%10 == 0)
- src[i]=0;
- else
- src[i]='A'+i%26;
- }
- string x(src,sizeof(src));
- input=x;
- cout<<"-------- 測試 AES-128 加密 -----"<<endl;
-
cout<<"[明文]"
- output = cryptoAES128(input, key, ENCRYPTION);//加密
- chars = output.data();
- length = output.size();
- cout<<"[密文]"<<endl;
- for(int i=0; i<length; i++)
- printf("%*.*X ",-2,2,chars[i]);
- cout<<endl;
- cout<<"密文大小: "<<length<<endl;//16的倍數
- //模擬C/S結構的傳送端和接收端
- //傳送端構造報文:報文前4個位元組是報文長度,為明文;後面跟上密文.
- int msgLen=length+4;
- char msgLenChars[4]={};
- char *message=newchar[msgLen];
- memset(message,0,msgLen);
- sprintf(msgLenChars,"%*.*d",-4,4,msgLen);
- memmove(message,msgLenChars,4);
- memmove(message+4,chars,length);//密文
- //傳送 message
- //接收端接收
- memmove(msgLenChars,message,4);
- sscanf(msgLenChars,"%d",&msgLen);
- char *message2=newchar[msgLen-4];//密文
- memset(message2,0,msgLen-4);
- memmove(message2,message+4,msgLen-4);
- string str(message2,msgLen-4);//密文
- output=str;
- output = cryptoAES128(output, key, DECRYPTION);//解密
- if(output =="")
- cout<<"DECRYPTION failed"<<endl;
- cout<<"[明文]"<<endl<<output<<endl;
- cout<<"--------------------------------"<<endl;
- cout<<"-------- 測試 AES-256 加密 -----"<<endl;
- cout<<"[明文]"<<endl<<input<<endl;
- output = cryptoAES256(input, key, ENCRYPTION);//加密
- chars = output.data();
- length = output.size();
- cout<<"[密文]"<<endl;
- for(int i=0; i<length; i++)
- printf("%*.*X ",-2,2,chars[i]);
- cout<<endl;
- cout<<"密文大小: "<<length<<endl;//32的倍數
- output = cryptoAES256(output, key, DECRYPTION);//解密
- cout<<"[明文]"<<endl<<output<<endl;
- cout<<"--------------------------------"<<endl;
- return 0;
- }
- //
- // Parameters
- /// string input 輸入位元組串
- /// string passphrase 用於生成128位金鑰的字串,本函式使用MD5生成128位長度的金鑰
- /// Cipher_Dir opt 加密或解密操作
- /// ENCRYPTION 加密
- /// DECRYPTION 解密
- /// @return 返回操作後的位元組串. 若output等於空串,表示解密或者解密失敗.
- //
- string cryptoAES128(string input,string passphrase,Cipher_Dir opt) {
- HashFunction* hash = get_hash("MD5"); //MD5演算法可以將任意長度的位元組串轉換為128位長度的位元組串
- SymmetricKey key = hash->process(passphrase); //產生128位的位元組串作為金鑰
- SecureVector<byte> raw_iv = hash->process('0'+ passphrase); //字串相加,然後對結果做MD5,生成128位的位元組串
- InitializationVector iv(raw_iv, 16); //初始化向量
- //AES-128是演算法名稱,分塊大小是128bit; CBC是演算法模式.
- //AES演算法金鑰和分塊大小可以是128,192,256位.
- //AES演算法模式還包括: ECB,CFB,OFB,CTR等.
- Pipe pipe(get_cipher("AES-128/CBC", key, iv, opt));
- //Pipe pipe(get_cipher("AES-128/CBC", key, opt));
- try{
- pipe.process_msg(input); //encryption or decryption.
- }
- catch(Botan::Decoding_Error &e)
- { //解密失敗,丟擲一個Botan::Decoding_Error型別的異常
- cout<< e.what() <<endl;
- string output="";
- return output;
- }
- string output=pipe.read_all_as_string();
- return output;
- }
- //
- // Parameters
- /// string input 輸入位元組串
- /// string passphrase 用於生成256位金鑰的字串,本函式使用SHA-256生成256位長度的金鑰
- /// Cipher_Dir opt 加密或解密操作
- /// ENCRYPTION 加密
- /// DECRYPTION 解密
- /// @return 返回操作後的位元組串. 若output等於空串,表示解密或者解密失敗.
- //
- string cryptoAES256(string input,string passphrase,Cipher_Dir opt) {
- HashFunction* hash = get_hash("SHA-256"); //SHA256演算法可以將任意長度的位元組串轉換為256位長度的位元組串
- SymmetricKey key = hash->process(passphrase); //產生256位的位元組串作為金鑰
- SecureVector<byte> raw_iv = hash->process('0'+ passphrase);
- InitializationVector iv(raw_iv, 16); //初始化向量,可能會丟擲異常 Botan::Invalid_IV_Length
- //AES-256是演算法名稱,分塊大小是256bit; CBC是演算法模式.
- //AES演算法金鑰和分塊大小可以是128,192,256位.
- //AES演算法模式還包括: ECB,CFB,OFB,CTR等.
- Pipe pipe(get_cipher("AES-256/CBC", key, iv, opt));
- try{
- pipe.process_msg(input); //encryption or decryption.
- }
- catch(Botan::Decoding_Error &e)
- {
- cout<< e.what() <<endl;
- string output="";
- return output;
- }
- string output=pipe.read_all_as_string();
- return output;
- }</pre><br>
- <pre></pre>
- <br>
- <br>
- <br>
- <br>
- <br>
- <br>