3

I'm trying to decrypt using RSACryptoServiceProvider, but I only have the modulus and d pair as a private key and also the exponent.

RsaParameters struct wont make do with these. It rejects me upon decryption with an exception "Bad key".

To my understanding, this pair is enough to decrypt without the entire DQ DP INVERSEQ parts. More over, in an example I found for python with pyCrypto, it has an RSA.construct method that only takes the above parts.

Is it possible with the classes in the .NET framework or another library? I've tried with BountyCastle but had no luck.

Piero Alberto
  • 3,823
  • 6
  • 56
  • 108

3 Answers3

2

With the information you have, you can recover all the information your are missing and then provide the RSACryptServiceProvider with all the parameters it wants. The algorithm you need to get started is here. Look at section 8.2.2(i), "Relation to Factoring". The third paragraph, which starts out "On the other hand", proceeds to outline a simple algorithm which you can use to recover the primes p and q. From these, you can recover the other values easily. You'll need a reasonable BigInteger package.

President James K. Polk
  • 40,516
  • 21
  • 95
  • 125
  • What decade are we living in, that the average C# developer is left to implement arbitrary algorithms defined in academic texts to perform common place cryptographic actions. That this functionality is missing from C# is appalling. – Courtland Caldwell Nov 20 '18 at 01:04
  • https://stackoverflow.com/questions/2921406/calculate-primes-p-and-q-from-private-exponent-d-public-exponent-e-and-the is a bit more helpful in getting a practical solution. – Courtland Caldwell Nov 20 '18 at 01:20
  • 1
    @CourtlandCaldwell: I actually provide Java code to perform the computation in [this answer](https://stackoverflow.com/a/43147199/238704). It shouldn't be too hard to translate to C#. – President James K. Polk Nov 20 '18 at 02:12
1

it's just a little bit math ;)

k = c^d mod N

k is the plaintextmessage
c is the chiffre
d is your private key
N is your modulus


In Java it would be like this:

BigInteger c = ...
BigInteger d = ...
BigInteger n = ...
BigInteger k = c.modPow(d, n);

I hope C# has something which is equal.

Tobias P.
  • 4,537
  • 2
  • 29
  • 36
0

If you can't get the .NET framework to do it, I long ago wrote a program in C++ (which you may be able to convert to C# with some effort) that manually performs RSA cryptographic transformations, and reviewing the source code, it looks like it should operate using a private key (d) without providing p and q. It's at http://sourceforge.net/projects/bmrsa/

BlueMonkMN
  • 25,079
  • 9
  • 80
  • 146