使用Bouncy Castle的C#RSA解密

时间:2022-03-16 15:45:49

I have been given a Base64 Encoded encrypted string, which was encrypted in Java using Bouncy Castle. Example Java snippet below:

我得到了一个Base64编码加密字符串,它使用Bouncy Castle在Java中加密。下面的示例Java代码段:

Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, key.getPublic());
byte[] encryptedText = cipher.doFinal("xxxxx|xxxxx".getBytes("UTF-8"));
String encodedText = new BASE64Encoder().encode(encryptedText);

I need to decrypt the resulting string using Bouncy Castle, but in C# I have been given a code snippet on how to do this in Java, but I can't convert this for C# (reasons is we are building a .net site, and is going to be an iFrame within a Java site. The Java site is going to passing in the RSA Encrypted string to the .NET site). Example Java code to decrypt below:

我需要使用Bouncy Castle解密生成的字符串,但是在C#中,我已经获得了关于如何在Java中执行此操作的代码片段,但我无法将其转换为C#(原因是我们正在构建.net站点,以及将成为Java站点中的iFrame.Java站点将把RSA加密字符串传递给.NET站点。下面解密的示例Java代码:

Cipher cipherDec = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipherDec.init(Cipher.DECRYPT_MODE, key.getPrivate());
byte[] decodedText = new BASE64Decoder().decodeBuffer(encodedText);
byte[] decryptedText = cipherDec.doFinal(decodedText);
String finalValue = new String(decryptedText, "UTF-8");

I have downloaded the examples from http://www.bouncycastle.org/csharp/ but there doesn't seem to be an example of inputting a string value to get encrypted, and it then going though the encrypt/decrypt process.

我从http://www.bouncycastle.org/csharp/下载了示例,但似乎没有输入字符串值来加密的示例,然后它通过加密/解密过程。

I have been given values for modulus, public exponent, private expontent, prime P, prime q, prime exponent p, prime exponent q and crt coefficient.

我已经给出了模数,公共指数,私有指数,素数P,素数q,素数指数p,素数指数q和crt系数的值。

I have seen that I can use the following:

我看到我可以使用以下内容:

IAsymmetricBlockCipher signer = new Pkcs1Encoding(new RsaEngine());
signer.Init(true, pubParameters);

But the signer object doesn't seem to have the same methods as the Java examples above.

但是签名者对象似乎与上面的Java示例没有相同的方法。

Only method I can use is

我只能使用的方法是

ProcessBlock(byte[] inbuf, int inOff, int inLen);

But I can't see how to use this in my context.

但我无法看到如何在我的上下文中使用它。

Any help here would be most appreciated.

这里的任何帮助将非常感激。

3 个解决方案

#1


13  

To Help others, the final code to convert is as follows:

为了帮助他人,转换的最终代码如下:

RsaKeyParameters privParameters = new RsaPrivateCrtKeyParameters(mod, pubExp, privExp, p, q, pExp, qExp, crtCoef);
RsaKeyParameters pubParameters = new RsaKeyParameters(false, mod, pubExp);
IAsymmetricBlockCipher eng = new Pkcs1Encoding(new RsaEngine());
eng.Init(false, privParameters);
byte[] encdata = System.Convert.FromBase64String("{the enc string}");
encdata = eng.ProcessBlock(encdata, 0, encdata.Length);
string result = Encoding.UTF8.GetString(encdata);

mod, pubExp etc etc are all BigInteger values:

mod,pubExp等都是BigInteger值:

static BigInteger mod = new BigInteger("big int value");

The Following using directives are required:

需要以下using指令:

using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Encodings;
using Org.BouncyCastle.Math;

Which can be obtained from the bouncycastle site. http://www.bouncycastle.org/csharp/

哪个可以从bouncycastle网站获得。 http://www.bouncycastle.org/csharp/

#2


2  

