public void encryptmess(string mess) { if (mess.Length > 0) { md5 md5 = new md5(); listBoxclient.Items.Add("Me: " + mess); Aes256 aes256 = new Aes256(); SHA256 sha256 = SHA256Managed.Create(); byte[] mess1 = Encoding.UTF8.GetBytes(mess); byte[] key1 = sha256.ComputeHash(Encoding.ASCII.GetBytes(Convert.ToString(dh.Key_Chung))); txtkey.Text = Convert.ToBase64String(key1); string ivc = getdatime(); byte[] iv = create_init_vector(ivc); txtiv.Text = Convert.ToBase64String(iv); byte[] dayhash = sha256.ComputeHash(Encoding.ASCII.GetBytes(ivc)); string encrypt = aes256.EncryptString(mess1, key1, iv, dayhash); string[] enc1 = encrypt.Split(';'); string mess_md5 = md5.GetMD5(enc1[0] + Convert.ToString(dh.Key_Chung)); string messsend = "Receive;" + encrypt + ";" + ivc + ";" + mess_md5; //string[] s1 = messsend.Split(';'); //textBoxmessnoise.Text = s1[1].ToString() + ";" + s1[2].ToString() + ";" + s1[3].ToString(); txtmessnoise.Text = messsend; senddata(messsend); } }
//Check MD5 public bool check_md5(string encrypt, string md5_encrypt) { bool check = false; md5 md5 = new md5(); string result = md5.GetMD5(encrypt + Convert.ToString(dh.Key_Chung)); if (result == md5_encrypt) { check = true; } return(check); }
//Tính Padding public string tinhpading(string text) { md5 hash = new md5(); string ivc = getdatime(); string chuoipading; int i = 0; int n = text.Length % 16; if (n != 0) { string thempading = hash.GetMD5(ivc).Substring(0, 16 - n); chuoipading = text + thempading; } else { chuoipading = text; } return(chuoipading); }