欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-01-05 12:11:16
本文实例讲述了c#通过des加密算法加密大文件的方法。分享给大家供大家参考。具体实现方法如下: using system.collections; using...

本文实例讲述了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#程序设计有所帮助。