1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101:
| using System.Text; using System.Windows;
namespace DiffieHellmanTest { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); }
private DiffieHellman Alice = null; private DiffieHellman Bob = null;
public string asString(byte[] Value) { StringBuilder Result = new StringBuilder(); foreach (byte Byte in Value) { Result.AppendFormat("{0:X2} ", Byte); }
return Result.ToString(); }
public void LogCommunication(string From, string To, string Message, string Hint) { EveLog.Items.Add(string.Format("{0} ==> {1} ({3})\n {2}", From, To, Message, Hint)); }
public void LogAlice(string Message) { AliceLog.Items.Add(string.Format("{0}", Message)); } public void LogBob(string Message) { BobLog.Items.Add(string.Format("{0}", Message)); }
private void Button_Click(object sender, RoutedEventArgs e) { Alice = new DiffieHellman(); AlicePK.Text = asString(Alice.PublicKey); Bob = new DiffieHellman(); BobPK.Text = asString(Bob.PublicKey); }
private void Button_Click_1(object sender, RoutedEventArgs e) { Bob.SetPartnerPublicKey(Alice.PublicKey); LogCommunication("Alice", "Bob", asString(Alice.PublicKey), "public Key Alice"); LogBob("From Alice: " + asString(Alice.PublicKey)); BobCK.Text = asString(Bob.secretKey);
Alice.SetPartnerPublicKey(Bob.PublicKey); LogCommunication("Bob", "Alice", asString(Bob.PublicKey), "public Key Bob"); LogAlice("From Bob: " + asString(Bob.PublicKey)); AliceCK.Text = asString(Alice.secretKey); }
private void Button_Click_2(object sender, RoutedEventArgs e) { string Message = AlliceText.Text; if (!string.IsNullOrWhiteSpace(Message)) { byte[] encryptedMessage; byte[] iv; Alice.Encrypt(Alice.secretKey, Message, out encryptedMessage, out iv); LogAlice(string.Format("Send to Bob: {0} => {1}", Message, Encoding.UTF8.GetString(encryptedMessage))); LogCommunication("Alice", "Bob", Encoding.UTF8.GetString(encryptedMessage), "encrypted Message"); LogCommunication("Alice", "Bob", Encoding.UTF8.GetString(iv), "Initialization Vector");
string DecryptedMessage = Bob.Decrypt(encryptedMessage, iv); LogBob(string.Format("From Alice: {0} => {1}", Encoding.UTF8.GetString(encryptedMessage), DecryptedMessage)); } }
private void Button_Click_3(object sender, RoutedEventArgs e) { string Message = BobText.Text; if (!string.IsNullOrWhiteSpace(Message)) { byte[] encryptedMessage; byte[] iv; Bob.Encrypt(Bob.secretKey, Message, out encryptedMessage, out iv); LogBob(string.Format("Send to Alice: {0} => {1}", Message, Encoding.UTF8.GetString(encryptedMessage))); LogCommunication("Bob", "Alice", Encoding.UTF8.GetString(encryptedMessage), "encrypted Message"); LogCommunication("Bob", "Alice", Encoding.UTF8.GetString(iv), "Initialization Vector");
string DecryptedMessage = Alice.Decrypt(encryptedMessage, iv); LogAlice(string.Format("From Bob: {0} => {1}", Encoding.UTF8.GetString(encryptedMessage), DecryptedMessage)); } } } } |