使用带有私有 PEM 文件的 BouncyCastle 的 RSA 解密不起作用

RSA Decryption using BouncyCastle with private PEM file not working

我正在使用 C# 中的 BouncyCastle 进行一些测试,我想加密一些数据并稍后使用我在计算机中存储为 PEM 文件的一对密钥对其进行解密。

  public static string RSABouncyEncrypt(string content)
    {
        var bytesToEncrypt = Encoding.UTF8.GetBytes(content);
        AsymmetricKeyParameter keyPair;
        using (var reader = File.OpenText(@"C:\Users\Diego\Documents\public.pem"))) 
            keyPair = (AsymmetricKeyParameter)new org.BouncyCastle.OpenSsl.PemReader(reader).ReadObject();



        var engine = new RsaEngine();
        engine.Init(true, keyPair);

        var encrypted = engine.ProcessBlock(bytesToEncrypt, 0, bytesToEncrypt.Length);

        var cryptMessage = Convert.ToBase64String(encrypted);
        Logs.Log.LogMessage("encrypted: " + cryptMessage);
        System.Windows.MessageBox.Show(cryptMessage);

        //Decrypt before return statement to check that it has been encrypted correctly
        RSADecrypt(cryptMessage);
        return cryptMessage;
    }

public static void RSADecrypt(string string64)
    {
        var bytesToDecrypt = Convert.FromBase64String(string64); // string to decrypt, base64 encoded

        AsymmetricCipherKeyPair keyPair;

        using (var reader = File.OpenText(@"C:\Users\Diego\Documents\private.pem"))
            keyPair = (AsymmetricCipherKeyPair)new Org.BouncyCastle.OpenSsl.PemReader(reader).ReadObject();

        var decryptEngine = new RsaEngine();
        decryptEngine.Init(false, keyPair.Private);

        var decrypted = Encoding.UTF8.GetString(decryptEngine.ProcessBlock(bytesToDecrypt, 0, bytesToDecrypt.Length));
        Logs.Log.LogMessage("decrypted: " + decrypted);
        System.Windows.MessageBox.Show(decrypted);
    }

RSADecrypt函数显示错误。当我在解密后显示消息框时,我得到这个:

����Z��8o>>����;;��/��Z��ב?����#F��(͌5����o1I�,����4� S�W ��)��w��x��4p��$-|А����&��Rv}��G��V��c ��&wU? ��D�� }E����O����7��n��!(e��E��$y��g9ςOأ��P�� ��t��d��T��NN. ��K$��bQ��!��v����-��Hb����1����?����@B��y� r��Le�h=*Yr�w �l�W|�嘟��|g��EV��@��[�� M

这绝对不是我加密的。我做错了什么?

我重现了这个问题,它的发生是因为您使用了不匹配的私钥和 public 密钥。换句话说,该消息是使用来自一对 (private_key_1/public_key_1) 的私钥(我们称之为 private_key_1)加密的,但您尝试使用 public 密钥对其进行解密(我们称之为它 publick_key_2) 来自另一对 (private_key_2/public_key_2)。尝试生成一个新的密钥对并在您的示例中使用它,例如:

var kpgen = new RsaKeyPairGenerator();
kpgen.Init(new KeyGenerationParameters(new SecureRandom(new CryptoApiRandomGenerator()), 1024));

var keyPair = kpgen.GenerateKeyPair();

using (var writer = new StreamWriter(File.OpenWrite(@"C:\Users\Diego\Documents\private2.pem")))
{
    new PemWriter(writer).WriteObject(keyPair.Private);
}

using (var writer = new StreamWriter(File.OpenWrite(@"C:\Users\Diego\Documents\public2.pem")))
{
    new PemWriter(writer).WriteObject(keyPair.Public);
}

实际上它不起作用的原因是没有关于填充的信息。如何实例化 RsaEngine 的正确方法是……。像这样

var decryptEngine = new Pkcs1Encoding(RsaEngine())

    var bytesToDecrypt = Convert.FromBase64String(string64); // string to decrypt, base64 encoded

    AsymmetricCipherKeyPair keyPair;

    using (var reader = File.OpenText(@"C:\Users\Diego\Documents\private.pem"))
        keyPair = (AsymmetricCipherKeyPair)new Org.BouncyCastle.OpenSsl.PemReader(reader).ReadObject();

    var decryptEngine = new Pkcs1Encoding(RsaEngine());
    decryptEngine.Init(false, keyPair.Private);

    var decrypted = Encoding.UTF8.GetString(decryptEngine.ProcessBlock(bytesToDecrypt, 0, bytesToDecrypt.Length));
    Logs.Log.LogMessage("decrypted: " + decrypted);
    System.Windows.MessageBox.Show(decrypted);