using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; using System.Security; using System.Security.Cryptography; namespace Lock_UnLock ... { public partial class Form1 : Form ...{ public Form1() ...{ InitializeComponent(); } private byte[] sKey; private byte[] sIV; public string key = ""; private void button5_Click(object sender, EventArgs e) ...{ Application.Exit(); } private void Form1_Load(object sender, EventArgs e) ...{ this.textBox1.ReadOnly = true; this.textBox2.ReadOnly = true; this.radioButton1.Checked = true; this.label4.Text = DateTime.Now.ToLongDateString(); this.textBox3.Text = "安全密匙用于解密文件!"; this.textBox3.Focus(); int len = textBox3.Text.Length; this.textBox3.SelectionStart = 0; this.textBox3.SelectionLength = len; } private void button1_Click(object sender, EventArgs e) ...{ this.openFileDialog1.ShowDialog(); string filename = openFileDialog1.FileName; int i = filename.LastIndexOf("."); if (i != -1) ...{ string filetype = filename.Substring(filename.LastIndexOf(".")); if (this.radioButton1.Checked) ...{ filename = filename.Replace("(解密文件)", ""); textBox2.Text = filename.Substring(0, filename.LastIndexOf(".")) + "(加密文件)" + filetype; } else ...{ filename = filename.Replace("(加密文件)", ""); textBox2.Text = filename.Substring(0, filename.LastIndexOf(".")) + "(解密文件)" + filetype; } } if (filename != "") ...{ textBox1.Text = openFileDialog1.FileName; } } private void button2_Click(object sender, EventArgs e) ...{ this.saveFileDialog1.ShowDialog(); if (saveFileDialog1.FileName != "") ...{ this.textBox2.Text = saveFileDialog1.FileName; } } private void radioButton1_CheckedChanged(object sender, EventArgs e) ...{ if (radioButton1.Checked == true) ...{ this.button4.Enabled = false; this.button3.Enabled = true; } } private void radioButton2_CheckedChanged(object sender, EventArgs e) ...{ if (radioButton2.Checked == true) ...{ this.button3.Enabled = false; this.button4.Enabled = true; } } "加密方法#region "加密方法 public bool EncryptFile(string filePath, string savePath, string keyStr) ...{ key = this.textBox3.Text; DESCryptoServiceProvider des = new DESCryptoServiceProvider(); if (keyStr == "") keyStr = key; FileStream fs = File.OpenRead(filePath); byte[] inputByteArray = new byte[fs.Length]; fs.Read(inputByteArray, 0, (int)fs.Length); fs.Close(); byte[] keyByteArray = Encoding.Default.GetBytes(keyStr); SHA1 ha = new SHA1Managed(); byte[] hb = ha.ComputeHash(keyByteArray); sKey = new byte[8]; sIV = new byte[8]; for (int i = 0; i < 8; i++) sKey[i] = hb[i]; for (int i = 8; i < 16; i++) sIV[i - 8] = hb[i]; des.Key = sKey; des.IV = sIV; MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); fs = File.OpenWrite(savePath); foreach (byte b in ms.ToArray()) ...{ fs.WriteByte(b); } fs.Close(); cs.Close(); ms.Close(); return true; } #endregion "解密方法#region "解密方法 public bool DecryptFile(string filePath,string savePath,string keyStr) ...{ key = this.textBox3.Text; DESCryptoServiceProvider des = new DESCryptoServiceProvider(); if(keyStr=="") keyStr=key; FileStream fs=File.OpenRead(filePath); byte[] inputByteArray =new byte[fs.Length]; fs.Read(inputByteArray,0,(int)fs.Length); fs.Close(); byte[] keyByteArray=Encoding.Default.GetBytes(keyStr); SHA1 ha=new SHA1Managed(); byte[] hb=ha.ComputeHash(keyByteArray); sKey=new byte[8]; sIV=new byte[8]; for(int i=0;i<8;i++) sKey[i]=hb[i]; for(int i=8;i<16;i++) sIV[i-8]=hb[i]; des.Key=sKey; des.IV=sIV; MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms,des.CreateDecryptor(),CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); fs=File.OpenWrite(savePath); foreach(byte b in ms.ToArray()) ...{ fs.WriteByte(b); } fs.Close(); cs.Close(); ms.Close(); return true; } #endregion "加密#region "加密 private void button3_Click(object sender, EventArgs e) ...{ if (this.textBox1.Text!="" && this.textBox2.Text!="" && this.textBox3.Text!="") ...{ key = this.textBox3.Text; this.label5.Text = "正在加密,请稍等..."; if ((EncryptFile(this.textBox1.Text, this.textBox2.Text, key))) ...{ this.label5.Text = "加密完成。"; MessageBox.Show("加密成功!"); this.textBox1.Text = ""; this.textBox2.Text = ""; this.textBox3.Text = ""; } this.label5.Visible = false; } else ...{ MessageBox.Show("请输入完整数据!"); } } #endregion "解密#region "解密 private void button4_Click(object sender, EventArgs e) ...{ key = this.textBox3.Text; try ...{ if (this.textBox1.Text != "" && this.textBox2.Text != "" && this.textBox3.Text != "") ...{ this.label5.Text = "正在解密,请稍等..."; if (DecryptFile(this.textBox1.Text, this.textBox2.Text, key)) ...{ this.label5.Text = "解密完成。"; MessageBox.Show("解密成功!"); this.textBox1.Text = ""; this.textBox2.Text = ""; this.textBox3.Text = ""; } this.label5.Visible = false; } else ...{ MessageBox.Show("请输入完整数据!"); } } catch (Exception) ...{ MessageBox.Show("解密失败!请确认是否输入了正确的安全密匙."); this.textBox3.Text = ""; } } #endregion }}