C#加密解密,请各位帮忙!

 

请各位大侠帮忙看看!!!
在运行解密时,报错:处理发生错误!原因:System.ApplicationException: 不正确的数据。
//加密
protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            if (FileUpload1.PostedFile.FileName == "")
            {
                Label1.Text = "要上传的文件不允许为空!";
                return;
            }
            else
            {
                string filename = "加密后文本.txt";
                string filepath = FileUpload1.PostedFile.FileName;
                string serverpath = Server.MapPath("File/") + filename;
                EncryptFile(filepath,serverpath);
            }
        }
        catch (Exception error)
        {
            Label1.Text = "处理发生错误!原因:" + error.ToString();
        }
    }
public void EncryptFile(string filePath, string outPath)
    {
        bool isExist = File.Exists(filePath);
        if (isExist)//如果存在
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();

            //byte[] ivb = Encoding.ASCII.GetBytes(this.iv);
            //byte[] keyb = Encoding.ASCII.GetBytes(this.EncryptKey);
            //byte[] ivb = System.Text.Encoding.Default.GetBytes(des.IV);
            byte[] ivb = des.IV;
            byte[] keyb =des.Key;
            //得到要加密文件的字节流
            FileStream fin = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            StreamReader reader = new StreamReader(fin,System.Text.Encoding.Default);
            string dataStr = reader.ReadToEnd();
            //byte[] toEncrypt = this.EncodingMode.GetBytes(dataStr);
            byte[] toEncrypt = System.Text.Encoding.Default.GetBytes(dataStr);
            fin.Close();

            FileStream fout = new FileStream(outPath, FileMode.Create, FileAccess.Write);
            ICryptoTransform encryptor = des.CreateEncryptor(keyb, ivb);
            CryptoStream csEncrypt = new CryptoStream(fout, encryptor, CryptoStreamMode.Write);
            try
            {
                //加密得到的文件字节流
                csEncrypt.Write(toEncrypt, 0, toEncrypt.Length);
                csEncrypt.FlushFinalBlock();
            }
            catch (Exception err)
            {
                throw new ApplicationException(err.Message);
            }
            finally
            {
                try
                {
                    fout.Close();
                    csEncrypt.Close();
                }
                catch
                {
                    ;
                }
            }
        }
        else
        {
            throw new FileNotFoundException("没有找到指定的文件 ");
        }
    }


//解密
protected void Button2_Click(object sender, EventArgs e)
    {
        try
        {
            if (FileUpload2.PostedFile.FileName == "")
            {
                Label1.Text = "要解密的文件不允许为空!";
                return;
            }
            else
            {
                string filename = "解密后文本.txt";
                string filepath = FileUpload2.PostedFile.FileName;
                string serverpath = Server.MapPath("File/") + filename;
                DecryptFile(filepath, serverpath);
            }
        }
        catch (Exception error)
        {
            Label1.Text = "处理发生错误!原因:" + error.ToString();
        }
    }
public void DecryptFile(string filePath, string outPath)
    {
        bool isExist = File.Exists(filePath);
        if (isExist)//如果存在
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();

            byte[] ivb = des.IV;
            byte[] keyb = des.Key;

            FileInfo file = new FileInfo(filePath);
            byte[] deCrypted = new byte[file.Length];
            //得到要解密文件的字节流
            FileStream fin = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            //解密文件
            try
            {
                ICryptoTransform decryptor = des.CreateDecryptor(keyb, ivb);
                CryptoStream csDecrypt = new CryptoStream(fin, decryptor, CryptoStreamMode.Read);
                csDecrypt.Read(deCrypted, 0, deCrypted.Length);
            }
            catch (Exception err)
            {
                throw new ApplicationException(err.Message);
            }
            finally
            {
                try
                {
                    fin.Close();
                }
                catch { ;}
            }
            FileStream fout = new FileStream(outPath, FileMode.Create, FileAccess.Write);
            fout.Write(deCrypted, 0, deCrypted.Length);
            fout.Close();
        }
        else
        {
            throw new FileNotFoundException("指定的解密文件没有找到 ");
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值