· 6 years ago · Mar 11, 2019, 12:24 PM
1// Form1.cs
2
3using System;
4using System.Collections.Generic;
5using System.ComponentModel;
6using System.Data;
7using System.Drawing;
8using System.Linq;
9using System.Text;
10using System.Threading.Tasks;
11using System.Windows.Forms;
12using System.Security.Cryptography;
13using System.IO;
14
15namespace ei_si_worksheet4
16{
17 public partial class Form1 : Form
18 {
19 // Constants
20 const string FILENAME_PUBLIC_KEY = "PublicKey.xml";
21 const string FILENAME_PRIVATE_PUBLIC_KEY = "PrivatePublicKey.xml";
22
23 public Form1()
24 {
25 InitializeComponent();
26 }
27
28 private void Form1_Load(object sender, EventArgs e)
29 {
30
31 }
32
33 private void ButtonGererateKeys_Click(object sender, EventArgs e)
34 {
35 try
36 {
37 using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
38 {
39 // FALSE -> EXPORTS ONLY THE PUBLIC KEY
40 textboxPublicKey.Text = rsa.ToXmlString(false);
41
42 //TRUE -> EXPORTS THE PUBLIC AND PRIVATE KEY
43 textboxBothKeys.Text = rsa.ToXmlString(true);
44 }
45 }
46 catch (ArgumentNullException ex)
47 {
48 MessageBox.Show($"Error: {ex.Message}");
49 }
50 }
51
52 private void ButtonSavePublicKey_Click(object sender, EventArgs e)
53 {
54 File.WriteAllText(FILENAME_PUBLIC_KEY, textboxPublicKey.Text);
55 MessageBox.Show("File created with Public Key!");
56 }
57
58 private void ButtonSaveKeys_Click(object sender, EventArgs e)
59 {
60 File.WriteAllText(FILENAME_PRIVATE_PUBLIC_KEY, textboxBothKeys.Text);
61 MessageBox.Show("File created with Public Key and Private key!");
62 }
63
64 private void ButtonEncrypt_Click(object sender, EventArgs e)
65 {
66 string publicKey = null;
67 byte[] secretKey = null; // Plain texto to be ciphered
68 byte[] cipheredData = null;
69
70 try
71 {
72 secretKey = Encoding.UTF8.GetBytes(textboxSymmentricKey.Text);
73
74 using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
75 {
76 // Get public key from the "person to whom we are going to send the secret key"
77 publicKey = File.ReadAllText(FILENAME_PUBLIC_KEY);
78 rsa.FromXmlString(publicKey);
79
80 // Encrypt data using the public key
81 cipheredData = rsa.Encrypt(secretKey, true); // True -> OAEP padding
82 textboxSymmetricKeyEncrtypted.Text = Convert.ToBase64String(cipheredData);
83 textboxBitSize.Text = (cipheredData.Length * 8).ToString();
84 }
85 }
86 catch (CryptographicException ex)
87 {
88 MessageBox.Show($"Error encrypting the key. Details: {ex.Message}");
89 }
90 }
91
92 private void ButtonDecrypt_Click(object sender, EventArgs e)
93 {
94 string privateKey = null;
95 byte[] cipheredData = null;
96 byte[] secretKey = null; // plain text
97
98 try
99 {
100 cipheredData = Convert.FromBase64String(textboxSymmetricKeyEncrtypted.Text);
101
102 using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
103 {
104 privateKey = File.ReadAllText(FILENAME_PRIVATE_PUBLIC_KEY);
105 rsa.FromXmlString(privateKey);
106
107 secretKey = rsa.Decrypt(cipheredData, true); // True -> OAEP padding
108
109 textboxSymmetricKeyDecrypted.Text = Encoding.UTF8.GetString(secretKey);
110 }
111 }
112 catch (CryptographicException ex)
113 {
114 MessageBox.Show($"Error decrypting the secret key. Details: {ex.Message}");
115 }
116 }
117 }
118}