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

这阵子写了一些数据加密的小程序,对比了好几种算法后,选择了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、程序一:加密

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


程序二:解密

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


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

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

猜你喜欢

转载自blog.csdn.net/p312011150/article/details/80847913