Have you tried converting the base 64 string to a byte array and then using the process block method? There may be more to it than that but it's definitely the first step I would take.

您是否尝试将base 64字符串转换为字节数组,然后使用process block方法?可能还有更多,但这绝对是我要采取的第一步。

Here's an example of how to do this: http://msdn.microsoft.com/en-us/library/system.convert.frombase64string.aspx

以下是如何执行此操作的示例:http://msdn.microsoft.com/en-us/library/system.convert.frombase64string.aspx

#3


1  

I'm not sure I understand why you must use Bouncycastle. The following small code snippet shows and RSA encryption/decryption example using only .NET classes:

我不确定为什么你必须使用Bouncycastle。以下小代码段显示和仅使用.NET类的RSA加密/解密示例:

using System;
using System.Text;
using System.Security.Cryptography;

namespace RsaForDotNet
{
    class Program
    {
        static void Main(string[] args)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(512);
            var encrypted_msg = rsa.Encrypt(Encoding.UTF8.GetBytes("Secret Data"), false);
            var encoded_msg = Convert.ToBase64String(encrypted_msg);
            Console.WriteLine(encoded_msg);
            var decoded_msg = Convert.FromBase64String(encoded_msg);
            var decrypted_msg = Encoding.UTF8.GetString(rsa.Decrypt(decoded_msg, false));
            Console.WriteLine(decrypted_msg);
        }
    }
}

#1


13  

To Help others, the final code to convert is as follows:

为了帮助他人,转换的最终代码如下:

RsaKeyParameters privParameters = new RsaPrivateCrtKeyParameters(mod, pubExp, privExp, p, q, pExp, qExp, crtCoef);
RsaKeyParameters pubParameters = new RsaKeyParameters(false, mod, pubExp);
IAsymmetricBlockCipher eng = new Pkcs1Encoding(new RsaEngine());
eng.Init(false, privParameters);
byte[] encdata = System.Convert.FromBase64String("{the enc string}");
encdata = eng.ProcessBlock(encdata, 0, encdata.Length);
string result = Encoding.UTF8.GetString(encdata);

mod, pubExp etc etc are all BigInteger values:

mod,pubExp等都是BigInteger值:

static BigInteger mod = new BigInteger("big int value");

The Following using directives are required:

需要以下using指令:

using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Encodings;
using Org.BouncyCastle.Math;

Which can be obtained from the bouncycastle site. http://www.bouncycastle.org/csharp/

哪个可以从bouncycastle网站获得。 http://www.bouncycastle.org/csharp/

#2


2  

Have you tried converting the base 64 string to a byte array and then using the process block method? There may be more to it than that but it's definitely the first step I would take.

您是否尝试将base 64字符串转换为字节数组,然后使用process block方法?可能还有更多,但这绝对是我要采取的第一步。

Here's an example of how to do this: http://msdn.microsoft.com/en-us/library/system.convert.frombase64string.aspx

以下是如何执行此操作的示例:http://msdn.microsoft.com/en-us/library/system.convert.frombase64string.aspx

#3


1  

I'm not sure I understand why you must use Bouncycastle. The following small code snippet shows and RSA encryption/decryption example using only .NET classes:

我不确定为什么你必须使用Bouncycastle。以下小代码段显示和仅使用.NET类的RSA加密/解密示例:

using System;
using System.Text;
using System.Security.Cryptography;

namespace RsaForDotNet
{
    class Program
    {
        static void Main(string[] args)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(512);
            var encrypted_msg = rsa.Encrypt(Encoding.UTF8.GetBytes("Secret Data"), false);
            var encoded_msg = Convert.ToBase64String(encrypted_msg);
            Console.WriteLine(encoded_msg);
            var decoded_msg = Convert.FromBase64String(encoded_msg);
            var decrypted_msg = Encoding.UTF8.GetString(rsa.Decrypt(decoded_msg, false));
            Console.WriteLine(decrypted_msg);
        }
    }
}