Esempio n. 1
0
        private void Login_Click(object sender, RoutedEventArgs e)
        {
            RSA2017TestDBContext context = new RSA2017TestDBContext();
            var Verify = context.RSA2017TestDB.Where(m => m.Username == Username.Text).FirstOrDefault();

            if (Verify != null)
            {
                if (Verify.PassWord == Password.Password)
                {
                    this.NavigationService.Navigate(new RSAIndex(Verify.UserId));
                    // Uri uri = new Uri("RSAIndex.xaml", UriKind.Relative);
                    //this.NavigationService.Navigate(uri);
                }
                else
                {
                    ErrorMsg.Content    = "Password incorrect";
                    ErrorMsg.Visibility = Visibility.Visible;
                }
            }
            else
            {
                ErrorMsg.Content    = "Username incorrect";
                ErrorMsg.Visibility = Visibility.Visible;
            }
        }
        public PlaintextDecryption(string id)
        {
            InitializeComponent();
            RSA2017TestDBContext context = new RSA2017TestDBContext();

            foreach (var item in context.RSA2017TestDB)
            {
                SelectUser.Items.Add(item.Username);
            }
            GottenId = id;
        }
        private void Encrptbtn_Click(object sender, RoutedEventArgs e)
        {
            RSA2017TestDBContext context = new RSA2017TestDBContext();
            Stopwatch            timer   = new Stopwatch();

            Timerlbl.Content = timer.ElapsedMilliseconds.ToString();
            var currentUser = context.RSA2017TestDB.Where(m => m.Username == SelectUser.Text).FirstOrDefault();

            try
            {
                BigInteger Dp            = BigInteger.Parse(currentUser.MyKeys.Dp);
                BigInteger Dq            = BigInteger.Parse(currentUser.MyKeys.Dq);
                BigInteger Qinv          = BigInteger.Parse(currentUser.MyKeys.Qinv);
                BigInteger P             = BigInteger.Parse(currentUser.MyKeys.P);
                BigInteger Q             = BigInteger.Parse(currentUser.MyKeys.Q);
                BigInteger D             = BigInteger.Parse(currentUser.MyKeys.D);
                BigInteger n             = BigInteger.Parse(currentUser.MyKeys.N);
                int        Size          = currentUser.MyKeys.KeySize;
                string[]   Input         = PlainTxtBlock.Text.Split('$');
                string[]   output        = new string[Input.Length];
                byte[]     dataToEncrypt = new byte[Size];
                timer.Reset();
                for (int i = 0; i <= (Input.Length - 1); i++)
                {
                    byte[] data      = new byte[Size];
                    byte[] use       = new byte[Size];
                    string Decrypted = string.Empty;
                    if (Input[i] == string.Empty)
                    {
                        Input[i] = Input[i] + " ";
                    }
                    timer.Start();
                    data             = Convert.FromBase64String(Input[i]);
                    use              = RSAMain.Decrypt(data, Dp, Dq, Qinv, P, Q, Size, D, n);
                    Decrypted        = Encoding.UTF8.GetString(use);
                    output[i]        = Decrypted;
                    Timerlbl.Content = timer.ElapsedMilliseconds.ToString();
                }
                EncryptedTxtBlock.Text = string.Join(" ", output);
                timer.Stop();
                Timerlbl.Content  = timer.ElapsedMilliseconds.ToString();
                Savebtn.IsEnabled = true;
            }
            catch (Exception)
            {
                MessageBox.Show("Decryption Failed!");
            }
        }
