我有一个用java编写的服务器,它生成RSA密钥对。我想在C ++客户端中使用私钥进行解密。
这是我用来创建私钥的代码:
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(4096);
KeyPair keyPair = keyGen.genKeyPair();
PrivateKey privateKey = keyPair.getPrivate();
byte[] encoded = privateKey.getEncoded();
String b64Encoded = Base64.getEncoder().encodeToString(encoded);
然后保存base64编码的字符串并尝试将其作为私钥加载到我的cpp代码中。
我将base64字符串解码为二进制数组,然后使用以下代码尝试加载私钥(encodedString应该是从base64解码后的DER编码数据):
ByteQueue queue;
StringSource ss(encodedString,true);
ss.TransferTo(queue);
queue.MessageEnd();
key.BERDecodePrivateKey(queue, false , queue.MaxRetrievable());
但是,此代码总是崩溃,例外情况为:CryptoPP::BERDecodeErr
。
我相信两个库都使用PKCS#8来编码关键参数。
注意:当我使用crypto ++创建密钥对然后对其进行编码(DER + base64)时,我得到一个3172/3176字符串,而在我的java代码中,我得到一个3168字符串。我不确定这些信息对任何事情都有帮助。
答案 0 :(得分:2)
您似乎需要致电Load
而不是BERDecodePrivateKey
。当你有一个键+信息(如版本和算法ID)时,你可以调用Load
;当你只有一把钥匙时,你打电话给BERDecodePrivateKey
。以下适用于我。
如果 私钥受密码保护,则需要PEM Pack。 PEM Pack是一个社区贡献,但它的维护与正确的库一样。
$ cat rsa_java.java
import java.io.*;
import java.util.*;
import java.security.*;
public class rsa_java {
public static void main (String[] args) throws Exception {
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(4096);
KeyPair keyPair = keyGen.genKeyPair();
PrivateKey privateKey = keyPair.getPrivate();
byte[] encodedPrivateKey = privateKey.getEncoded();
String b64Encoded = Base64.getEncoder().encodeToString(encodedPrivateKey);
try (PrintStream out = new PrintStream(new FileOutputStream("rsa_key.txt")))
{
out.print(b64Encoded);
}
}
和
$ cat rsa_cryptopp.cxx
#include <iostream>
using namespace std;
#include "cryptopp/rsa.h"
#include "cryptopp/files.h"
#include "cryptopp/osrng.h"
#include "cryptopp/base64.h"
using namespace CryptoPP;
int main(int argc, char* argv[])
{
try
{
RSA::PrivateKey key;
FileSource fs("rsa_key.txt", true, new Base64Decoder);
key.Load(fs);
cout << "Loaded RSA key" << endl;
AutoSeededRandomPool prng;
key.Validate(prng, 3);
cout << "Validated RSA key" << endl;
}
catch(const Exception& ex)
{
cout << "Exception: " << ex.what() << endl;
}
return 0;
}
结果是:
$ javac rsa_java.java && java rsa_java
$ g++ -I. rsa_cryptopp.cxx cryptopp/libcryptopp.a -o rsa_cryptopp.exe
$ ./rsa_cryptopp.exe
Loaded RSA key
Validated RSA key
为了完整性,这是Base64编码的关键。它的3168字节:
$ fold -w 80 -s rsa_key.txt
MIIJRAIBADANBgkqhkiG9w0BAQEFAASCCS4wggkqAgEAAoICAQC+Ncoo+FjXzMgjNm7NaKloa6omc0og
lpozL1Y4efyA9F9OCjBk4Tub87XtFBjcJqFv/BRMGF3f21/kfmt5vHcOFf2f8mZDbyYxyYoVXFf8fmnQ
E+82WrhCsY44/ZPPCCQV6Yyo83JMgZydRMt879r6dWlXlTLAuTpSXS5OptgCtlHu0fWsaPDbzQzqvgsL
5DdsN6rsZ76PTAV6DqgNR9JSoWf+It/MFwMAlBV9fTRpBaMK4EcKBp38NEb1zHvPlrs3iIsPm3eEnGl5
Rvu0VAI4tXsyRuRG/zTMvsP3/rpFTgDssCXuMjZbGsf0oSO4adtj1LZl9j6y7Gz1d+5ou0zvs4YAKhAj
NaxA8RJNnfbiofoMnFM+nc3hVvOodo2Yg6uvRJDZOe5llsv59zF5P9cGr6maJC/PR3qynczFpvpl9CAE
GDE+AKkJHjYUP6dsGbe6krpxLQCqsU1QIv89wdEyW4OHHTPaHqvodajBgLlgqysMgv1k2wV5Vyrwn9H9
etNky+pf4PsJykp5ut80zLTPXBVvb3o5PtOaZ/gwQ8ooPkgv6Uw6bveVbt/ZjScVK+Bg+KKn9B4ELbMm
ht6XC/y3LPrwjVEhgZqCF8TsG7KDOIRx2+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