private void Encrypt() { try { RSAx rsax = RSAx.CreateFromXML(Key, ModSize); rsax.RSAxHashAlgorithm = HashAlgorithm; byte[] ct = rsax.Encrypt(Encoding.UTF8.GetBytes(TextPlain), InvertPPK, UseOAEP); TextCrypt = string.Join("\r\n", GetChunks(Convert.ToBase64String(ct), 50)); } catch (Exception ex) { MessageBox.Show("Exception while Encryption: " + ex.Message); } }
private void button_Encrypt_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[] CT = rsax.Encrypt(Encoding.UTF8.GetBytes(richTextBox_PlainText.Text), checkBox_PrivateKey.Checked, checkBox_OAEP.Checked); richTextBox_CipherText.Text = Convert.ToBase64String(CT); } catch (System.Exception ex) { MessageBox.Show("Exception while Encryption: " + ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Hand); } ButtonState(true); }
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); } }