void Send() { 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.EncryptString(txtMessage.Text, key, dateTimeIv); byte[] mahoa = Diff.MaHoaDiffie(nhankey, s); byte[] dodai = BitConverter.GetBytes(mahoa.Length); byte[] initvector = Diff.IV; if (client != null && txtMessage.Text != string.Empty) { client.Send(dodai); client.Send(mahoa); client.Send(initvector); } }
int AddPadding() { string Timestamp = new DateTimeOffset(DateTime.UtcNow).ToUniversalTime().ToString("yyyyMMddHHmmssffff"); string MHtimeStamp = md5.maHoaMd5(Timestamp); int soByteCuaChuoi = UTF8Encoding.UTF8.GetByteCount(textBox1.Text); int i = 0; string tmpTime = string.Empty; if (soByteCuaChuoi % 16 != 0) { i = 1; int length = soByteCuaChuoi; while (length % 16 != 0) { tmpTime = MHtimeStamp.Substring(0, i); length = length + 1; i = i + 1; } } textBox1.Text = textBox1.Text + tmpTime; return(i + 2); }
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(); } }