使用BouncyCastle与私有PEM文件无法正常工作的RSA解密

时间:2015-01-15 00:10:00

标签: c# .net cryptography rsa

我正在使用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

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

2 个答案:

答案 0 :(得分:3)

实际上,为什么它不起作用的答案是没有关于填充的信息。正确的方法如何实例化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);

答案 1 :(得分:2)

我重现了这个问题,因为您使用的私钥和公钥不匹配。换句话说,消息是使用来自一对(private_key_1 / public_key_1)的私钥(我们称之为private_key_1)加密的,但是您尝试使用来自不同对的公钥(让我们称之为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);
}