Beispiel #1
0
 private void button_Decrypt_Click(object sender, EventArgs e)
 {
     ButtonState(false);
     try
     {
         RSAx rsax = new RSAx(richTextBox_Key.Text, int.Parse(comboBox_ModulusSize.Text));
         rsax.RSAxHashAlgorithm = ha_types[comboBox_OAEP_Hash.SelectedIndex];
         byte[] PT = rsax.Decrypt(Convert.FromBase64String(richTextBox_CipherText.Text), checkBox_PrivateKey.Checked, checkBox_OAEP.Checked);
         richTextBox_PlainText.Text = Encoding.UTF8.GetString(PT);
     }
     catch (System.Exception ex)
     {
         MessageBox.Show("Exception while Decryption: " + ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
     }
     ButtonState(true);
 }
Beispiel #2
0
        private void Decrypt()
        {
            try
            {
                RSAx rsax = RSAx.CreateFromXML(Key, ModSize);
                rsax.RSAxHashAlgorithm = HashAlgorithm;

                byte[] raw = Convert.FromBase64String(TextCrypt.Replace("\r", "").Replace("\n", ""));

                byte[] pt = rsax.Decrypt(raw, InvertPPK, UseOAEP);
                TextPlain = Encoding.UTF8.GetString(pt);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Exception while Decryption: " + ex.Message);
            }
        }
Beispiel #3
0
        private void button_Test_Click(object sender, EventArgs e)
        {
            richTextBox_Test.Clear();

            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(int.Parse(comboBox_ModulusSize.Text));

            rsa.FromXmlString(richTextBox_Key.Text);

            RSAxParameters param = RSAxUtils.GetRSAxParameters(richTextBox_Key.Text, int.Parse(comboBox_ModulusSize.Text));
            RSAx           rsax  = new RSAx(param);

            rsax.UseCRTForPublicDecryption = checkBox_UseCRT.Checked;
            rsax.RSAxHashAlgorithm         = ha_types[comboBox_OAEP_Hash.SelectedIndex];

            int hLen      = hLens[comboBox_OAEP_Hash.SelectedIndex];
            int maxLength = 0;
            int mod_sz    = int.Parse(comboBox_ModulusSize.Text);

            if (checkBox_OAEP.Checked)
            {
                maxLength = (mod_sz / 8) - 2 * hLen - 2;
            }
            else
            {
                maxLength = (mod_sz / 8) - 11;
            }

            if (maxLength >= 0)
            {
                Random    r  = new Random();
                Stopwatch sw = new Stopwatch();
                sw.Start();
                int  iterations = int.Parse(comboBox_Test_Iterations.Text);
                bool error      = false;
                for (int i = 0; i < iterations; i++)
                {
                    byte[] da = new byte[maxLength]; // 86 for modulus of 1024.
                    r.NextBytes(da);
                    try
                    {
                        byte[] CTX = rsax.Encrypt(da, checkBox_OAEP.Checked);
                        byte[] PTX = rsax.Decrypt(CTX, checkBox_OAEP.Checked);
                        bool   ok  = true;
                        for (int j = 0; j < da.Length; j++)
                        {
                            if (da[j] != PTX[j])
                            {
                                ok = false;
                                break;
                            }
                        }

                        richTextBox_Test.AppendText("\nIteration: " + i + ", MATCH: " + ok + ", Time: " + sw.ElapsedMilliseconds + " ms\n");
                        richTextBox_Test.ScrollToCaret();

                        //Application.DoEvents();
                    }
                    catch (System.Exception ex)
                    {
                        richTextBox_Test.Text += "\nException: " + ex.Message;
                        error = true;
                    }
                }

                if (!error)
                {
                    richTextBox_Test.Text += "\n\nTest Successful\n" + iterations + " Iterations took " + sw.ElapsedMilliseconds + " ms" +
                                             "\n\n Average Time: " + (sw.ElapsedMilliseconds / iterations) + " ms per Encryption and Decryption";
                }
                else
                {
                    richTextBox_Test.Text += "\n\nTest Failed";
                }
            }
            else
            {
                MessageBox.Show("Invalid Settings detected. Please check the settings in the 'Test' tab.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
        }