private void EnButton_Click(object sender, EventArgs e) { type = 0; byte[] tmp1 = new byte[100]; if (this.keyBox.Text.Length > 0)//输入新的密钥 key = Encoding.Default.GetBytes(this.keyBox.Text); foreach (byte b in key)//将密钥录入kl; kl.Add(b); if (this.plainBox.Text.Length > 0)//输入新的明文 { cipherBox.Text = ""; tmp1 = Encoding.Default.GetBytes(this.plainBox.Text); pl.Clear(); for (int i = 0; i < tmp1.Length; i++) pl.Add(tmp1[i]);//将明文录入pl DES des = new DES(0); Stopwatch sw = new Stopwatch(); sw.Start(); this.ci = des.DesRun(pl, kl);//DES 加密 sw.Stop(); TimeSpan ts = sw.Elapsed; time = ts.TotalMilliseconds; tmp1 = ListToByteArray(ci); resultBox.Text = "加密后:"+Encoding.Default.GetString(tmp1);//打印结果 MessageBox.Show("加密总花费" + (time.ToString() + "ms.")); } kl.Clear(); key = Encoding.UTF8.GetBytes("computer");//重置初始密钥 }
private void deButton_Click(object sender, EventArgs e) { type = 1; byte[] tmp = new byte[100]; if (this.keyBox.Text.Length > 0) key = Encoding.Default.GetBytes(this.keyBox.Text);//输入新的密钥 foreach (byte b in key)//将密钥录入kl; kl.Add(b); if (this.cipherBox.Text.Length > 0)//输入新的密文 { plainBox.Text = ""; DES des1 = new DES(1); ci.Clear(); tmp = Encoding.Default.GetBytes(this.cipherBox.Text);//字符串转换为字节数组 for (int i = 0; i < tmp.Length; i++) ci.Add(tmp[i]);//将密文录入ci Stopwatch sw = new Stopwatch(); sw.Start(); pl = des1.DesRun(ci, kl); tmp = ListToByteArray(pl); sw.Stop(); TimeSpan ts = sw.Elapsed; time = ts.TotalMilliseconds; resultBox.Text ="解密后:"+Encoding.Default.GetString(tmp); MessageBox.Show("解密总花费" + (time.ToString() + "ms.")); } else//不输入新明文,直接解密 { Stopwatch sw = new Stopwatch(); sw.Start(); DES des0 = new DES(1); pl = des0.DesRun(this.ci, this.kl);//DES 解密 sw.Stop(); TimeSpan ts = sw.Elapsed; time = ts.TotalMilliseconds; tmp = ListToByteArray(pl); resultBox.Text="解密后:"+Encoding.Default.GetString(tmp);//打印结果 MessageBox.Show("解密总花费" + (time.ToString() + "ms.")); } kl.Clear(); key = Encoding.UTF8.GetBytes("computer");//重置初始密钥 }