private void btnEncrypt_Click(object sender, EventArgs e) { dt = MD5.HashMd5(DateTime.Now.ToString()); string valuePadding = paddingData().ToString(); string encryptedText = Encrypted(txtPadding.Text, txtKey.Text, dt); string MD5encryptedText = MD5.HashMd5(encryptedText); txtEncrypted.Text = encryptedText + ";" + dt + ";" + MD5encryptedText + ";" + txtKey.Text + ";" + valuePadding; }
private void btnSend_Click(object sender, EventArgs e) { string text = txtEncrypted.Text; byte[] data = new byte[1024 * 24]; data = Encoding.ASCII.GetBytes(text); listBox1.Items.Add("You : " + txtMessage.Text); txtMessage.Text = ""; KhoiTaoTimer(); client.Send(data); data = new byte[1024]; client.Receive(data); string s = Encoding.ASCII.GetString(data); string[] arraystring = s.Split(';'); string encryptedText = arraystring[0]; string dt = arraystring[1]; string md5EncryptedText = arraystring[2]; txtKey.Text = arraystring[3]; string paddingValue = arraystring[4]; string hashEncryptedText = MD5.HashMd5(encryptedText); if (md5EncryptedText == hashEncryptedText) { txtMessaged_Receive.Text = encryptedText; string rawText = Decrypted(txtMessaged_Receive.Text, dt); txtDecrypted.Text = rawText.Substring(0, rawText.Length - int.Parse(paddingValue)); listBox1.Items.Add("Him :" + txtDecrypted.Text); } else { listBox1.Invoke((MethodInvoker) delegate() { listBox1.Items.Add("Error: Noi dung da bi thay doi."); }); } IPEndPoint ip = (IPEndPoint)client.RemoteEndPoint; txtIP.Text = ip.Address.ToString(); }
private int paddingData() { string Timestamp = new DateTimeOffset(DateTime.UtcNow).ToUniversalTime().ToString("yyyyMMddHHmmss"); string MHtimeStamp = MD5.HashMd5(Timestamp); int soByteCuaChuoi = UTF8Encoding.UTF8.GetByteCount(txtMessage.Text); int lengthNeed = 32 - soByteCuaChuoi; 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++; } } txtPadding.Text = txtMessage.Text + tmpTime; return(i - 1); }