Mapping RSA encryption settings from CRT (Chinese remainder theorem) in .NET format

I need to implement RSA encryption / decryption using C #

I have a private key with the following parameters:

mod n , exponent , p , q , dP , dQ and (p -1 mod q)

The above parameters are explained in the Chinese stop algorithm

However, the C # .NET implementation in RSA has the following set of parameters:

Modulus , exponent , p , q , dP , dQ , D , InverseQ

When I try to display data from CRT to DOTNET , I get a Bad Data error

For p , q , dP and dQ mapping is obvious, but I'm not sure about the rest of the parameters.

It would be great if I could get help comparing these parameters

+6
source share
3 answers

mod n maps to Modulus , p -1 mod q maps to InverseQ , the encryption metric is mapped to Exponent , and the decryption metric is mapped to D

The encryption index e and decryption index D are associated with e * d = 1 mod (p-1) (q-1). That way, if you have them, you can easily get another use of several methods from the System.Numerics.BigInteger class.

 var Pminus1 = BigInteger.Subtract(P, BigInteger.One); var Qminus1 = BigInteger.Subtract(Q, BigInteger.One); var Phi = BigInteger.Multiply(Pminus1, Qminus1); var PhiMinus1 = BigInteger.Subtract(Phi, BigInteger.One); // var D = BigInteger.ModPow(E, PhiMinus1, Phi); 

Note that you must be careful when creating the .NET BigInteger, especially if you are used to the Java BigInteger class. See this question for more information.

EDIT:

Because CodeInChaos indicates that the last line is WRONG!

WRONG! WRONG! WRONG!

I am embarrassed. In bowing to the forces of evil, the BigInteger class does not have a modular inverse method or an extended Euclidean algorithm method. However, you can use google for the 'C # advanced Euclidean algorithm, you can find many implementations. The extended Euclidean algorithm will give you integers x and y such that 1 = e * x + phi * y. x is the inverse of e mod phi, so you need to set D = x mod phi.

+5
source

Euclidean advanced algorithm can be used to calculate the modular inverse, in which case D will be calculated, use this link: http://www.di-mgt.com.au/euclidean.html#extendedeuclidean to get the details, I tested the source code on C # as shown below, and the result is consistent,

 public static BigInteger modinv(BigInteger u, BigInteger v) { BigInteger inv, u1, u3, v1, v3, t1, t3, q; BigInteger iter; /* Step X1. Initialise */ u1 = 1; u3 = u; v1 = 0; v3 = v; /* Remember odd/even iterations */ iter = 1; /* Step X2. Loop while v3 != 0 */ while (v3 != 0) { /* Step X3. Divide and "Subtract" */ q = u3 / v3; t3 = u3 % v3; t1 = u1 + q * v1; /* Swap */ u1 = v1; v1 = t1; u3 = v3; v3 = t3; iter = -iter; } /* Make sure u3 = gcd(u,v) == 1 */ if (u3 != 1) return 0; /* Error: No inverse exists */ /* Ensure a positive result */ if (iter < 0) inv = v - u1; else inv = u1; return inv; } 
+1
source

D can be calculated as follows:

  var qq = BigInteger.Multiply(phi, n); var qw = BigInteger.Multiply(phi, qq); BigInteger D = BigInteger.ModPow(e, (qw - 1), phi); 
0
source

All Articles