C# BouncyCastle - RSA 加密与公钥/私钥



我需要在C#中加密数据才能将其传递给Java。Java 代码属于第三方,但我已经得到了相关的源代码,所以我决定由于 Java 使用 Bouncy Castle 库,我将使用 C# 端口。

解密工作正常。但是,解密仅在我使用私钥而不是公钥进行加密时才有效。使用公钥时,解密失败并显示 unknown block type

显然,RsaEncryptWithPrivate内部的加密在加密时使用公钥,所以我不明白为什么这两种加密方法在功能上不相同:

using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Encodings;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.OpenSsl;
public class EncryptionClass
{       
    public string RsaEncryptWithPublic(string clearText
        , string publicKey)
    {
        var bytesToEncrypt = Encoding.UTF8.GetBytes(clearText);
        var encryptEngine = new Pkcs1Encoding(new RsaEngine());
        using (var txtreader = new StringReader(publicKey))
        {
            var keyParameter = (AsymmetricKeyParameter)new PemReader(txtreader).ReadObject();
            encryptEngine.Init(true, keyParameter);
        }
        var encrypted = Convert.ToBase64String(encryptEngine.ProcessBlock(bytesToEncrypt, 0, bytesToEncrypt.Length));
        return encrypted;
    }
    public string RsaEncryptWithPrivate(string clearText
        , string privateKey)
    {
        var bytesToEncrypt = Encoding.UTF8.GetBytes(clearText);
        var encryptEngine = new Pkcs1Encoding(new RsaEngine());
        using (var txtreader = new StringReader(privateKey))
        {
            var keyPair = (AsymmetricCipherKeyPair)new PemReader(txtreader).ReadObject();
            encryptEngine.Init(true, keyPair.Public);
        }
        var encrypted= Convert.ToBase64String(encryptEngine.ProcessBlock(bytesToEncrypt, 0, bytesToEncrypt.Length));
        return encrypted;
    }

    // Decryption:
    public string RsaDecrypt(string base64Input
        , string privateKey)
    {
        var bytesToDecrypt = Convert.FromBase64String(base64Input);
        //get a stream from the string
        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;
    }
}
// In my test project   
    [Test()]
    public void EncryptTest()
    {
        // Set up 
        var input = "Perceived determine departure explained no forfeited";
        var enc = new EncryptionClass();
        var publicKey = "-----BEGIN PUBLIC KEY----- // SNIPPED // -----END PUBLIC KEY-----";
        var privateKey = "-----BEGIN PRIVATE KEY----- // SNIPPED // -----END PRIVATE KEY-----";
        // Encrypt it
        var encryptedWithPublic = enc.RsaEncryptWithPublic(input, publicKey); 
        var encryptedWithPrivate = enc.RsaEncryptWithPrivate(input, privateKey);
        // Decrypt
        var outputWithPublic = payUEnc.RsaDecrypt(encryptedWithPrivate, privateKey); 
        // Throws error: "unknown block type"
        var outputWithPrivate = payUEnc.RsaDecrypt(encryptedWithPrivate, _privateKey); 
        // returns the correct decrypted text, "Perceived determine departure explained no forfeited"
        // Assertion
        Assert.AreEqual(outputWithPrivate, input); // This is true
    }

顺便说一下,Java 解密也表现出同样的问题 - 仅使用公钥加密时,它会失败。

我对密码学很陌生,所以我确定我在RsaEncryptWithPublic方法中做了一些非常简单的错误。

编辑:

我还添加了一个单元测试,证明公钥等于从私钥中提取的公钥:

    [Test()]
    public void EncryptCompareTest()
    {
        AsymmetricKeyParameter keyParameterFromPub;
        AsymmetricKeyParameter keyParameterFromPriv;
        AsymmetricCipherKeyPair keyPair;
        using (var txtreader = new StringReader(_publicKey))
        {
            keyParameterFromPub = (AsymmetricKeyParameter)new PemReader(txtreader).ReadObject();
        }
        using (var txtreader = new StringReader(_privateKey))
        {
            keyPair = (AsymmetricCipherKeyPair)new PemReader(txtreader).ReadObject();
            keyParameterFromPriv = keyPair.Public;
        }
        Assert.AreEqual(keyParameterFromPub, keyParameterFromPriv); // returns true;
    } 

