【发布时间】:2016-10-19 02:05:35
【问题描述】:
我在使用 Crypto++ 保存 RSA 公钥字符串时遇到问题。在解码密钥时,我总是得到一个 BERDecodeErr 异常。
代码:
string RsaEncryptor::encryptor(string plaintext, string publicKey)
{
std::string cipher;
AutoSeededRandomPool prng;
try
{
ByteQueue queue;
Base64Decoder decoder(new Redirector(queue));
decoder.Put((const byte *) publicKey.data(), publicKey.size());
decoder.MessageEnd();
RSA::PublicKey rsaPublick;
rsaPublick.BERDecodePublicKey(queue, false, (size_t) queue.MaxRetrievable());
// BERDecodePrivateKey is a void function. Here's the only check
// we have regarding the DER bytes consumed.
CRYPTOPP_ASSERT(queue.IsEmpty());
bool valid = rsaPublick.Validate(prng, 3);
if (!valid)
cipher = "RSA private key is not valid";
RSAES_OAEP_SHA_Encryptor e(rsaPublick);
StringSource ss(plaintext, true,
new PK_EncryptorFilter(prng, e,
new StringSink(cipher)
) // PK_EncryptorFilter
); // StringSource
}
catch (CryptoPP::Exception &e) {
cipher = e.what();
}
return cipher;
}
公钥是:
MIGdMA0GCSqGSIb3DQEBAQUAA4GLADCBhwKBgQDs648aASMAR9VprkzNVS7b36N1hiYvbBG0c
dE0QkS3H/sc3+Ej92lGBQErpBu9LVhwN/beBX4QnbCn1eNSrKoOzS4yqWlwOaCe0WLmFDHCn1
cMTkX89cT4A0pcjBbY+0W7htxWcqHxEQH9x/AjQ9/4blerh1i6/lLIo6hn2hB8kQIB
【问题讨论】:
-
请出示公钥或私钥以供测试。我怀疑您需要使用
Load和Save,但我需要查看密钥才能确定。另请参阅 Crypto++ wiki 上的 Keys and Formats。另请参阅 Crypto++ 手册中的ASN1CryptoMaterial<T>。ASN1CryptoMaterial<T>提供了Load和Save,它使用OID 来描述密钥(subjectPublicKeyInfo和私钥)。最后,您可能对PEM Pack感兴趣。 -
@ JWW这是公钥:MIGdMA0GCSqGSIb3DQEBAQUAA4GLADCBhwKBgQDs648aASMAR9VprkzNVS7b36N1hiYvbBG0cdE0QkS3H / SC3 + Ej92lGBQErpBu9LVhwN / beBX4QnbCn1eNSrKoOzS4yqWlwOaCe0WLmFDHCn1cMTkX89cT4A0pcjBbY + 0W7htxWcqHxEQH9x / AjQ9 / 4blerh1i6 / lLIo6hn2hB8kQIB 跨度>
-
打扰了,请看我这里的详细描述:[groups.google.com/forum/#!topic/cryptopp-users/4ofdfSW9588]
-
顺便说一句...当你得到一个格式良好的密钥时,你应该使用
rsaPublick.Load。
标签: android android-ndk rsa crypto++