-
-
[旧帖] 如何在cryptopp库中获取已经生成的RSA公钥和私钥 0.00雪花
-
发表于: 2013-8-14 11:13 3860
-
我对cryptopp库的API一点都不熟悉,所以不知道如何获取RSA公钥。我在网络上找到了下面的
RSA使用例子 ,下面的例子可以实现加密和解密,但是我不知道RSA公钥和私钥保存在那个类里面。
我获取公钥所对应的字符串. 例如 获取到这样的公钥对
n = CF 08 02 0E 96 AA F9 2C C7 22 B1 FB B2 29 77 FF 62 30 17 E9 51 8D AC 9B 0D CA 58 51 47 28 80 58
1A F3 C2 74 97 2D ED FE 20 6B 11 2C 0E BA 36 AD 66 F0 45 36 FB 13 94 F8 C6 7D EB 93 F5 AE EB E7
e = 03
谢谢。麻烦给个例子。
//------------------------
// 主程序
//------------------------
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;
getchar();
}
//------------------------
// 生成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;
}
RSA使用例子 ,下面的例子可以实现加密和解密,但是我不知道RSA公钥和私钥保存在那个类里面。
我获取公钥所对应的字符串. 例如 获取到这样的公钥对
n = CF 08 02 0E 96 AA F9 2C C7 22 B1 FB B2 29 77 FF 62 30 17 E9 51 8D AC 9B 0D CA 58 51 47 28 80 58
1A F3 C2 74 97 2D ED FE 20 6B 11 2C 0E BA 36 AD 66 F0 45 36 FB 13 94 F8 C6 7D EB 93 F5 AE EB E7
e = 03
谢谢。麻烦给个例子。
//------------------------
// 主程序
//------------------------
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;
getchar();
}
//------------------------
// 生成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;
}
[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课
赞赏
他的文章
- [原创]TikTok越狱检测之二 <调试器检测> 4253
- [原创]TikTok越狱检测之一 <代码模块污染检测> 6328
- [原创]某直播平台协议分析之一 30288
- [求助]IDA 远程调试Mac 应用错误,求指导 8741
- [求助]如何在mac 系统下面利用ida 调试 软件呢 7337
看原图
赞赏
雪币:
留言: