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.Cryptography; namespace 文件加密和解秘 

    public partial class Form1 : Form 
    { 
        public Form1() 
        { 
            InitializeComponent(); 
        } 
        private void Form1_Load(object sender, EventArgs e) 
        { 
            this.radioBtn_Encryption.Checked = true; 
        }         private void radioBtn_Encryption_CheckedChanged(object sender, EventArgs e) 
        { 
            this.label1.Text = "选择要加密的文件:"; 
            this.label2.Text = "选择加密后的文件路径:"; 
            this.label3.Text = "请输入加密密码:"; 
        }         private void radioBtn_Decoding_CheckedChanged(object sender, EventArgs e) 
        { 
            this.label1.Text = "选择要解密的文件:"; 
            this.label2.Text = "选择解密后的文件路径:"; 
            this.label3.Text = "请输入解密密码:"; 
        }         private void btn_Browse1_Click(object sender, EventArgs e) 
        { 
            openFileDialog1.ShowDialog(); 
            textBox1.Text = openFileDialog1.FileName; 
        }         private void btn_Browse2_Click(object sender, EventArgs e) 
        { 
            folderBrowserDialog1.ShowDialog(); 
            textBox2.Text = folderBrowserDialog1.SelectedPath; 
        }         private void btn_ok_Click(object sender, EventArgs e) 
        { 
            if (this.radioBtn_Encryption.Checked == true) 
            { 
                string Sourcefile = this.textBox1.Text; 
                string fileName = Path.GetFileNameWithoutExtension(Sourcefile); 
                string j = this.textBox2.Text; 
                string mfile = j + "\\" + fileName + ".dat"; 
                string strkey = this.textBox3.Text;                 if (this.textBox1.Text != "" && this.textBox2.Text != "" && this.textBox3.Text != "") 
                { 
                    encryption(strkey, Sourcefile, mfile); 
                } 
                else 
                    this.label4.Text = "加密失败!!请输入完整信息!!"; 
            } 
            else if (this.radioBtn_Decoding.Checked == true) 
            { 
                string mfile = this.textBox1.Text;                 string Sourcefile1 = this.textBox1.Text; 
                string ExtensionName = Path.GetExtension(Sourcefile1);                 string fileName = Path.GetFileNameWithoutExtension(mfile); 
                string j = this.textBox2.Text; 
                string Sourcefile = j + "\\" + fileName + ExtensionName; 
                string strkey = this.textBox3.Text; 
                if (this.textBox1.Text != "" && this.textBox2.Text != "" && this.textBox3.Text != "") 
                { 
                    decryption(strkey, mfile, Sourcefile); 
                } 
                else 
                    this.label4.Text = "解密失败!!请将信息输入完整!!!"; 
            } 
        }         public void encryption(string strkey, string Sourcefile, string mfile) 
        { 
            try 
            { 
                byte[] key = System.Text.Encoding.Default.GetBytes(strkey); 
                HMACMD5 hashObject = new HMACMD5(key); 
                byte[] hanshValue = hashObject.ComputeHash(key); 
                //配置对称密钥 
                //textBox2.Text = BitConverter.ToString(hanshValue); 
                TripleDESCryptoServiceProvider tDesCrypt = new TripleDESCryptoServiceProvider(); 
                tDesCrypt.Key = hanshValue; 
                tDesCrypt.IV = Encoding.Default.GetBytes("12345678"); 
                ICryptoTransform transform = tDesCrypt.CreateEncryptor(tDesCrypt.Key, tDesCrypt.IV); 
                //写进文件 
                FileStream fswrite = new FileStream(mfile, FileMode.Create); 
                CryptoStream cs = new CryptoStream(fswrite, transform, CryptoStreamMode.Write); 
                //打开文件 
                FileStream fsread = new FileStream(Sourcefile, FileMode.Open); 
                int data; 
                while ((data = fsread.ReadByte()) != -1) 
                    cs.WriteByte((byte)data); 
                cs.Close(); 
                fsread.Close(); 
                fswrite.Close(); 
                this.label4.Text = "加密成功!!!"; 
            } 
            catch (CryptographicException e) 
            { 
                MessageBox.Show(e.Message, "信息提示"); 
            } 
            catch (UnauthorizedAccessException e) 
            { 
                MessageBox.Show(e.Message, "信息提示"); 
            } 
        }         public void decryption(string strkey, string mfile, string Sourcefile) 
        { 
            try 
            { 
                FileStream fsopen = new FileStream(mfile, FileMode.Open); 
                byte[] key = System.Text.Encoding.Default.GetBytes(strkey); 
                //计算密钥的hash 
                HMACMD5 hashObject = new HMACMD5(key); 
                byte[] hanshValue = hashObject.ComputeHash(key);                 //配置对称密钥 
                TripleDESCryptoServiceProvider tDesCrypt = new TripleDESCryptoServiceProvider(); 
                tDesCrypt.Key = hanshValue;                 //textBox1.Text=BitConverter.ToString(hanshValue); 
                tDesCrypt.IV = Encoding.Default.GetBytes("12345678");                 ICryptoTransform transform = tDesCrypt.CreateDecryptor(tDesCrypt.Key, tDesCrypt.IV); 
                CryptoStream cs = new CryptoStream(fsopen, transform, CryptoStreamMode.Read); 
              
                //把解密后的结果写进文件 
                FileStream fswrite = new FileStream(Sourcefile, FileMode.OpenOrCreate); 
                int data; 
                while ((data = cs.ReadByte()) != -1) 
                    fswrite.WriteByte((byte)data); 
                fsopen.Close(); 
                fswrite.Close(); 
                cs.Close(); 
                this.label4.Text = "解密成功!!!"; 
            } 
            catch (CryptographicException e) 
            { 
                MessageBox.Show(e.Message, "信息提示");             } 
            catch (UnauthorizedAccessException e) 
            { 
                MessageBox.Show(e.Message, "信息提示");             }         } 
    } 
}这是我写的一个加密和解密的程序,我在解密的时候,我第一次把密码输错的话,再次输入正确密码的是时候还是抱密码错误!
那位高手能帮忙改改!!!!

解决方案 »

  1.   

                    if (this.textBox1.Text != "" && this.textBox2.Text != "" && this.textBox3.Text != "") 
                    { 
                        decryption(strkey, mfile, Sourcefile); 
                    } 
                    else 
                        this.label4.Text = "解密失败!!请将信息输入完整!!!"; 
    这里根本就是判断解密是否成功的。。而是判断文本框是否有输入。。