/// <summary> /// Receive Message /// </summary> void Receive() { try { c : while (true)//luôn luôn nhận { data = new byte[1024]; client.Receive(data); if (string.Equals(Encoding.UTF8.GetString(data), "guikey", StringComparison.InvariantCultureIgnoreCase)) { txtkeyserver.Clear(); txtFinalKey.Clear(); nhankeydadoi = new byte[140]; client.Receive(nhankeydadoi); keypublic = Convert.ToBase64String(nhankeydadoi); txtkeyserver.Text = keypublic; TaoKey(); Diff.LayKhoaBiMat(nhankeydadoi); khoabimat = Diff.aes.Key; keysecret = Convert.ToBase64String(khoabimat); txtFinalKey.Text = keysecret; nhankey = nhankeydadoi; guilaipublickey(); } else if (string.Equals(Encoding.UTF8.GetString(data), "guikeytoclient", StringComparison.InvariantCultureIgnoreCase)) { txtkeyserver.Clear(); txtFinalKey.Clear(); nhankeydadoi = new byte[140]; client.Receive(nhankeydadoi); keypublic = Convert.ToBase64String(nhankeydadoi); txtkeyserver.Text = keypublic; Diff.LayKhoaBiMat(nhankeydadoi); khoabimat = Diff.aes.Key; keysecret = Convert.ToBase64String(khoabimat); txtFinalKey.Text = keysecret; nhankey = nhankeydadoi; } else { dateTimeIV = md5.maHoaMd5(DateTime.Now.ToString()); string time = dateTimeIV.Substring(0, 16); dateTimeIv = Encoding.UTF8.GetBytes(time); tinnhan = new byte[BitConverter.ToInt32(data, 0)]; client.Receive(tinnhan); byte[] nhanvector = new byte[16]; client.Receive(nhanvector); string message = Diff.GiaiMaDiffie(nhankey, tinnhan, nhanvector); string a = txtFinalKey.Text.Substring(0, 32); byte[] key = Encoding.ASCII.GetBytes(a); string s = aes.DecryptString(message, key, dateTimeIv); foreach (char c in s) { if (c.ToString() == ";") { string[] tokens = s.Trim().Split(';'); compare1 = tokens[1]; compare2 = md5.maHoaMd5(tokens[0]); if (compare1 != compare2) { AddMessage(tokens[0]); MessageHeTHong("chuỗi này đã bị thay đổi vì 2 chuỗi mã hóa khác nhau :"); MessageHeTHong("Chuỗi mã hóa trước khi gửi :" + compare1); MessageHeTHong("chuỗi mã hóa sau khi mã hóa tin nhận :" + compare2); goto c; } } } AddMessage(s); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
void Receive() { try { c : while (true) { byte[] data = new byte[1024]; client.Receive(data); if (string.Equals(Encoding.UTF8.GetString(data), "guikey", StringComparison.InvariantCultureIgnoreCase)) { txtServerKey.Clear(); txtFinalKey.Clear(); nhankeydadoi = new byte[140]; client.Receive(nhankeydadoi); keypublic = Convert.ToBase64String(nhankeydadoi); txtServerKey.Text = keypublic; CreatePublicKey(); Diff.LayKhoaBiMat(nhankeydadoi); khoabimat = Diff.aes.Key; keysecret = Convert.ToBase64String(khoabimat); txtFinalKey.Text = keysecret; nhankey = nhankeydadoi; guilaipublickey(); } else if (string.Equals(Encoding.UTF8.GetString(data), "guikeytoclient", StringComparison.InvariantCultureIgnoreCase)) { txtServerKey.Clear(); txtFinalKey.Clear(); nhankeydadoi = new byte[140]; client.Receive(nhankeydadoi); keypublic = Convert.ToBase64String(nhankeydadoi); txtServerKey.Text = keypublic; Diff.LayKhoaBiMat(nhankeydadoi); khoabimat = Diff.aes.Key; keysecret = Convert.ToBase64String(khoabimat); txtFinalKey.Text = keysecret; nhankey = nhankeydadoi; } else { tinnhan = new byte[BitConverter.ToInt32(data, 0)]; client.Receive(tinnhan); byte[] nhanvector = new byte[16]; client.Receive(nhanvector); string message = Diff.GiaiMaDiffie(nhankey, tinnhan, nhanvector); dateTimeIV = md5.maHoaMd5(DateTime.Now.ToString()); string time = dateTimeIV.Substring(0, 16); dateTimeIv = Encoding.UTF8.GetBytes(time); string a = txtFinalKey.Text.Substring(0, 32); byte[] key = Encoding.ASCII.GetBytes(a); string s = aes.DecryptString(message, key, dateTimeIv); foreach (char c in s) { if (c.ToString() == ";") { string[] tokens = s.Trim().Split(';'); compare1 = tokens[1]; compare2 = md5.maHoaMd5(tokens[0]); if (compare1 != compare2) { LstMS.Items.Add("Server: " + tokens[0]); LstMS.Items.Add("Dữ liệu đã bị thay đổi trong quá trình chuyền:"); LstMS.Items.Add("Chuỗi mã trước:" + compare1); LstMS.Items.Add("Chuỗi mã sau:" + compare2); goto c; } } } LstMS.Items.Add("Server: " + s); } } }catch { Close(); } }
void Receive() { try { c : while (true) { data = new byte[1024]; client.Receive(data); if (string.Equals(Encoding.UTF8.GetString(data), "guikey", StringComparison.InvariantCultureIgnoreCase)) { textBox2.Clear(); textBox4.Clear(); nhankeydadoi = new byte[140]; client.Receive(nhankeydadoi); keypublic = Convert.ToBase64String(nhankeydadoi); textBox2.Text = keypublic; TaoKey(); Diff.LayKhoaBiMat(nhankeydadoi); khoabimat = Diff.aes.Key; keysecret = Convert.ToBase64String(khoabimat); textBox4.Text = keysecret; nhankey = nhankeydadoi; guilaipublickey(); } else if (string.Equals(Encoding.UTF8.GetString(data), "guikeytoclient", StringComparison.InvariantCultureIgnoreCase)) { textBox2.Clear(); textBox4.Clear(); nhankeydadoi = new byte[140]; client.Receive(nhankeydadoi); keypublic = Convert.ToBase64String(nhankeydadoi); textBox2.Text = keypublic; Diff.LayKhoaBiMat(nhankeydadoi); khoabimat = Diff.aes.Key; keysecret = Convert.ToBase64String(khoabimat); textBox4.Text = keysecret; nhankey = nhankeydadoi; } else { dateTimeIV = md5.maHoaMd5(DateTime.Now.ToString()); string time = dateTimeIV.Substring(0, 16); dateTimeIv = Encoding.UTF8.GetBytes(time); tinnhan = new byte[BitConverter.ToInt32(data, 0)]; client.Receive(tinnhan); byte[] nhanvector = new byte[16]; client.Receive(nhanvector); string message = Diff.GiaiMaDiffie(nhankey, tinnhan, nhanvector); string a = textBox4.Text.Substring(0, 32); byte[] key = Encoding.ASCII.GetBytes(a); string s = aes.DecryptString(message, key, dateTimeIv); string[] arr = s.Split('|'); string padding = arr[1]; string result = s.Substring(0, s.Length - int.Parse(padding)); AddMessage(result); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } }