C # RSA extension using Bouncy Castle

I was provided with a Base64 encoded encrypted string that was encrypted in Java using Bouncy Castle. An example of a 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); 

I need to decrypt the resulting string using Bouncy Castle, but in C # I was provided with a piece of code on how to do this in Java, but I can’t convert it for C # (the reason is that we are building a .net site and we will be an iFrame on a Java site. going to send an encrypted string to the RSA to the .NET site). Sample Java code for decryption 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"); 

I downloaded examples from http://www.bouncycastle.org/csharp/ , but there seems to be no example of entering a string value for encryption, and then it goes through the encryption / decryption process.

I am assigned values ​​for the module , public exponent , private indicator , prime P , prime q , prime index p , prime index q and coefficient crt .

I saw that I can use the following:

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

But the signer object does not seem to have the same methods as the Java examples above.

Only method that I can use

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

But I do not see how to use this in my context.

Any help here would be most appreciated.

+8
java c # cryptography rsa bouncycastle
source share
3 answers

To help others, the final code for the conversion 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. - all values ​​of BigInteger:

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

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; 

What can be obtained from the bouncycastle website. http://www.bouncycastle.org/csharp/

+13
source share

Have you tried converting the base 64 string to an array of bytes and then using the process block method? There may be more than that, but this is definitely the first step I would take.

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

+2
source share

I'm not sure I understand why you should use Bouncycastle. Below is a small code snippet and an example of RSA encryption / decryption using only .NET classes:

 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
source share