使用充气城堡C#RSA解密 [英] C# RSA Decryption using Bouncy Castle

查看:232
本文介绍了使用充气城堡C#RSA解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在考虑Base64编码加密的字符串,其使用充气城堡在Java中加密。例如Java片断如下:

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

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



我需要解密使用充气城堡生成的字符串,但在C#
我一直就如何做到这一点在Java代码片断,但我不能转换为这个C#(原因是我们正在建设一个.NET网站,将是一个Java站点内的iFrame。在Java网站将通过在RSA加密字符串到.NET网站)。例如Java代码来解密如下:

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:

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



我已经从网上下载的 http://www.bouncycastle.org/csharp/ 但似乎没有要输入一个字符串值的例子得到加密,然后去虽然加密/解密过程。

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.

我已经给出了值的系数 公开指数 私人expontent 素数p 数q 黄金指数p 黄金指数q CRT系数

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

我已经看到,我可以使用以下命令:

I have seen that I can use the following:

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



签名对象似乎并不有相同的方法,如上述的Java示例

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

只有我可以使用的方法是

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.

推荐答案

要帮助别人,最终代码转换如下:

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



模,pubExp等等等等都是BigInteger的值:

mod, pubExp etc etc are all BigInteger values:

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



以下使用指令是必需的:

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



可以从BouncyCastle的站点获得。 http://www.bouncycastle.org/csharp/

这篇关于使用充气城堡C#RSA解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