——————————————————说明:部分内容参考网上现有。————————————————--
——————此文适合新手入门,大神直接飞过————————
————感想,可跳过哈 ,^_^————
这几天老师要求做一个关于rsa加密的项目,要求加解密文本内容。主要遇到密钥生成、待加解密字符串过长等遇到问题,下面会一一解决。网上查看资料大多是每次都生成新的密钥,而且加密的明文较短,所以第一次有了写博客的冲动哈!~~~~~
—————进入主题 ! ———————
一、crypto++sdk,环境及项目的配置,参照:http://www.cnblogs.com/cxun/archive/2008/07/30/743541.html
二、生成固定密钥,参照crypto++官方文档:http://www.cryptopp.com/wiki/Keys_and_Formats#Generating.2C_Validating.2C_Saving.2C_and_Loading_Keys
所用代码示例也为官方sample,稍稍改动了一点点,在代码中会有说明。(别被吓唬了,其实慢慢看还是很容易的)
必须现在c盘建一个rsa05的文件夹,否则会出错。出错点在下面代码中有标注。(之前没有建立这个文件夹,程序会中断抛出异常。)
#include <iostream>
using std::cout;
using std::cerr;
using std::endl;
#include <string>
using std::string;
#include <stdexcept>
using std::runtime_error;
#include <queue.h>
using CryptoPP::ByteQueue;
#include "filters.h"
using CryptoPP::StringSink;
using CryptoPP::StringSource;
using CryptoPP::PK_EncryptorFilter;
using CryptoPP::PK_DecryptorFilter;
#include <files.h>
using CryptoPP::FileSource;
using CryptoPP::FileSink;
#include "dsa.h"
using CryptoPP::DSA;
#include "rsa.h"
using CryptoPP::RSA;
using CryptoPP::InvertibleRSAFunction;
using CryptoPP::RSAES_OAEP_SHA_Encryptor;
using CryptoPP::RSAES_OAEP_SHA_Decryptor;
#include <cryptlib.h>
using CryptoPP::PrivateKey;
using CryptoPP::PublicKey;
using CryptoPP::BufferedTransformation;
#include "osrng.h"
using CryptoPP::AutoSeededRandomPool;
#pragma comment(lib, "cryptlib.lib");
void EncodePrivateKey(const string& filename, const RSA::PrivateKey& key);
void EncodePublicKey(const string& filename, const RSA::PublicKey& key);
void Encode(const string& filename, const BufferedTransformation& bt);
void DecodePrivateKey(const string& filename, RSA::PrivateKey& key);
void DecodePublicKey(const string& filenam