c#通过DES加密算法加密大文件的方法

所属分类: 软件编程 / C#教程 阅读数: 69
收藏 0 赞 0 分享

本文实例讲述了c#通过DES加密算法加密大文件的方法。分享给大家供大家参考。具体实现方法如下:

using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.IO;
using System.Security.Cryptography;
public partial class Default2 : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
  }
  private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };//自定义密匙
  private string filePathA;//储存文件路径
  private string filePathB;//储存文件复制后的路径
  /// <summary>
  /// 文件加密
  /// </summary>
  /// <param name="inFile">文件储存路径</param>
  /// <param name="outFile">储存文件复制的路径</param>
  /// <param name="encryptKey"></param>
  /// <returns></returns>
  public bool EncryptDES(string inFile, string outFile, string encryptKey)
  {
   byte[] rgb = Keys;
   try
   {
    byte[] rgbKeys = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
    FileStream inFs = new FileStream(inFile, FileMode.Open, FileAccess.Read);//读入流
    FileStream outFs = new FileStream(outFile, FileMode.OpenOrCreate, FileAccess.Write);// 等待写入流
    outFs.SetLength(0);//帮助读写的变量
    byte[] byteIn = new byte[100];//放临时读入的流
    long readLen = 0;//读入流的长度
    long totalLen = inFs.Length;//读入流的总长度
    int everylen=0;//每次读入流的长度
    DES des = new DESCryptoServiceProvider();//将inFile加密后放到outFile
    CryptoStream encStream = new CryptoStream(outFs, des.CreateEncryptor(rgb, rgbKeys), CryptoStreamMode.Write);
    while (readLen < totalLen)
    {
     everylen = inFs.Read(byteIn, 0, 100);
     encStream.Write(byteIn, 0, everylen);
     readLen = readLen + everylen;
    }
    encStream.Close();
    inFs.Close();
    outFs.Close();
    return true;//加密成功
   }
   catch (Exception ex)
   {
    Response.Write(ex.Message.ToString());
    return false;//加密失败
   }
  }

  public bool DecryptDES(string inFile, string outFile, string encryptKey)
  {
   byte[] rgb = Keys;
   try
   {
    byte[] rgbKeys = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
    FileStream inFs = new FileStream(inFile, FileMode.Open, FileAccess.Read);//读入流
    FileStream outFs = new FileStream(outFile, FileMode.OpenOrCreate, FileAccess.Write);// 等待写入流
    outFs.SetLength(0);//帮助读写的变量
    byte[] byteIn = new byte[100];//放临时读入的流
    long readLen = 0;//读入流的长度
    long totalLen = inFs.Length;//读入流的总长度
    int everylen=0;//每次读入流的长度
    DES des = new DESCryptoServiceProvider();//将inFile加密后放到outFile
    CryptoStream encStream = new CryptoStream(outFs, des.CreateDecryptor(rgb, rgbKeys), CryptoStreamMode.Write);
    while (readLen < totalLen)
    {
     everylen = inFs.Read(byteIn, 0, 100);
     encStream.Write(byteIn, 0, everylen);
     readLen = readLen + everylen;
    }
    encStream.Close();
    inFs.Close();
    outFs.Close();
    return true;//加密成功
   }
   catch (Exception ex)
   {
    Response.Write(ex.Message.ToString());
    return false;//加密失败
   }
  }
  /// <summary>
  /// 拷贝文件
  /// </summary>
  public void copyFile()
  {
   filePathA = this.fei.PostedFile.FileName;//获取文件全部路径
   string fileName = this.fei.FileName;
   string path = System.IO.Path.GetDirectoryName(filePathA);
   filePathB = path + "\\1" + fileName;//重新设置文件名
   File.Copy(filePathA, filePathB);
  }
  protected void btnOK_Click(object sender, EventArgs e)
  {
   copyFile();
   if (EncryptDES(filePathB, filePathA, "mingrisoft"))
   {
    RegisterStartupScript("false", "<script>alert('加密成功!\\n');</script>");
   }
   else
   {
    RegisterStartupScript("false", "<script>alert('失败成功!\\n');</script>");
   }
   File.Delete(filePathB);
  }
  protected void btnCancel_Click(object sender, EventArgs e)
  {
   copyFile();
   if (DecryptDES(filePathB, filePathA, "mingrisoft"))
   {
    RegisterStartupScript("false", "<script>alert('加密成功!\\n');</script>");
   }
   else
   {
    RegisterStartupScript("false", "<script>alert('失败成功!\\n');</script>");
   }
   File.Delete(filePathB);
  }
}

希望本文所述对大家的C#程序设计有所帮助。

更多精彩内容其他人还在看

C#抽象类与抽象方法详解

这篇文章主要为大家详细介绍了C#抽象类与抽象方法的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

C#代码实现扑克牌排序的几种方式

今天小编就为大家分享一篇关于C#代码实现扑克牌排序,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

C#泛型概念的简介与泛型的使用

今天小编就为大家分享一篇关于C#泛型概念的简介与泛型的使用,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

C# 7.0 使用下划线忽略使用的变量的原因分析

这篇文章主要介绍了C# 7.0 使用下划线忽略使用的变量的原因浅析,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C# 中使用正则表达式匹配字符的含义

正则表达式的作用用来描述字符串的特征。本文重点给大家介绍C# 中使用正则表达式匹配字符的含义,非常不错,具有一定的参考借鉴价值,需要的朋友参考下吧
收藏 0 赞 0 分享

C# Dictionary和SortedDictionary的简介

今天小编就为大家分享一篇关于C# Dictionary和SortedDictionary的简介,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

C#中SQL Command的基本用法

今天小编就为大家分享一篇关于C#中SQL Command的基本用法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

C#使用SQL DataReader访问数据的优点和实例

今天小编就为大家分享一篇关于C#使用SQL DataReader访问数据的优点和实例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

C#使用SQL Dataset数据集代码实例

今天小编就为大家分享一篇关于的文章,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

C#使用SQL DataAdapter数据适配代码实例

今天小编就为大家分享一篇关于C#使用SQL DataAdapter数据适配代码实例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享
查看更多