AES加密 C++调用Crypto++加密库 例子

来源:互联网 发布:银川的网络怎么用不上 编辑:程序博客网 时间:2024/06/05 03:05

这阵子写了一些数据加密的小程序,对比了好几种算法后,选择了AES,高级加密标准(英语:Advanced Encryption Standard,缩写:AES),听这名字就很厉害的样子

估计会搜索到这文章的,对AES算法已经有了些基本了解了吧,下面先简单介绍一下AES加密算法吧

(1)AES在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准。2006年,高级加密标准已然成为对称密钥加密中最流行的算法之一。

(2)AES加密数据块分组长度必须为128比特,密钥长度可以是128比特、192比特、256比特中的任意一个。(8比特 == 1字节)

(3)在CBC、CFB、OFB、CTR模式下除了密钥外,还需要一个初始化向IV。(ECB模式不用IV

关于AES更多的介绍,http://zh.wikipedia.org/wiki/AES,或者是百度百科吧

AES可使用的加密模式的介绍,http://blog.csdn.NET/aaaaatiger/article/details/2525561


我使用的是Crypto++库,开发者是Wei Dai,使用C++写的加密库,实现了非常多的加密算法,基本能满足我们的加密需求,使用起来也很简单方便,这是官方网站http://www.cryptopp.com/


写这文章目的不是介绍AES算法,只是想给一个小例子让大家参考一下而已,避免大家在查了大半天加密算法,看了老久AES原理,可就是就不知道怎么使用

(基本加解密过程是stackoverflow的一个小demo,我将它修改一下,实现了一个在两个程序之间,以文件做为介质的加解密的过程)

这里选的是CBC模式(其它模式调用也一样)

1、程序一:加密

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include <stdio.h>  
  2.   
  3. #include <iostream>  
  4. #include <fstream>  
  5. #include <sstream>  
  6.   
  7. #include <cryptopp/aes.h>  
  8. #include <cryptopp/filters.h>  
  9. #include <cryptopp/modes.h>  
  10.   
  11. using namespace std;  
  12.   
  13. byte key[ CryptoPP::AES::DEFAULT_KEYLENGTH ], iv[ CryptoPP::AES::BLOCKSIZE];  
  14.   
  15. void initKV()  
  16. {  
  17.     memset( key, 0x00, CryptoPP::AES::DEFAULT_KEYLENGTH );  
  18.     memset( iv, 0x00, CryptoPP::AES::BLOCKSIZE );  
  19.   
  20.     // 或者也可以  
  21.     /* 
  22.     char tmpK[] = "1234567890123456"; 
  23.     char tmpIV[] = "1234567890123456"; 
  24.     for (int j = 0; j < CryptoPP::AES::DEFAULT_KEYLENGTH; ++j) 
  25.     { 
  26.         key[j] = tmpK[j]; 
  27.     } 
  28.  
  29.     for (int i = 0; i < CryptoPP::AES::BLOCKSIZE; ++i) 
  30.     { 
  31.         iv[i] = tmpIV[i]; 
  32.     } 
  33.     */  
  34. }  
  35.   
  36. string encrypt(string plainText)  
  37. {  
  38.     string cipherText;  
  39.   
  40.     //  
  41.     CryptoPP::AES::Encryption aesEncryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);  
  42.     CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption( aesEncryption, iv );  
  43.     CryptoPP::StreamTransformationFilter stfEncryptor(cbcEncryption, new CryptoPP::StringSink( cipherText ));  
  44.     stfEncryptor.Put( reinterpret_cast<const unsigned char*>( plainText.c_str() ), plainText.length() + 1 );  
  45.     stfEncryptor.MessageEnd();  
  46.   
  47.     string cipherTextHex;  
  48.     forint i = 0; i < cipherText.size(); i++ )  
  49.     {  
  50.         char ch[3] = {0};  
  51.         sprintf(ch, "%02x",  static_cast<byte>(cipherText[i]));  
  52.         cipherTextHex += ch;  
  53.     }  
  54.   
  55.     return cipherTextHex;  
  56. }  
  57.   
  58.   
  59.   
  60. void writeCipher(string output)  
  61. {  
  62.     ofstream out("/tmp/cipher.data");  
  63.     out.write(output.c_str(), output.length());  
  64.     out.close();  
  65.   
  66.     cout<<"writeCipher finish "<<endl<<endl;  
  67. }  
  68.   
  69.   
  70.   
  71. int main()  
  72. {  
  73.     string text = "hello zhuzhu dashen !";  
  74.     cout<<"text : "<<text<<endl;  
  75.   
  76.     initKV();  
  77.     string cipherHex = encrypt(text);  
  78.     cout<<"cipher : "<<cipherHex<<endl;  
  79.     writeCipher(cipherHex);  
  80.   
  81.     return 0;  
  82. }  


