-1

I have a RSA Encrypted file & I want to decrypt it with C# in .NET

I have the foll. parameters of (1024 bit enc) message(cipher text) to decrypt

  • modulus
  • public exponent
  • private exponent
  • prime p
  • prime q
  • prime exponent p
  • prime exponent q
  • CRT coefficient

The cipher text is in HEX format

I know the CRT method to decrypt the message but not clear on how to use it

m1 = (ciphertext ^ dP) Mod P
m2 = (ciphertext ^ dQ) Mod Q
h = (qInv * (m1 - m2)) Mod P
m = m2 + (h * Q)

I tried performing the decryption using foll namespace

System.Security.Cryptography

can someone help me with a sample code to achieve decryption, as this is my first time to deal with Decryption.

Is there any ready API available? in which I just need to pass the parameters & I will receive the desired output.

oberfreak
  • 1,799
  • 13
  • 20
Akshay
  • 1,341
  • 2
  • 9
  • 18

2 Answers2

2

Yes, you may use the System.Security.Cryptography namespace. MSDN Reference For some examples just dig a little deeper in to the MSDN library. For example, there are examples for the RSACryptoServiceProvider here.

Bryan Allred
  • 606
  • 4
  • 5
0

First create an RSAParameters structure and fill it with your own RSA parameters. Next create an RSACryptoServiceProvider instance and call ImportParameters on your previously defined parameters. At that point you should be able to call Decrypt on your data with (or without) using the OAEP padding.

So something like:

RSAParameters p = new RSAParameters ();
p.D = d;
// ... all parameters
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider ();
rsa.ImportParameters (p);
byte[] decrypted = rsa.Decrypt (encrypted, false);
poupou
  • 43,413
  • 6
  • 77
  • 174
  • you mean my code should be as follows `RSAParameters p = new RSAParameters (); p.P = "" p.Q = "" p.dP = "" p.dQ = "" p.qInv = "" p.modulus = "" p.PrivateExponent = "" p.PublicExponent = "" RSACryptoServiceProvider rsa = new RSACryptoServiceProvider (); rsa.ImportParameters (p); byte[] decrypted = rsa.Decrypt (encrypted, false); ` – Akshay Nov 30 '11 at 05:37
  • Yes, you should have all parameters (I don't recall if CRT parameters are required for .NET but they are not for Mono as they can be re-computed) but `byte[]` (not `string`). .NET is also picky about them being 0-left-padded to the *right* size. See other SO questions about this if this is an issue. – poupou Nov 30 '11 at 12:46