private void KryptoMainForm_Load(object sender, EventArgs e) { EncryptInputFileField.Text = ENCRYPTION_INPUT_PATH; EncryptOutputFileField.Text = ENCRYPTION_OUTPUT_PATH; DecryptInputFileField.Text = ENCRYPTION_OUTPUT_PATH; DecryptOutputFileField.Text = DECRYPTION_OUTPUT_PATH; AlphabetField1.Text = RSACore.GetAlphabet(); AlphabetField2.Text = RSACore.GetAlphabet(); }
private void GenerateKeysButton_Click(object sender, EventArgs evt) { string publickey; string privatekey; BigInteger p; BigInteger q; try { p = BigInteger.Parse(TextBoxP.Text); q = BigInteger.Parse(TextBoxQ.Text); if ((TextBoxP.Text == "") || TextBoxQ.Text == "") { MessageBox.Show("Please specify both p and q parameters and try again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } else if (!IsNumeric(TextBoxP.Text) || !IsNumeric(TextBoxQ.Text)) { MessageBox.Show("Non-numeric input detected! Please double-check and try again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } else if (!RSACore.IsPrime(p) || !RSACore.IsPrime(q)) { MessageBox.Show("Both p and q parameters should be prime numbers! Please double-check and try again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } else { KeyGenerationConsole.Items.Clear(); RSACore.GetEncryptionParameters(p, q, out BigInteger n, out BigInteger phi, out BigInteger d, out BigInteger e); publickey = $"PUBLIC KEY (e, n): ({e}, {n})"; privatekey = $"PRIVATE KEY (d, n): ({d}, {n})"; KeyGenerationConsole.Items.Add($"p = {p}, q = {q}"); KeyGenerationConsole.Items.Add($"Modulus: n = {n}"); KeyGenerationConsole.Items.Add($"Euler's function: ф(n) = {phi}"); KeyGenerationConsole.Items.Add($"Public exponent: e = {e}"); KeyGenerationConsole.Items.Add($"Private exponent: d = {d}"); KeyGenerationConsole.Items.Add("-------------------------------------------------------------------------------------------"); KeyGenerationConsole.Items.Add(publickey); KeyGenerationConsole.Items.Add(privatekey); Clipboard.SetText($"{publickey}\r\n{privatekey}"); MessageBox.Show("Key pairs have been successfully generated and copied to clipboard. \nWarning: do not disclose the private key to any third-parties.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); } } catch { MessageBox.Show("An error occured. Please check whether the filled in data are correct and try again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } }
private void Encrypt(BigInteger e, BigInteger n, string inputpath, string outputpath, bool b64enabled) { if (b64enabled) { string message = b64.ToBase(ReadBytes(inputpath)); List <string> result = RSACore.Encrypt(message, e, n); WriteText(outputpath, result); } else { string message = ""; message = ReadTextStr(inputpath); List <string> result = RSACore.Encrypt(message, e, n); WriteText(outputpath, result); } }
private void Decrypt(BigInteger d, BigInteger n, string inputpath, string outputpath, bool b64enabled) { string decryptedmessage = ""; byte[] bytes; List <string> encryptedmessage = new List <string>(); encryptedmessage = ReadText(inputpath); decryptedmessage = RSACore.Decrypt(encryptedmessage, d, n); if (b64enabled) { bytes = b64.FromBase(decryptedmessage); WriteBytes(outputpath, bytes); } else { decryptedmessage = RSACore.Decrypt(encryptedmessage, d, n); WriteText(outputpath, decryptedmessage); } }
private void AlphabetField_TextChanged(object sender, EventArgs e) { RSACore.SetAlphabet(AlphabetField1.Text); }