public RSAParameters ToRsaParamaters() { return(new RSAParameters { Modulus = Modulus.Reverse().ToArray(), P = P.Reverse().ToArray(), D = D.Reverse().ToArray(), Q = Q.Reverse().ToArray(), InverseQ = Iq.Reverse().ToArray(), Exponent = BitConverter.GetBytes(PubExp).Reverse().ToArray(), DQ = Dq.Reverse().ToArray(), DP = Dp.Reverse().ToArray() }); }
public override string ToString() { StringBuilder builder = new StringBuilder(); builder.Clear(); builder.Append("<RSAKeyValue>"); builder.Append("<Modulus>"); builder.Append(Modulus.Trim()); builder.Append("</Modulus>"); builder.Append("<Exponent>"); builder.Append(Exponent.Trim()); builder.Append("</Exponent>"); builder.Append("<P>"); builder.Append(P.Trim()); builder.Append("</P>"); builder.Append("<Q>"); builder.Append(Q.Trim()); builder.Append("</Q>"); builder.Append("<DP>"); builder.Append(Dp.Trim()); builder.Append("</DP>"); builder.Append("<DQ>"); builder.Append(Dq.Trim()); builder.Append("</DQ>"); builder.Append("<InverseQ>"); builder.Append(InverseQ.Trim()); builder.Append("</InverseQ>"); builder.Append("<D>"); builder.Append(D.Trim()); builder.Append("</D>"); builder.Append("</RSAKeyValue>"); return(builder.ToString()); }
/* * public static BigInteger Pow(BigInteger value, BigInteger exponent) * { * BigInteger originalValue = value; * while (exponent-- > 1) * value = BigInteger.Multiply(value, originalValue); * return value; * }*/ public static MyRSAProvider.MyMyKeys GenerateKey(int Keysize) { //initialize constructor MyRSAProvider.MyMyKeys MyKeys = new MyRSAProvider.MyMyKeys(); //Get prime P P = GetPrime(Keysize); //Get prime Q Q = GetPrime(Keysize); //calculate the modulus N = GetN(P, Q); //calculate the totient or phi To = GetTotient(P, Q); // Get the value of the public exponent...(65537) E = GetE(); //compute the value of the private exponent such that (D*E % To) = 1 D = GetD(E, To); //calculate other properties of the private and public keys to be used in chinese remender theorem Dp = D % (P - 1); Dq = D % (Q - 1); Ep = E % (P - 1); Eq = E % (Q - 1); Qinv = GetD(Q, P); //get the keysize into our constructor MyKeys.KeySize = Keysize; //do checks to see that everything is in other else calculate again if (P == 0 || Q == 0 || N == 0 || To == 0 || D == 0 || P == Q || (D * E % To) != 1) { GenerateKey(Keysize); } //Add the private key values to the constructor MyKeys.PrivateKey.Add(new MyRSAProvider.PrivateKey { D = D.ToString(), N = N.ToString(), Dp = Dp.ToString(), Dq = Dq.ToString(), Qinv = Qinv.ToString(), P = P.ToString(), Q = Q.ToString() }); //Add the public key values to the constructor MyKeys.PublicKey.Add(new MyRSAProvider.PublicKey { E = E.ToString(), N = N.ToString(), Ep = Ep.ToString(), Eq = Eq.ToString(), Qinv = Qinv.ToString(), P = P.ToString(), Q = Q.ToString() }); return(MyKeys); }