Exemple #1
0
        /*
         * 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);
        }