Crypto++库在VS 2008中的使用——RSA加解密

2024-06-15 01:48
文章标签 使用 vs rsa 2008 crypto 加解密

本文主要是介绍Crypto++库在VS 2008中的使用——RSA加解密,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

        源代码:下载 

 

一.   下载Crypto++ Library

 

Crypto++ Library的官方网:http://www.cryptopp.com/

 

二.   建立自己使用的Crypto++ Library

 

由于从官方网下载的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 <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();

 

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

// 主程序

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

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!"<<endl<<endl;

    strcpy(message, "Hello World!");

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

    cout<<"Encrypted Text:\t"<<encryptedText<<endl<<endl;

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

    cout<<"Decrypted Text:\t"<<decryptedText<<endl<<endl;

}

 

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

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

}

 

2.       设置工程属性

选择工程属性(Alt + F7):

(1)“Configuration Properties”→“C/C++” →“General”,右边的“Additional Include Directories”设置为上面建好的Crypto++ SDK的Include文件夹,“C:\Program Files\CyptoPP\include”;

(2) “Configuration Properties”→“Linker” →“General”,右边的“Additional Library Directories”设置为上面建好的Crypto++ SDK的Lib\Debug文件夹,“C:\Program Files\CyptoPP\lib\debug”(Release模式下对应着Release文件夹);

(3) “Configuration Properties”→“C/C++” →“Code Generation”,右边的“Runtime Library”设置为“Multi-threaded Debug (/MTd)”(Release模式下对应着“Multi-threaded (/MT)”)

 

3.       运行程序(Ctrl + F5)

正常运行的输出结果为:

 

Origin Text:    Hello World!

Encrypted Text: 79C72A482482EF45111F961772456310792AB735ECF72329ECB26292D2B26374
824E0E35D24A63CB03B867DD2C70B001FD4B2B33FBC984BD229A5226F284B889901817976A680322
9E8351372C5E28E8BEBA2A94E7CF61A8A162F0BA2F3E0C35D26842D92EC4866D25E6BF878743E481
84D9F6FF9BA690F953568D017C02D540

Decrypted Text: Hello World! 

 

如果上面的第(3)步没有设置则会出现以下链接错误:

cryptlib.lib(randpool.obj) : error LNK2005: "public: __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >(char const *)" (??0?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAE@PBD@Z) already defined in msvcprtd.lib(MSVCP80D.dll)

说在msvcprtd.lib和MSVCRTD.lib中已经定义过。

这篇关于Crypto++库在VS 2008中的使用——RSA加解密的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1062094

相关文章

使用Python删除Excel中的行列和单元格示例详解

《使用Python删除Excel中的行列和单元格示例详解》在处理Excel数据时,删除不需要的行、列或单元格是一项常见且必要的操作,本文将使用Python脚本实现对Excel表格的高效自动化处理,感兴... 目录开发环境准备使用 python 删除 Excphpel 表格中的行删除特定行删除空白行删除含指定

深入理解Go语言中二维切片的使用

《深入理解Go语言中二维切片的使用》本文深入讲解了Go语言中二维切片的概念与应用,用于表示矩阵、表格等二维数据结构,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧... 目录引言二维切片的基本概念定义创建二维切片二维切片的操作访问元素修改元素遍历二维切片二维切片的动态调整追加行动态

prometheus如何使用pushgateway监控网路丢包

《prometheus如何使用pushgateway监控网路丢包》:本文主要介绍prometheus如何使用pushgateway监控网路丢包问题,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录监控网路丢包脚本数据图表总结监控网路丢包脚本[root@gtcq-gt-monitor-prome

Python通用唯一标识符模块uuid使用案例详解

《Python通用唯一标识符模块uuid使用案例详解》Pythonuuid模块用于生成128位全局唯一标识符,支持UUID1-5版本,适用于分布式系统、数据库主键等场景,需注意隐私、碰撞概率及存储优... 目录简介核心功能1. UUID版本2. UUID属性3. 命名空间使用场景1. 生成唯一标识符2. 数

SpringBoot中如何使用Assert进行断言校验

《SpringBoot中如何使用Assert进行断言校验》Java提供了内置的assert机制,而Spring框架也提供了更强大的Assert工具类来帮助开发者进行参数校验和状态检查,下... 目录前言一、Java 原生assert简介1.1 使用方式1.2 示例代码1.3 优缺点分析二、Spring Fr

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

java使用protobuf-maven-plugin的插件编译proto文件详解

《java使用protobuf-maven-plugin的插件编译proto文件详解》:本文主要介绍java使用protobuf-maven-plugin的插件编译proto文件,具有很好的参考价... 目录protobuf文件作为数据传输和存储的协议主要介绍在Java使用maven编译proto文件的插件

SpringBoot线程池配置使用示例详解

《SpringBoot线程池配置使用示例详解》SpringBoot集成@Async注解,支持线程池参数配置(核心数、队列容量、拒绝策略等)及生命周期管理,结合监控与任务装饰器,提升异步处理效率与系统... 目录一、核心特性二、添加依赖三、参数详解四、配置线程池五、应用实践代码说明拒绝策略(Rejected

C++ Log4cpp跨平台日志库的使用小结

《C++Log4cpp跨平台日志库的使用小结》Log4cpp是c++类库,本文详细介绍了C++日志库log4cpp的使用方法,及设置日志输出格式和优先级,具有一定的参考价值,感兴趣的可以了解一下... 目录一、介绍1. log4cpp的日志方式2.设置日志输出的格式3. 设置日志的输出优先级二、Window

Ubuntu如何分配​​未使用的空间

《Ubuntu如何分配​​未使用的空间》Ubuntu磁盘空间不足,实际未分配空间8.2G因LVM卷组名称格式差异(双破折号误写)导致无法扩展,确认正确卷组名后,使用lvextend和resize2fs... 目录1:原因2:操作3:报错5:解决问题:确认卷组名称​6:再次操作7:验证扩展是否成功8:问题已解