crypto++7.0 RSA加密

#include "randpool.h"
#include "rsa.h"
#include "hex.h"
#include "files.h"


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_s(priKey, "pri");  // 生成的私钥文件
strcpy_s(pubKey, "pub");  // 生成的公钥文件
strcpy_s(seed, "seed");
cout << "priKey: " << priKey << endl;
cout << "pubKey: " << pubKey << endl;
GenerateRSAKey(1024, priKey, pubKey, seed);

// RSA 加解密
char message[1024] = { 0 };
cout << "Origin Text:\t" << "Hello World!" << endl << endl;
strcpy_s(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;
system("pause");
}

//------------------------
// 生成RSA密钥对
//------------------------
void GenerateRSAKey(unsigned int keyLength, const char *privFilename, const char *pubFilename, const char *seed){
RandomPool randPool;
randPool.IncorporateEntropy((byte *)seed, strlen(seed));

RSAES_OAEP_SHA_Decryptor priv(randPool, keyLength);
HexEncoder privFile(new FileSink(privFilename));
priv.AccessMaterial().Save(privFile);
privFile.MessageEnd();

RSAES_OAEP_SHA_Encryptor pub(priv);
HexEncoder pubFile(new FileSink(pubFilename));
pub.AccessMaterial().Save(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.IncorporateEntropy((byte *)seed, strlen(seed));

std::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);

std::string result;
StringSource(ciphertext, true, new HexDecoder(new PK_DecryptorFilter(GlobalRNG(), priv, new StringSink(result))));
return result;
}

//------------------------
// 定义全局的随机数池
//------------------------
RandomPool & GlobalRNG(){
static RandomPool randomPool;
return randomPool;
}

http://blog.sina.com.cn/s/blog_607787d30102x91p.html

 

https://blog.csdn.net/phker/article/details/5056288

还可以参考这里

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值