简体   繁体   English

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

[英]RSA Decryption using BouncyCastle with private PEM file not working

I'm doing some tests with BouncyCastle in C# and I want to encrypt some data and decrypt it later with a pair of keys that I have in my computer stored as PEM files. 我正在使用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);
    }

The RSADecrypt function shows an error. RSADecrypt函数显示错误。 when I show the message box after decrypting I get this: 当我在解密后显示消息框时,得到以下信息:

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

which is definitely not what I encrypted. 绝对不是我加密的。 What am I doing wrong? 我究竟做错了什么?

Actually the answer why it is not working is that there is no information about padding. 实际上,为什么它不起作用的答案是没有关于填充的信息。 Correct way how to instantiate RsaEngine is sth. 正确的实例化RsaEngine的方法是……。 like this 像这样

var decryptEngine = new Pkcs1Encoding (RsaEngine()) var解密引擎=新的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);

I reproduced this problem and it happened because you used a private key and a public key that don't match. 我重现了此问题,它的发生是因为您使用了不匹配的私钥和公钥。 In other words the message was encrypted with a private key (let's call it private_key_1) that came from one pair (private_key_1/public_key_1) but you tried to decrypt it with a public key (let's call it publick_key_2) that came from a different pair (private_key_2/public_key_2). 换句话说,邮件是用一对(private_key_1 / public_key_1)的私钥(我们称其为private_key_1)加密的,但是您尝试使用来自另一对的私钥(我们称其为publick_key_2)对其进行解密( private_key_2 / public_key_2)。 Try to generate a new key pair and use it in your example eg: 尝试生成一个新的密钥对,并在示例中使用它,例如:

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM