从字符串 Crypto++ 导入 RSA 公钥/私钥

Import RSA public/private key from a string Crypto++

本文关键字:公钥 私钥 RSA 导入 字符串 Crypto++      更新时间:2023-10-16

我想直接从字符串变量导入RSA密钥,以将其与Crypto++library一起使用。

代码如下所示: 这是我想做的一个例子,而不是一个工作代码。

std::string publickey_str =
"-----BEGIN PUBLIC KEY-----"
"gm6mZA1NTZQJVUk9AGDb6NRngzRlRAgXBTWAispwlqsuHFoCrv02xPm1uxkLyfUq"
"LoA4/EQJ25okjmGkrjgak+XmQIPKmAg94gWAtvRIrLZNmCj/aPeuikmCPXkKtg2b"
"pdB6xzHY0ftGu0l6Vb8zttg7Wfo1kJowjoqCRwo9ex/IKwPXxE3UsugshcZOGdqT"
"6E3B/Vw+JoerL/LfeOU2OYcSFEXsWqjzkrGzEVuKzRnve5RlXyY0gShP33f+hDnC"
"F+Uu2tFfFgxRkdQPk7AKm4MCAwEAAQ=="
"-----END PUBLIC KEY-----";
RSA::PublicKey publicKey;
publicKey.load(publickey_str);