程序二:解密

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include <stdio.h>  
  2.   
  3. #include <iostream>  
  4. #include <fstream>  
  5. #include <sstream>  
  6.   
  7. #include <cryptopp/aes.h>  
  8. #include <cryptopp/filters.h>  
  9. #include <cryptopp/modes.h>  
  10.   
  11. using namespace std;  
  12.   
  13. byte key[ CryptoPP::AES::DEFAULT_KEYLENGTH ], iv[ CryptoPP::AES::BLOCKSIZE];  
  14.   
  15. void initKV()  
  16. {  
  17.     memset( key, 0x00, CryptoPP::AES::DEFAULT_KEYLENGTH );  
  18.     memset( iv, 0x00, CryptoPP::AES::BLOCKSIZE );  
  19.   
  20.     // 或者也可以  
  21.     /*  
  22.     char tmpK[] = "1234567890123456";  
  23.     char tmpIV[] = "1234567890123456";  
  24.     for (int j = 0; j < CryptoPP::AES::DEFAULT_KEYLENGTH; ++j)  
  25.     {  
  26.         key[j] = tmpK[j];  
  27.     }  
  28.   
  29.     for (int i = 0; i < CryptoPP::AES::BLOCKSIZE; ++i)  
  30.     {  
  31.         iv[i] = tmpIV[i];  
  32.     }  
  33.     */  
  34. }  
  35.   
  36. string decrypt(string cipherTextHex)  
  37. {  
  38.     string cipherText;  
  39.     string decryptedText;  
  40.   
  41.     int i = 0;  
  42.     while(true)  
  43.     {  
  44.         char c;  
  45.         int x;  
  46.         stringstream ss;  
  47.         ss<<hex<<cipherTextHex.substr(i, 2).c_str();  
  48.         ss>>x;  
  49.         c = (char)x;  
  50.         cipherText += c;  
  51.         if(i >= cipherTextHex.length() - 2)break;  
  52.         i += 2;  
  53.     }  
  54.   
  55.     //  
  56.     CryptoPP::AES::Decryption aesDecryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);  
  57.     CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption( aesDecryption, iv );  
  58.     CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink( decryptedText ));  
  59.     stfDecryptor.Put( reinterpret_cast<const unsigned char*>( cipherText.c_str() ), cipherText.size());  
  60.   
  61.     stfDecryptor.MessageEnd();  
  62.   
  63.     return decryptedText;  
  64. }  
  65.   
  66. string readCipher()  
  67. {  
  68.     ifstream in("/tmp/cipher.data");  
  69.   
  70.     string line;  
  71.     string decryptedText;  
  72.     while(getline(in, line))  
  73.     {  
  74.         if(line.length() > 1)  
  75.         {  
  76.             decryptedText += decrypt(line) + "\n";  
  77.         }  
  78.         line.clear();  
  79.     }  
  80.   
  81.     cout<<"readCipher finish "<<endl;  
  82.     in.close();  
  83.   
  84.     return decryptedText;  
  85. }  
  86.   
  87. int main()  
  88. {  
  89.     initKV();  
  90.     string text = readCipher();  
  91.     cout<<"text : "<<text<<endl;  
  92.     return 0;  
  93. }  


安装cryptopp: sudo apt-get install libcrypto++-dev
编译:g++ main.cpp -o main -lcryptopp

(以上内容仅供学习参考,若发现有误,请留言告知,谢谢!)

原创粉丝点击