设为首页 加入收藏

TOP

Crypto++ 的使用方法 (一)
2014-11-23 22:53:50 来源: 作者: 【 】 浏览:2
Tags:Crypto 使用方法

从官方网下载的Crypto++库是开源的,只有源文件和几个可以生成lib、dll的工程,以及一个使用的例子工程,因此希望生成自己建的工程能使用的SDK。

1.编译链接生成cryptlib.lib

打开cryptest.sln,分别在Debug模式和Release模式下编译链接cryptlib工程,成功后会在cryptopp54\\Win32\\output\\debug和cryptopp54\\Win32\\output\\release下生成cryptlib.lib文件。作者当时用的是Crypto++ 5.4版本。

Build时方法是,右击Solution Explorer中的cryptlib工程,单击build。第一次时它会报错说“d:\\cryptopp54\\adler32.cpp(3) : fatal error C1033: cannot open program database ’d:\\cryptopp54\\win32\\cryptlib\\debug\\vc80.idb’”,没关系,按这样再build一次,就可以build成功了。

2.建立Crypto++ SDK

在C:\\Program Files\\中新建文件夹,取名“CryptoPP”,里面新建文件夹“include”、“lib”,在“lib”中新建文件夹“debug”、“release”。将Crypto++库中的所有头文件复制到“include”文件夹中,再将上面生成的两个cryptlib.lib分别复制到“debug”和“release”中。

三.RSA加解密

1.在VS 2005中新建Win32 Console Application工程,建立空的工程。完成后新建文件main.cpp,里面源码如下:

#include \"randpool.h\"

#include \"rsa.h\"

#include \"hex.h\"

#include \"files.h\"

#include

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();

//------------------------

// 主程序

//------------------------

void main()

{

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!\"<

strcpy(message, \"Hello World!\");

string encryptedText = RSAEncryptString(pubKey, seed, message); // RSA 加密[Page]

cout<<\"Encrypted Text:\\t\"<

string decryptedText = RSADecryptString(priKey, encryptedText.c_str()); // RSA 解密

cout<<\"Decrypted Text:\\t\"<

}

//------------------------

// 生成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)

{

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇MFC各种指针的获取 下一篇 fork与进程地址空间

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: