/// <summary> /// Receive Message /// </summary> void Receive(object obj) { Socket client = obj as Socket; try { c : while (true)//luôn luôn nhận { data = new byte[1024]; client.Receive(data); if (string.Equals(Encoding.UTF8.GetString(data), "guikeyserver", StringComparison.InvariantCultureIgnoreCase)) { nhankeydadoi = new byte[140]; client.Receive(nhankeydadoi); keypublic = Convert.ToBase64String(nhankeydadoi); txtkeyclient.Text = keypublic; diff.LayKhoaBiMat(nhankeydadoi); khoabimat = diff.aes.Key; keysecret = Convert.ToBase64String(khoabimat); txbSecretkey.Text = keysecret; nhankey = nhankeydadoi; } else if (string.Equals(Encoding.UTF8.GetString(data), "guikeytoserver", StringComparison.InvariantCultureIgnoreCase)) { nhankeydadoi = new byte[140]; client.Receive(nhankeydadoi); keypublic = Convert.ToBase64String(nhankeydadoi); txtkeyclient.Text = keypublic; TaoKey(); diff.LayKhoaBiMat(nhankeydadoi); khoabimat = diff.aes.Key; keysecret = Convert.ToBase64String(khoabimat); txbSecretkey.Text = keysecret; nhankey = nhankeydadoi; guiLaiKeyChoClientVuaGui(); } 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 = txbSecretkey.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 { clientList.Remove(client); client.Close(); } }
void Receive(object obj) { Socket client = obj as Socket; try { while (true)//luôn luôn nhận { data = new byte[1024]; client.Receive(data); if (string.Equals(Encoding.UTF8.GetString(data), "guikeyserver", StringComparison.InvariantCultureIgnoreCase)) { nhankeydadoi = new byte[140]; client.Receive(nhankeydadoi); keypublic = Convert.ToBase64String(nhankeydadoi); txtkeyclient.Text = keypublic; diff.LayKhoaBiMat(nhankeydadoi); khoabimat = diff.aes.Key; keysecret = Convert.ToBase64String(khoabimat); txbSecretkey.Text = keysecret; nhankey = nhankeydadoi; } else if (string.Equals(Encoding.UTF8.GetString(data), "guikeytoserver", StringComparison.InvariantCultureIgnoreCase)) { nhankeydadoi = new byte[140]; client.Receive(nhankeydadoi); keypublic = Convert.ToBase64String(nhankeydadoi); txtkeyclient.Text = keypublic; TaoKey(); diff.LayKhoaBiMat(nhankeydadoi); khoabimat = diff.aes.Key; keysecret = Convert.ToBase64String(khoabimat); txbSecretkey.Text = keysecret; nhankey = nhankeydadoi; guiLaiKeyChoClientVuaGui(); } 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 = txbSecretkey.Text.Substring(0, 32); byte[] key = Encoding.ASCII.GetBytes(a); string s = aes.DecryptString(message, key, dateTimeIv); AddMessage(s); } } } catch { clientList.Remove(client); client.Close(); } }