Esempio n. 4
0
        private void Encrptbtn_Click(object sender, RoutedEventArgs e)
        {
            Stopwatch timer = new Stopwatch();

            Timerlbl.Content = timer.ElapsedMilliseconds.ToString();
            RSA2017TestDBContext context = new RSA2017TestDBContext();

            var currentUser = context.RSA2017TestDB.Where(m => m.Username == SelectUser.Text).FirstOrDefault();

            try
            {
                BigInteger Ep            = BigInteger.Parse(currentUser.MyKeys.Ep);
                BigInteger Eq            = BigInteger.Parse(currentUser.MyKeys.Eq);
                BigInteger Qinv          = BigInteger.Parse(currentUser.MyKeys.Qinv);
                BigInteger P             = BigInteger.Parse(currentUser.MyKeys.P);
                BigInteger Q             = BigInteger.Parse(currentUser.MyKeys.Q);
                BigInteger E             = BigInteger.Parse(currentUser.MyKeys.E);
                BigInteger N             = BigInteger.Parse(currentUser.MyKeys.N);
                int        Size          = currentUser.MyKeys.KeySize;
                string[]   Input         = PlainTxtBlock.Text.Split(' ');
                string[]   output        = new string[Input.Length];
                byte[]     dataToEncrypt = new byte[Size];
                timer.Reset();
                for (int i = 0; i <= (Input.Length - 1); i++)
                {
                    string Encrypted = string.Empty;
                    if (Input[i] == string.Empty)
                    {
                        Input[i] = Input[i] + " ";
                    }
                    timer.Start();
                    byte[] data = Encoding.UTF8.GetBytes(Input[i]);
                    byte[] use  = RSAMain.Encrypt(data, Ep, Eq, Qinv, P, Q, Size, E, N);

                    Encrypted = Convert.ToBase64String(use);
                    output[i] = Encrypted;
                }
                EncryptedTxtBlock.Text = string.Join("$", output);
                Timerlbl.Content       = timer.ElapsedMilliseconds.ToString();
                timer.Stop();
                Timerlbl.Content  = timer.ElapsedMilliseconds.ToString();
                Savebtn.IsEnabled = true;
            }
            catch (Exception)
            {
                MessageBox.Show("Select a receiver");
            }
        }
Esempio n. 5
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;
            }
        }
        private void Encrptbtn_Click(object sender, RoutedEventArgs e)
        {
            RSA2017TestDBContext context        = new RSA2017TestDBContext();
            BitmapImage          EncryptedImage = new BitmapImage();

            var currentUser = context.RSA2017TestDB.Where(m => m.Username == SelectUser.Text).FirstOrDefault();
            int l           = 0;

            try
            {
                BigInteger Ep   = BigInteger.Parse(currentUser.MyKeys.Ep);
                BigInteger Eq   = BigInteger.Parse(currentUser.MyKeys.Eq);
                BigInteger Qinv = BigInteger.Parse(currentUser.MyKeys.Qinv);
                BigInteger P    = BigInteger.Parse(currentUser.MyKeys.P);
                BigInteger Q    = BigInteger.Parse(currentUser.MyKeys.Q);
                BigInteger E    = BigInteger.Parse(currentUser.MyKeys.E);
                BigInteger N    = BigInteger.Parse(currentUser.MyKeys.N);
                int        Size = currentUser.MyKeys.KeySize;
                byte[]     EncryptedBytes;
                byte[]     NewImg    = new byte[(34 * Size) * (Imgbytes.Length / 100)];
                byte[]     Encrypted = new byte[Imgbytes.Length];
                int        count     = 0;
                for (int i = 0; i < Imgbytes.Length;)
                {
                    byte[] bitsOfBytes = new byte[100];
                    for (int j = 0; j < 100; j++)
                    {
                        l = i + j;
                        if (l == (Imgbytes.Length - 1))
                        {
                            break;
                        }
                        bitsOfBytes[j] = Imgbytes[l];
                    }
                    try
                    {
                        EncryptedBytes = RSAMain.Encrypt(bitsOfBytes, Ep, Eq, Qinv, P, Q, Size, E, N);

                        //Image encryptedImg = BitmapImage.Create(Imgppt.)
                        for (int m = 0; m < EncryptedBytes.Length; m++)
                        {
                            NewImg[m + count] = EncryptedBytes[m];
                            Encrypted[m]      = EncryptedBytes[m];
                        }
                        count += EncryptedBytes.Length;
                        try
                        {
                            BitmapSource bitmap = BitmapSource.Create(2, 2, imageSource.Width, imageSource.Height, PixelFormats.Indexed8, BitmapPalettes.WebPalette, Encrypted, 2);

                            EncryptedImg.Source = bitmap;
                            // EncryptedImage = LoadImage(Encrypted);
                            //EncryptedImg.Source = EncryptedImage;
                        }
                        catch (Exception exp)
                        {
                            MessageBox.Show("Error in converting image\n" + exp.ToString());
                        }
                    }
                    catch (Exception imgexp)
                    {
                        MessageBox.Show("Encryption Failed!\nTry again with reduced file size" + imgexp);
                    }
                    if (l == (Imgbytes.Length - 1))
                    {
                        break;
                    }
                    i = i + 100;
                }
                BitmapSource bitmapSource = BitmapSource.Create(2, 2, imageSource.Width, imageSource.Height, PixelFormats.Indexed8, BitmapPalettes.Gray256, NewImg, 2);

                EncryptedImg.Source = bitmapSource;
            }
            catch (Exception)
            {
                MessageBox.Show("Select a receiver");
            }
        }