|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.ComponentModel; |
| 4 | +using System.Data; |
| 5 | +using System.Drawing; |
| 6 | +using System.IO; |
| 7 | +using System.Linq; |
| 8 | +using System.Text; |
| 9 | +using System.Threading.Tasks; |
| 10 | +using System.Windows.Forms; |
| 11 | +using System.Security.Cryptography; |
| 12 | +using System.Diagnostics; |
| 13 | +using static System.Windows.Forms.VisualStyles.VisualStyleElement; |
| 14 | + |
| 15 | +namespace SymmetricEncryptionAlgorithms |
| 16 | +{ |
| 17 | + public partial class Form1 : Form |
| 18 | + { |
| 19 | + private byte[] key; |
| 20 | + private byte[] IV; |
| 21 | + private byte[] cipherBytes; |
| 22 | + List<CipherMode> cipherModesLocal = new List<CipherMode>() { CipherMode.ECB, CipherMode.CBC, CipherMode.CFB }; |
| 23 | + |
| 24 | + private Dictionary<string, long> dictionaryCipherResults; |
| 25 | + private Dictionary<string, long> dictionaryDecipherResults; |
| 26 | + public Form1() |
| 27 | + { |
| 28 | + InitializeComponent(); |
| 29 | + |
| 30 | + openFileDialog1.Filter = "Text files(*.txt)|*.txt|All files(*.*)|*.*"; |
| 31 | + |
| 32 | + string[] cipherTypes = { "All", "DES", "3DES", "RC2", "Rijndael", "AES" }; |
| 33 | + string[] cipherModes = { "ECB", "CBC", "CFB" }; |
| 34 | + |
| 35 | + |
| 36 | + comboBoxMethods.Items.AddRange(cipherTypes); |
| 37 | + comboBoxMethods.SelectedIndex = 0; |
| 38 | + comboBoxMode.Items.AddRange(cipherModes); |
| 39 | + |
| 40 | + chartEncryption.Series.Clear(); |
| 41 | + chartDecryption.Series.Clear(); |
| 42 | + |
| 43 | + for (int i = 1; i < cipherTypes.Length; i++) |
| 44 | + { |
| 45 | + chartEncryption.Series.Add(cipherTypes[i]); |
| 46 | + chartEncryption.Series[cipherTypes[i]].XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Auto; |
| 47 | + chartEncryption.Series[cipherTypes[i]].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Column; |
| 48 | + |
| 49 | + chartDecryption.Series.Add(cipherTypes[i]); |
| 50 | + chartDecryption.Series[cipherTypes[i]].XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Auto; |
| 51 | + chartDecryption.Series[cipherTypes[i]].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Column; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + private void buttonOpen_Click(object sender, EventArgs e) |
| 56 | + { |
| 57 | + if (openFileDialog1.ShowDialog() != DialogResult.Cancel) |
| 58 | + { |
| 59 | + string filename = openFileDialog1.FileName; |
| 60 | + string fileText = File.ReadAllText(filename); |
| 61 | + textBoxSource.Text = fileText; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + private void buttonStart_Click(object sender, EventArgs e) |
| 66 | + { |
| 67 | + SymmetricAlgorithm saDES = DES.Create(); |
| 68 | + SymmetricAlgorithm sa3DES = TripleDES.Create(); |
| 69 | + SymmetricAlgorithm saRC2 = RC2.Create(); |
| 70 | + SymmetricAlgorithm saRijndael = Rijndael.Create(); |
| 71 | + SymmetricAlgorithm saAES = Aes.Create(); |
| 72 | + |
| 73 | + switch (comboBoxMethods.SelectedItem.ToString()) |
| 74 | + { |
| 75 | + case "All": |
| 76 | + RunCipher(new List<SymmetricAlgorithm> { saDES, sa3DES, saRC2, saRijndael, saAES }, cipherModesLocal, new List<string> { "DES", "3DES", "RC2", "Rijndael", "AES" }); |
| 77 | + break; |
| 78 | + case "DES": |
| 79 | + RunCipher(new List<SymmetricAlgorithm> { saDES }, cipherModesLocal, new List<string> { "DES" }); |
| 80 | + break; |
| 81 | + case "3DES": |
| 82 | + RunCipher(new List<SymmetricAlgorithm> { sa3DES }, cipherModesLocal, new List<string> { "3DES" }); |
| 83 | + break; |
| 84 | + case "RC2": |
| 85 | + RunCipher(new List<SymmetricAlgorithm> { saRC2 }, cipherModesLocal, new List<string> { "RC2" }); |
| 86 | + break; |
| 87 | + case "Rijndael": |
| 88 | + RunCipher(new List<SymmetricAlgorithm> { saRijndael }, cipherModesLocal, new List<string> { "Rijndael" }); |
| 89 | + break; |
| 90 | + case "AES": |
| 91 | + RunCipher(new List<SymmetricAlgorithm> { saAES }, cipherModesLocal, new List<string> { "AES" }); |
| 92 | + break; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + private void RunCipher(List<SymmetricAlgorithm> symmetricAlgorithm, List<CipherMode> cipherModesLocal, List<string> symmetricAlgorithmList) |
| 97 | + { |
| 98 | + dictionaryCipherResults = new Dictionary<string, long>(); |
| 99 | + dictionaryDecipherResults = new Dictionary<string, long>(); |
| 100 | + chartEncryption.Series.Clear(); |
| 101 | + chartDecryption.Series.Clear(); |
| 102 | + textBoxEncrypted.Clear(); |
| 103 | + textBoxDecrypted.Clear(); |
| 104 | + for (int i = 0; i < symmetricAlgorithm.Count; i++) |
| 105 | + { |
| 106 | + for (int j = 0; j < cipherModesLocal.Count; j++) |
| 107 | + { |
| 108 | + progressBar1.PerformStep(); |
| 109 | + Stopwatch stopwatch = new Stopwatch(); |
| 110 | + stopwatch.Start(); |
| 111 | + symmetricAlgorithm[i].GenerateKey(); |
| 112 | + key = symmetricAlgorithm[i].Key; |
| 113 | + symmetricAlgorithm[i].GenerateIV(); |
| 114 | + IV = symmetricAlgorithm[i].IV; |
| 115 | + symmetricAlgorithm[i].Mode = cipherModesLocal[j]; |
| 116 | + symmetricAlgorithm[i].Padding = PaddingMode.PKCS7; |
| 117 | + |
| 118 | + MemoryStream ms = new MemoryStream(); |
| 119 | + CryptoStream cs = new CryptoStream(ms, symmetricAlgorithm[i].CreateEncryptor(), CryptoStreamMode.Write); |
| 120 | + byte[] plainbytes = Encoding.UTF8.GetBytes(textBoxSource.Text.ToCharArray()); |
| 121 | + cs.Write(plainbytes, 0, plainbytes.Length); |
| 122 | + cs.Close(); |
| 123 | + cipherBytes = ms.ToArray(); |
| 124 | + ms.Close(); |
| 125 | + stopwatch.Stop(); |
| 126 | + |
| 127 | + string ciphedText = Encoding.UTF8.GetString(cipherBytes); |
| 128 | + dictionaryCipherResults.Add(symmetricAlgorithmList[i] + " " + cipherModesLocal[j].ToString(), stopwatch.ElapsedTicks); |
| 129 | + textBoxEncrypted.Text += $"{symmetricAlgorithmList[i]} {cipherModesLocal[j]}: {ciphedText}\r\n"; |
| 130 | + |
| 131 | + stopwatch = new Stopwatch(); |
| 132 | + stopwatch.Start(); |
| 133 | + symmetricAlgorithm[i].Key = key; |
| 134 | + symmetricAlgorithm[i].IV = IV; |
| 135 | + MemoryStream ms1 = new MemoryStream(cipherBytes); |
| 136 | + CryptoStream cs1 = new CryptoStream(ms1, symmetricAlgorithm[i].CreateEncryptor(), CryptoStreamMode.Read); |
| 137 | + byte[] plainbytes1 = new Byte[cipherBytes.Length]; |
| 138 | + cs1.Read(plainbytes1, 0, cipherBytes.Length); |
| 139 | + cs1.Close(); |
| 140 | + ms1.Close(); |
| 141 | + stopwatch.Stop(); |
| 142 | + |
| 143 | + string deciphedText = Encoding.UTF8.GetString(plainbytes); |
| 144 | + dictionaryDecipherResults.Add(symmetricAlgorithmList[i] + " " + cipherModesLocal[j], stopwatch.ElapsedTicks); |
| 145 | + textBoxDecrypted.Text += $"{symmetricAlgorithmList[i]} {cipherModesLocal[j]}: {deciphedText}\r\n"; |
| 146 | + } |
| 147 | + } |
| 148 | + int counter = 0; |
| 149 | + foreach (KeyValuePair<string, long> item in dictionaryCipherResults) |
| 150 | + { |
| 151 | + chartEncryption.Series.Add(item.Key); |
| 152 | + chartEncryption.Series[counter].Points.AddY(item.Value); |
| 153 | + counter++; |
| 154 | + } |
| 155 | + counter = 0; |
| 156 | + foreach (KeyValuePair<string, long> item in dictionaryDecipherResults) |
| 157 | + { |
| 158 | + chartDecryption.Series.Add(item.Key); |
| 159 | + chartDecryption.Series[counter].Points.AddY(item.Value); |
| 160 | + counter++; |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + private void comboBoxMethods_SelectedIndexChanged(object sender, EventArgs e) |
| 165 | + { |
| 166 | + if (comboBoxMethods.SelectedItem.ToString() != "All") |
| 167 | + { |
| 168 | + comboBoxMode.SelectedIndex = 0; |
| 169 | + } |
| 170 | + else |
| 171 | + { |
| 172 | + comboBoxMode.SelectedIndex = -1; |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + private void comboBoxMode_SelectedIndexChanged(object sender, EventArgs e) |
| 177 | + { |
| 178 | + cipherModesLocal.Clear(); |
| 179 | + comboBoxMode.Enabled = true; |
| 180 | + switch (comboBoxMode.SelectedItem != null ? comboBoxMode.SelectedItem.ToString() : "All") |
| 181 | + { |
| 182 | + case "All": |
| 183 | + cipherModesLocal.Add(CipherMode.ECB); |
| 184 | + cipherModesLocal.Add(CipherMode.CBC); |
| 185 | + cipherModesLocal.Add(CipherMode.CFB); |
| 186 | + comboBoxMode.SelectedIndex = -1; |
| 187 | + comboBoxMode.Text = ""; |
| 188 | + comboBoxMode.Enabled = false; |
| 189 | + break; |
| 190 | + case "ECB": |
| 191 | + cipherModesLocal.Add(CipherMode.ECB); |
| 192 | + break; |
| 193 | + case "CBC": |
| 194 | + cipherModesLocal.Add(CipherMode.CBC); |
| 195 | + break; |
| 196 | + case "CFB": |
| 197 | + cipherModesLocal.Add(CipherMode.CFB); |
| 198 | + break; |
| 199 | + } |
| 200 | + } |
| 201 | + } |
| 202 | +} |
0 commit comments