C# RSA Decryption using Bouncy Castle(使用 Bouncy Castle 进行 C# RSA 解密)
问题描述
我得到了一个 Base64 编码的加密字符串,它是在 Java 中使用 Bouncy Castle 加密的.下面的示例 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);
我需要使用 Bouncy Castle 解密生成的字符串,但在 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.
我得到了 模数、公共指数、私人指数、素数P、的值>prime q、prime exponent p、prime exponent q和crt coefficient.
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);
但是 signer
对象似乎没有与上面的 Java 示例相同的方法.
But the signer
object doesn't seem to have the same methods as the Java examples above.
我唯一能用的方法是
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);
mod、pubExp 等都是 BigInteger 值:
mod, pubExp etc etc are all BigInteger values:
static BigInteger mod = new BigInteger("big int value");
以下 using
指令是必需的:
The Following using
directives are required:
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/
Which can be obtained from the bouncycastle site. http://www.bouncycastle.org/csharp/
这篇关于使用 Bouncy Castle 进行 C# RSA 解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 Bouncy Castle 进行 C# RSA 解密
- Jersey REST 客户端:发布多部分数据 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01