在Crypto++维基中,我只找到如何从/导出密钥(.der,.pem(。你知道我怎么能用字符串做同样的事情吗?

编辑:我很抱歉,我忘了告诉我缩短了示例的RSA公钥。

我只找到如何从/导出密钥(.der,.pem(。你知道我怎么能用字符串做同样的事情吗?

在 Crypto++ 中,您可以将一个源交换为任何其他源。您可以更改NetworkSourceStringSourceArraySource等的FileSource

这同样适用于过滤器。HexEncoderBase64EncoderHexDecoderBase64Decoder和所有可互换的过滤器。它不仅限于编码器,您也可以换入加密过滤器、签名过滤器或验证过滤器。它们可以换入和换出,因为它们都实现了BufferedTransformation接口。

这同样适用于水槽。您可以更改NetworkSinkStringSinkArraySink等的FileSink


您显示的密钥是 PEM 编码的密钥。要在 PEM 中进行编码和解码,您需要 PEM 包。它不是库本身的一部分,因此它可能在您的库副本中丢失。相反,PEM 包是由社区维护的附加组件,您必须下载并构建它。

要使用 PEM 包,您需要从源代码重建库。要设置内容:

$ cd cryptopp
$ wget https://www.cryptopp.com/w/images/5/5a/Pem-pack.zip
--2017-08-05 16:30:26--  https://www.cryptopp.com/w/images/5/5a/Pem-pack.zip
Resolving www.cryptopp.com (www.cryptopp.com)... 144.217.231.241
Connecting to www.cryptopp.com (www.cryptopp.com)|144.217.231.241|:443...
...
2017-08-05 16:30:26 (862 KB/s) - ‘Pem-pack.zip’ saved [20769/20769]
$ unzip -aoq Pem-pack.zip
$ ls pem*
pem-com.cpp  pem-create-keys.sh  pem-rd.cpp    pem-verify-keys.sh
pem-com.h    pem.h               pem-test.cxx  pem-wr.cpp

然后,像往常一样制作:

$ make distclean
...
$ make -j 9
g++ -DNDEBUG -g2 -O3 -fPIC -march=native -pipe -c cryptlib.cpp
g++ -DNDEBUG -g2 -O3 -fPIC -march=native -pipe -c cpu.cpp
g++ -DNDEBUG -g2 -O3 -fPIC -march=native -pipe -c integer.cpp
...
g++ -DNDEBUG -g2 -O3 -fPIC -march=native -pipe -c pem-com.cpp
g++ -DNDEBUG -g2 -O3 -fPIC -march=native -pipe -c pem-rd.cpp
g++ -DNDEBUG -g2 -O3 -fPIC -march=native -pipe -c pem-wr.cpp
...
ar r libcryptopp.a cryptlib.o cpu.o integer.o 3way.o ... zdeflate.o zinflate.o zlib.o

注意:我需要修复auto_ptr警告。我今天晚些时候会谈到它。此问题已修复。PEM Pack 的新版本可从 wiki 获得。

现在,您拥有 PEM 支持:

$ nm libcryptopp.a | grep PEM | grep ' T ' | c++filt
00000000000000a0 T CryptoPP::PEM_WriteLine(CryptoPP::BufferedTransformation&, CryptoPP::SecBlock<unsigned char, CryptoPP::AllocatorWithCleanup<unsigned char, false> > const&)
00000000000000f0 T CryptoPP::PEM_WriteLine(CryptoPP::BufferedTransformation&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)
0000000000000140 T CryptoPP::PEM_Base64Decode(CryptoPP::BufferedTransformation&, CryptoPP::BufferedTransformation&)
00000000000004e0 T CryptoPP::PEM_Base64Encode(CryptoPP::BufferedTransformation&, CryptoPP::BufferedTransformation&)
...

这是代码。

$ cat pem-test.cxx
#include <string>
#include <iostream>
#include "integer.h"
#include "rsa.h"
#include "pem.h"
using namespace CryptoPP;
int main(int argc, char* argv[])
{
const std::string publickey_str =
"-----BEGIN PUBLIC KEY-----n"
"gm6mZA1NTZQJVUk9AGDb6NRngzRlRAgXBTWAispwlqsuHFoCrv02xPm1uxkLyfUqn"
"LoA4/EQJ25okjmGkrjgak+XmQIPKmAg94gWAtvRIrLZNmCj/aPeuikmCPXkKtg2bn"
"pdB6xzHY0ftGu0l6Vb8zttg7Wfo1kJowjoqCRwo9ex/IKwPXxE3UsugshcZOGdqTn"
"6E3B/Vw+JoerL/LfeOU2OYcSFEXsWqjzkrGzEVuKzRnve5RlXyY0gShP33f+hDnCn"
"F+Uu2tFfFgxRkdQPk7AKm4MCAwEAAQ==n"
"-----END PUBLIC KEY-----n";
RSA::PublicKey publicKey;
try
{
StringSource source(publickey_str, true);
PEM_Load(source, publicKey);
}
catch(const Exception& ex)
{
std::cerr << ex.what() << std::endl;
return 1;
}
const Integer& e = publicKey.GetPublicExponent();
std:: cout << e << std::endl;
const Integer& n = publicKey.GetModulus();
std:: cout << n << std::endl;
return 0;
}

结果如下:

$ ./test.exe
BER decode error

看起来您的公钥坏了。我不会在上面浪费时间,因为我不清楚它是真正的钥匙,还是你编造的随机垃圾。


关于这个:

$ unzip -aoq Pem-pack.zip
$ ls pem*
pem-com.cpp  pem-create-keys.sh  pem-rd.cpp    pem-verify-keys.sh
pem-com.h    pem.h               pem-test.cxx  pem-wr.cpp

我使用脚本进行测试。如果需要,可以删除它们。如果需要,也可以删除pem-test.cxx。也不需要。

$ rm pem-*.sh pem-test.cxx

这就是我将字符串加载到公钥中的方式

//Create Cryptopp StringSource From Std::string
std::string PublicKeyString = "<Your key as std::string value>";
CryptoPP::StringSource PKeyStringSource(PublicKeyString, true);
CryptoPP::RSA::PublicKey publicKey;
publicKey.Load(PKeyStringSource);

查看整个功能以进行详细使用。在这里,我将密钥作为函数中的 std::string 传递以验证签名。RSA::PublicKey是从传递到函数std::string创建的

bool Signature::VerifySignature(const std::string &PublicKeyString,
const std::string &data,
const std::string &SignatureStr)
{
CryptoPP::StringSource PKeyStringSource(PublicKeyString, true);
CryptoPP::StringSource SignStringSource(SignatureStr, true);
CryptoPP::RSA::PublicKey publicKey;
publicKey.Load(PKeyStringSource);
// verify message
bool result = false;
m_verifier.AccessPublicKey().Load(SignStringSource);
CryptoPP::StringSource ss2(SignatureStr + data, true,
new CryptoPP::SignatureVerificationFilter(m_verifier,
new CryptoPP::ArraySink((CryptoPP::byte*)&result,
sizeof(result))));
return result;
}