Пример #1
0
        private void bt_Decrypt_Click(object sender, RoutedEventArgs e)
        {
            string plaintext    = "";
            string cipheredtext = txt_Ciphered.Text;

            cipheredtext = File.ReadAllText("info.txt");
            long n  = Convert.ToInt64(txt_Keyn.Text);
            long de = Convert.ToInt64(txt_Keyd.Text);
            int  buf;
            int  error = 0;

            Class.RSA rsa = new Class.RSA();
            for (int i = 0; i < cipheredtext.Length; i++)
            {
                try
                {
                    buf = rsa.encode(Char.ConvertToUtf32(cipheredtext[i].ToString(), 0), n, de);
                    if (buf > 55295 && buf < 57343)
                    {
                        plaintext += cipheredtext[i];
                        error++;
                    }
                    else
                    {
                        plaintext += Char.ConvertFromUtf32(buf);
                    }
                }
                catch
                {
                    buf = rsa.encode(Char.ConvertToUtf32(cipheredtext[i], cipheredtext[i + 1]), n, de);
                    if (buf > 55295 && buf < 57343)
                    {
                        plaintext += cipheredtext[i] + cipheredtext[i + 1];
                        error++;
                        error++;
                    }
                    else
                    {
                        plaintext += Char.ConvertFromUtf32(buf);
                    }
                    i++;
                }
            }
            if (error > cipheredtext.Length * 0.1)
            {
                MessageBox.Show("E:" + error.ToString() + "Proszę zmienić klucz");
            }
            txt_Plain.Text = plaintext;
            File.WriteAllText("info.txt", cipheredtext);
        }
Пример #2
0
        private void bt_encrypt_Click(object sender, RoutedEventArgs e)
        {
            string plaintext    = txt_Plain.Text;
            string cipheredtext = "";
            long   n            = Convert.ToInt64(txt_Keyn.Text);
            long   en           = Convert.ToInt64(txt_Keye.Text);
            int    buf;
            int    error = 0;

            Class.RSA rsa = new Class.RSA();
            //string hexValue =
            for (int i = 0; i < plaintext.Length; i++)
            {
                try
                {
                    buf = rsa.encode(Char.ConvertToUtf32(plaintext[i].ToString(), 0), n, en);
                    if (buf > 55295 && buf < 57343)
                    {
                        cipheredtext += plaintext[i];
                        error++;
                    }
                    else
                    {
                        cipheredtext += Char.ConvertFromUtf32(buf);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                    buf = rsa.encode(Char.ConvertToUtf32(plaintext[i], plaintext[i + 1]), n, en);
                    if (buf > 55295 && buf < 57343)
                    {
                        cipheredtext += plaintext[i] + plaintext[i + 1];
                        error++;
                        error++;
                    }
                    else
                    {
                        cipheredtext += Char.ConvertFromUtf32(buf);
                    }
                }
            }
            if (error > plaintext.Length * 0.1)
            {
                MessageBox.Show("E:" + error.ToString() + "Proszę zmienić klucz");
            }
            txt_Ciphered.Text = cipheredtext;
            this.ciphered     = cipheredtext;
        }