Esempio n. 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);
        }
Esempio n. 2
0
        private async void Sign_Up_Click(object sender, RoutedEventArgs e)
        {
            Stopwatch keyGenTime = new Stopwatch();

            if (!string.IsNullOrWhiteSpace(Name.Text) && !string.IsNullOrWhiteSpace(Username.Text) && !string.IsNullOrWhiteSpace(Password.Password) && !string.IsNullOrWhiteSpace(ConfirmPassword.Password))
            {
                if (string.Equals(Password.Password, ConfirmPassword.Password))
                {
                    int SelectedKeySize        = int.Parse(KeySizeBox.Text);
                    MyRSAProvider.MyMyKeys rsa = new MyRSAProvider.MyMyKeys();
                    keyGenTime.Reset();
                    keyGenTime.Start();
                    rsa = RSAMain.GenerateKey(SelectedKeySize);
                    keyGenTime.Stop();
                    KeyGenTime.Content = keyGenTime.ElapsedMilliseconds.ToString();
                    var    newPublicKey  = rsa.PublicKey.FirstOrDefault();
                    var    newPrivateKey = rsa.PrivateKey.FirstOrDefault();
                    string publicKey     = newPublicKey.E;
                    string privateKey    = newPrivateKey.D;
                    string modulus       = newPrivateKey.N;
                    string Dp            = newPrivateKey.Dp;
                    string Dq            = newPrivateKey.Dq;
                    string Qinv          = newPrivateKey.Qinv;
                    string P             = newPrivateKey.P;
                    string Q             = newPrivateKey.Q;
                    string Ep            = newPrivateKey.Dp;
                    string Eq            = newPrivateKey.Dq;

                    string Id = DateTime.Now.ToString();
                    RSA2017TestDBContext context = new RSA2017TestDBContext();
                    var CheckUsername            = context.RSA2017TestDB.Where(m => m.Username == Username.Text).FirstOrDefault();
                    MessageBox.Show("User has been created and keys have been generated for the user.\nClick OK to be redirected to landing page.");

                    if (CheckUsername == null)
                    {
                        var UserKey = context.MyKeys.Add(new MyKeys {
                            KeyId = Id, D = privateKey, E = publicKey, KeySize = SelectedKeySize, N = modulus, Dp = Dp, Dq = Dq, Ep = Ep, Eq = Eq, P = P, Q = Q, Qinv = Qinv
                        });
                        var NewUser = context.RSA2017TestDB.Add(new RSA2017TestDB {
                            KeyId = Id, Name = Name.Text, PassWord = Password.Password, UserId = Id, Username = Username.Text, MyKeys = UserKey, Email = EmailTxt.Text
                        });
                        await context.SaveChangesAsync();

                        this.NavigationService.Navigate(new RSAIndex(NewUser.UserId));
                    }
                    else
                    {
                        ErrorMsg.Content    = "Username already exist.";
                        ErrorMsg.Visibility = Visibility.Visible;
                    }
                }
                else
                {
                    ErrorMsg.Content    = "Password and Confirm Password doesn't match.";
                    ErrorMsg.Visibility = Visibility.Visible;
                }
            }
            else
            {
                ErrorMsg.Content    = "Enter valid details and try again.";
                ErrorMsg.Visibility = Visibility.Visible;
            }
        }