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、程序一:加密
- #include <stdio.h>
-
- #include <iostream>
- #include <fstream>
- #include <sstream>
-
- #include <cryptopp/aes.h>
- #include <cryptopp/filters.h>
- #include <cryptopp/modes.h>
-
- using namespace std;
-
- byte key[ CryptoPP::AES::DEFAULT_KEYLENGTH ], iv[ CryptoPP::AES::BLOCKSIZE];
-
- void initKV()
- {
- memset( key, 0x00, CryptoPP::AES::DEFAULT_KEYLENGTH );
- memset( iv, 0x00, CryptoPP::AES::BLOCKSIZE );
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
-
- string encrypt(string plainText)
- {
- string cipherText;
-
-
- CryptoPP::AES::Encryption aesEncryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
- CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption( aesEncryption, iv );
- CryptoPP::StreamTransformationFilter stfEncryptor(cbcEncryption, new CryptoPP::StringSink( cipherText ));
- stfEncryptor.Put( reinterpret_cast<const unsigned char*>( plainText.c_str() ), plainText.length() + 1 );
- stfEncryptor.MessageEnd();
-
- string cipherTextHex;
- for( int i = 0; i < cipherText.size(); i++ )
- {
- char ch[3] = {0};
- sprintf(ch, "%02x", static_cast<byte>(cipherText[i]));
- cipherTextHex += ch;
- }
-
- return cipherTextHex;
- }
-
-
-
- void writeCipher(string output)
- {
- ofstream out("/tmp/cipher.data");
- out.write(output.c_str(), output.length());
- out.close();
-
- cout<<"writeCipher finish "<<endl<<endl;
- }
-
-
-
- int main()
- {
- string text = "hello zhuzhu dashen !";
- cout<<"text : "<<text<<endl;
-
- initKV();
- string cipherHex = encrypt(text);
- cout<<"cipher : "<<cipherHex<<endl;
- writeCipher(cipherHex);
-
- return 0;
- }
程序二:解密- #include <stdio.h>
-
- #include <iostream>
- #include <fstream>
- #include <sstream>
-
- #include <cryptopp/aes.h>
- #include <cryptopp/filters.h>
- #include <cryptopp/modes.h>
-
- using namespace std;
-
- byte key[ CryptoPP::AES::DEFAULT_KEYLENGTH ], iv[ CryptoPP::AES::BLOCKSIZE];
-
- void initKV()
- {
- memset( key, 0x00, CryptoPP::AES::DEFAULT_KEYLENGTH );
- memset( iv, 0x00, CryptoPP::AES::BLOCKSIZE );
-
- // 或者也可以
- /*
- char tmpK[] = "1234567890123456";
- char tmpIV[] = "1234567890123456";
- for (int j = 0; j < CryptoPP::AES::DEFAULT_KEYLENGTH; ++j)
- {
- key[j] = tmpK[j];
- }
-
- for (int i = 0; i < CryptoPP::AES::BLOCKSIZE; ++i)
- {
- iv[i] = tmpIV[i];
- }
- */
- }
-
- string decrypt(string cipherTextHex)
- {
- string cipherText;
- string decryptedText;
-
- int i = 0;
- while(true)
- {
- char c;
- int x;
- stringstream ss;
- ss<<hex<<cipherTextHex.substr(i, 2).c_str();
- ss>>x;
- c = (char)x;
- cipherText += c;
- if(i >= cipherTextHex.length() - 2)break;
- i += 2;
- }
-
- //
- CryptoPP::AES::Decryption aesDecryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
- CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption( aesDecryption, iv );
- CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink( decryptedText ));
- stfDecryptor.Put( reinterpret_cast<const unsigned char*>( cipherText.c_str() ), cipherText.size());
-
- stfDecryptor.MessageEnd();
-
- return decryptedText;
- }
-
- string readCipher()
- {
- ifstream in("/tmp/cipher.data");
-
- string line;
- string decryptedText;
- while(getline(in, line))
- {
- if(line.length() > 1)
- {
- decryptedText += decrypt(line) + "\n";
- }
- line.clear();
- }
-
- cout<<"readCipher finish "<<endl;
- in.close();
-
- return decryptedText;
- }
-
- int main()
- {
- initKV();
- string text = readCipher();
- cout<<"text : "<<text<<endl;
- return 0;
- }
安装cryptopp: sudo apt-get install libcrypto++-dev编译:g++ main.cpp -o main -lcryptopp(以上内容仅供学习参考,若发现有误,请留言告知,谢谢!)