我只知道如何从文件(.der、.pem)导入/导出密钥。你知道我怎样才能用字符串做同样的事情吗?
在 Crypto++ 中,您可以将一个来源交换为任何其他来源。您可以将FileSource 更改为NetworkSource、StringSource、ArraySource 等。
这同样适用于过滤器。 HexEncoder、Base64Encoder、HexDecoder、Base64Decoder 和所有可互换的过滤器。它不仅限于编码器,您也可以换入加密过滤器、签名过滤器或验证过滤器。因为它们都实现了BufferedTransformation 接口,所以它们可以被换入和换出。
这同样适用于水槽。您可以将FileSink 更改为NetworkSink、StringSink、ArraySink 等。
您显示的密钥是 PEM 编码的密钥。要在 PEM 中编码和解码,您需要 PEM Pack。它不是库的一部分,因此它可能从您的库副本中丢失。相反,PEM Pack 是由社区维护的附加组件,您必须下载并构建它。
要使用 PEM Pack,您需要从源代码重建库。设置:
$ 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"
"gm6mZA1NTZQJVUk9AGDb6NRngzRlRAgXBTWAispwlqsuHFoCrv02xPm1uxkLyfUq\n"
"LoA4/EQJ25okjmGkrjgak+XmQIPKmAg94gWAtvRIrLZNmCj/aPeuikmCPXkKtg2b\n"
"pdB6xzHY0ftGu0l6Vb8zttg7Wfo1kJowjoqCRwo9ex/IKwPXxE3UsugshcZOGdqT\n"
"6E3B/Vw+JoerL/LfeOU2OYcSFEXsWqjzkrGzEVuKzRnve5RlXyY0gShP33f+hDnC\n"
"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