crypto++刚开始也不知道是什么,只是设计过程中需要用crypto++来实现RSA算法的加解密。
首先应该了解crypto++的基本干什么的,这里不加以赘述,可以找更加官方的描述,当然官网找资料是最好不过了。
如果项目中需要用到,需要下载一下官方提供的下载包,下载后来然后安装。简要介绍一下安装步骤:
第一步:解压下载的包,找到cryptest.sln的工程,然后用visual studio 2005(或更高版本,vc6.0找到dsw工程)打开,然后,可以看到有四个子工程,这里,我们只需要将cryptolib进行build一下,然后会在win32的output/debug目录下找到cryptlib.lib文件,这个是我们编程的时候要用到的。
第二步:新建一个win32 console application工程,暂且命名为crypto1。然后为工程添加两个文件夹,include和lib文件,为lib文件添加现有项,将cryptlib.lib文件导入进去,然后将crypto++下载包的所有.h文件添加到include文件夹下。
第三步:修改工程属性。“项目”->"属性"->"C/C++"->"常规"->"附加包含目录",将include目录包含进去,先应用一下;同样找到“C/C++”->“代码生成”,运行时库改为“MTD”,集中方式的区别可以自己百度;找到“链接器”->"常规"->"附加库目录",这里将lib库包含进来;在“链接器”->“命令行”,在附加选项中添加cryptlib.lib,点击确定,工程属性设置完毕了。
第四步:编程开始调试。可以在网上找一些好的例子拿来运行。这里贴出,我的一个例子,是可以运行的。
#include "stdafx.h"
#include "randpool.h"
#include "rsa.h"
#include "hex.h"
#include "files.h"
#include <iostream>
using namespace std;
using namespace CryptoPP;
#pragma comment(lib, "cryptlib.lib")
void GenerateRSAKey(unsigned int keyLength, const char *privFilename, const char *pubFilename, const char *seed);
string RSAEncryptString(const char *pubFilename, const char *seed, const char *message);
string RSADecryptString(const char *privFilename, const char *ciphertext);
RandomPool & GlobalRNG();
int _tmain(int argc, _TCHAR* argv[])
{
char priKey[128] = {0};
char pubKey[128] = {0};
char seed[1024] = {0};
// 生成 RSA 密钥对
strcpy(priKey, "pri"); // 生成的私钥文件
strcpy(pubKey, "pub"); // 生成的公钥文件
strcpy(seed, "seed");
GenerateRSAKey(1024, priKey, pubKey, seed);
// RSA 加解密
char message[1024] = {0};
cout<<"Origin Text:\t"<<"Hello World!"<<endl<<endl;
strcpy(message, "Hello World!");
string encryptedText = RSAEncryptString(pubKey, seed, message); // RSA 加密 [Page]
cout<<"Encrypted Text:\t"<<encryptedText<<endl<<endl;
string decryptedText = RSADecryptString(priKey, encryptedText.c_str()); // RSA 解密
cout<<"Decrypted Text:\t"<<decryptedText<<endl<<endl;
system("pause");
return 0;
}
//------------------------
// 生成RSA密钥对
//------------------------
void GenerateRSAKey(unsigned int keyLength, const char *privFilename, const char *pubFilename, const char *seed)
{
RandomPool randPool;
randPool.Put((byte *)seed, strlen(seed));
RSAES_OAEP_SHA_Decryptor priv(randPool, keyLength);
HexEncoder privFile(new FileSink(privFilename));
priv.DEREncode(privFile);
privFile.MessageEnd();
RSAES_OAEP_SHA_Encryptor pub(priv);
HexEncoder pubFile(new FileSink(pubFilename));
pub.DEREncode(pubFile);
pubFile.MessageEnd();
}
//------------------------
// RSA加密
//------------------------
string RSAEncryptString(const char *pubFilename, const char *seed, const char *message)
{
FileSource pubFile(pubFilename, true, new HexDecoder);
RSAES_OAEP_SHA_Encryptor pub(pubFile);
RandomPool randPool;
randPool.Put((byte *)seed, strlen(seed));
string result;
StringSource(message, true, new PK_EncryptorFilter(randPool, pub, new HexEncoder(new StringSink(result))));
return result;
}
//------------------------
// RSA解密
//------------------------
string RSADecryptString(const char *privFilename, const char *ciphertext)
{
FileSource privFile(privFilename, true, new HexDecoder);
RSAES_OAEP_SHA_Decryptor priv(privFile);
string result;
StringSource(ciphertext, true, new HexDecoder(new PK_DecryptorFilter(GlobalRNG(), priv, new StringSink(result))));
return result;
}
//------------------------
// 定义全局的随机数池
//------------------------
RandomPool & GlobalRNG()
{
static RandomPool randomPool;
return randomPool;
}
如果,出现包含文件找不到,那么你的.h文件没有包含成功;如果是报库不兼容的话,就是你的工程属性没有设置成功,按照上面说的办法设置一下就没问题的!