Crypto++学习总结---RSA
来源:互联网 发布:苏联陈兵百万知乎 编辑:程序博客网 时间:2024/05/16 11:16
静态库下载连接Cryp++ lib 下载
RSA使用方法如下:
#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 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 加密 [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; }
- Crypto++学习总结---RSA
- Crypto++学习总结---RSA
- Crypto++学习总结---MD5
- Crypto++学习总结---AES
- Crypto++学习总结---DES
- Crypto++学习总结---AES
- Crypto++学习总结--算法介绍
- Crypto++学习总结--算法介绍
- Crypto++入门学习笔记(DES、AES、RSA、SHA-256)
- Crypto++入门学习笔记(DES、AES、RSA、SHA-256)
- Crypto++入门学习笔记(DES、AES、RSA、SHA-256)
- Crypto++入门学习笔记(DES、AES、RSA、SHA-256)
- Crypto++入门学习笔记(DES、AES、RSA、SHA-256)
- Crypto++入门学习笔记(DES、AES、RSA、SHA-256)
- Crypto++入门学习笔记(DES、AES、RSA、SHA-256)
- Crypto++入门学习笔记(DES、AES、RSA、SHA-256)
- Crypto++入门学习笔记(DES、AES、RSA、SHA-256)
- Crypto++入门学习笔记(DES、AES、RSA、SHA-256)
- #小练习 pickle模块 _ version3
- C#高级编程 string
- 关于select函数中timeval和fd_s重新设置的问题
- 深入Struts2的过滤器FilterDispatcher中文乱码及字符编码过滤器
- equals()与==
- Crypto++学习总结---RSA
- 阻塞、非阻塞、同步、异步浅析
- 卫计委:全面推进免费孕前优生健康检查全覆盖
- 绑定远程服务
- Android 百度地图开发(一)--- 申请API Key和在项目中显示百度地图
- Protocol Buffers定义协议格式
- HDU 4417 Super Mario (树状数组+离线处理)(划分树+二分答案)
- gsoap工具生成soap.h.cpp文件遇到的问题
- C# 转义字符