我在使用Bouncycastle解密字符串时遇到困难。我怀疑它的编码,但找不到充气弹药的任何PKCS8编码
var bytesToDecrypt = Convert.FromBase64String(base64Input);
AsymmetricCipherKeyPair keyPair;
var decryptEngine = new Pkcs1Encoding(new RsaEngine());
using (var txtreader = new StringReader(privateKey))
{
keyPair = (AsymmetricCipherKeyPair)new PemReader(txtreader).ReadObject();
decryptEngine.Init(false, keyPair.Private);
}
var decrypted = Encoding.UTF8.GetString(decryptEngine.ProcessBlock(bytesToDecrypt, 0, bytesToDecrypt.Length));
return decrypted;
这是PKCS8格式的RSA密钥:
-----BEGIN PRIVATE KEY-----
...
-----END PRIVATE KEY-----
答案 0 :(得分:1)
使用AsymmetricKeyParameter代替AsymmetricCipherKeyPair,如下所示:
AsymmetricKeyParameter akp;
var decryptEngine = new Pkcs1Encoding(new RsaEngine());
using (var txtreader = new StringReader(privateKey))
{
akp = (AsymmetricKeyParameter)new PemReader(txtreader).ReadObject();
decryptEngine.Init(false, akp);
}