OP的代码中存在一些错误。我做了一些更改。这是我运行的内容。

public class TFRSAEncryption
{
    public string RsaEncryptWithPublic(string clearText, string publicKey)
    {
        var bytesToEncrypt = Encoding.UTF8.GetBytes(clearText);
        var encryptEngine = new Pkcs1Encoding(new RsaEngine());
        using (var txtreader = new StringReader(publicKey))
        {
            var keyParameter = (AsymmetricKeyParameter)new PemReader(txtreader).ReadObject();
            encryptEngine.Init(true, keyParameter);
        }
        var encrypted = Convert.ToBase64String(encryptEngine.ProcessBlock(bytesToEncrypt, 0, bytesToEncrypt.Length));
        return encrypted;
    }
    public string RsaEncryptWithPrivate(string clearText, string privateKey)
    {
        var bytesToEncrypt = Encoding.UTF8.GetBytes(clearText);
        var encryptEngine = new Pkcs1Encoding(new RsaEngine());
        using (var txtreader = new StringReader(privateKey))
        {
            var keyPair = (AsymmetricCipherKeyPair)new PemReader(txtreader).ReadObject();
            encryptEngine.Init(true, keyPair.Private);
        }
        var encrypted = Convert.ToBase64String(encryptEngine.ProcessBlock(bytesToEncrypt, 0, bytesToEncrypt.Length));
        return encrypted;
    }

    // Decryption:
    public string RsaDecryptWithPrivate(string base64Input, string privateKey)
    {
        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;
    }
    public string RsaDecryptWithPublic(string base64Input, string publicKey)
    {
        var bytesToDecrypt = Convert.FromBase64String(base64Input);
        var decryptEngine = new Pkcs1Encoding(new RsaEngine());
        using (var txtreader = new StringReader(publicKey))
        {
            var keyParameter = (AsymmetricKeyParameter)new PemReader(txtreader).ReadObject();
            decryptEngine.Init(false, keyParameter);
        }
        var decrypted = Encoding.UTF8.GetString(decryptEngine.ProcessBlock(bytesToDecrypt, 0, bytesToDecrypt.Length));
        return decrypted;
    }
}
class Program
{
    static void Main(string[] args)
    {
        // Set up 
        var input = "Perceived determine departure explained no forfeited";
        var enc = new TFRSAEncryption();
        var publicKey = "-----BEGIN PUBLIC KEY----- // Base64 string omitted // -----END PUBLIC KEY-----";
        var privateKey = "-----BEGIN PRIVATE KEY----- // Base64 string omitted// -----END PRIVATE KEY-----";
        // Encrypt it
        var encryptedWithPublic = enc.RsaEncryptWithPublic(input, publicKey);
        var encryptedWithPrivate = enc.RsaEncryptWithPrivate(input, privateKey);
        // Decrypt
        var output1 = enc.RsaDecryptWithPrivate(encryptedWithPublic, privateKey);
        var output2 = enc.RsaDecryptWithPublic(encryptedWithPrivate, publicKey);
        Console.WriteLine(output1 == output2 && output2 == input);
        Console.Read();
    }
}

我尝试了@Morio的解决方案,但遇到了一些异常。第一个是

-----END PUBLIC KEY not found

我能够通过正确格式化两个键来修复

publicKey = $"-----BEGIN PUBLIC KEY-----n{publicKey}n-----END PUBLIC KEY-----n";

第二个错误是在尝试解密文本时。 Unable to cast object of type 'Org.BouncyCastle.Crypto.Parameters.RsaPrivateCrtKeyParameters' to type 'Org.BouncyCastle.Crypto.AsymmetricCipherKeyPair'.
我通过强制转换为适当的类型来解决。

var keyPair = (RsaPrivateCrtKeyParameters)new PemReader(txtreader).ReadObject();

我使用了不正确的公钥......证明私钥和公钥匹配的测试使用了正确的公钥。

上面的代码可以按原样完美运行,只要您正确处理密钥即可!

最新更新