C#实现对文件进行加密解密的方法

 更新时间:2015年04月08日 12:25:14   作者:heishui  
这篇文章主要介绍了C#实现对文件进行加密解密的方法,涉及C#加密与解密的技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了C#实现对文件进行加密解密的方法。分享给大家供大家参考。具体如下:

using System;
using System.IO;
using System.Security.Cryptography;
public class Example19_9
{
 public static void Main()
 {
  // Create a new file to work with
  FileStream fsOut = File.Create(@"c:\temp\encrypted.txt");
  // Create a new crypto provider
  TripleDESCryptoServiceProvider tdes =
   new TripleDESCryptoServiceProvider();
  // Create a cryptostream to encrypt to the filestream
  CryptoStream cs = new CryptoStream(fsOut, tdes.CreateEncryptor(),
   CryptoStreamMode.Write);
  // Create a StreamWriter to format the output
  StreamWriter sw = new StreamWriter(cs);
  // And write some data
  sw.WriteLine("'Twas brillig, and the slithy toves");
  sw.WriteLine("Did gyre and gimble in the wabe.");
  sw.Flush();
  sw.Close();
  // save the key and IV for future use
  FileStream fsKeyOut = File.Create(@"c:\\temp\encrypted.key");
  // use a BinaryWriter to write formatted data to the file
  BinaryWriter bw = new BinaryWriter(fsKeyOut);
  // write data to the file
  bw.Write( tdes.Key );
  bw.Write( tdes.IV );
  // flush and close
  bw.Flush();
  bw.Close();
 }
}

解密代码如下:

using System;
using System.IO;
using System.Security.Cryptography;
public class Example19_10
{
 public static void Main()
 {
  // Create a new crypto provider
  TripleDESCryptoServiceProvider tdes =
   new TripleDESCryptoServiceProvider();
  // open the file containing the key and IV
  FileStream fsKeyIn = File.OpenRead(@"c:\temp\encrypted.key");
  // use a BinaryReader to read formatted data from the file
  BinaryReader br = new BinaryReader(fsKeyIn);
  // read data from the file and close it
  tdes.Key = br.ReadBytes(24);
  tdes.IV = br.ReadBytes(8);
  // Open the encrypted file
  FileStream fsIn = File.OpenRead(@"c:\\temp\\encrypted.txt");
  // Create a cryptostream to decrypt from the filestream
  CryptoStream cs = new CryptoStream(fsIn, tdes.CreateDecryptor(),
   CryptoStreamMode.Read);
  // Create a StreamReader to format the input
  StreamReader sr = new StreamReader(cs);
  // And decrypt the data
  Console.WriteLine(sr.ReadToEnd());
  sr.Close();
 }
}

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

相关文章

  • 使用GetInvalidFileNameCharts生成文件名

    使用GetInvalidFileNameCharts生成文件名

    这篇文章主要介绍了一个很实用的函数Path.GetInvalidFileNameCharts(),他可以很方便的生成一个有效的文件名称
    2014-01-01
  • C#中的委托使用

    C#中的委托使用

    委托是C#中新加入的一个类型,可以把它想作一个和Class类似的一种类型,和使用类相似,使用一个委托时,需要两个步骤,首先你要定义一个委托,就像是定义一个类一样;然后,你可以创建一个或多个该委托的实例。
    2016-07-07
  • C#中的HttpWebRequest类用法详解

    C#中的HttpWebRequest类用法详解

    本文详细讲解了C#中的HttpWebRequest类的用法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-01-01
  • C# Pointer指针应用实例简述

    C# Pointer指针应用实例简述

    这篇文章主要介绍了C# Pointer指针应用,对初学者很有借鉴学习价值,需要的朋友可以参考下
    2014-08-08
  • c#设计模式 适配器模式详细介绍

    c#设计模式 适配器模式详细介绍

    结构模式(Structural Pattern)描述如何将类或者对象结合在一起形成更大的结构。结构模式描述两种不同的东西:类与类的实例。根据这一点,结构模式可以分为类的结构模式和对象的结构模式
    2012-10-10
  • C#中if语句使用概述

    C#中if语句使用概述

    这里介绍C#使用if语句,C#使用if语句中的表达式必须放在一对圆括号中。除此之外,表达式必须是布尔表达式
    2014-03-03
  • Http上传与Ftp上传的区别详解

    Http上传与Ftp上传的区别详解

    本篇文章是对Http上传与Ftp上传的区别进行了详细的分析介绍,需要的朋友参考下
    2013-05-05
  • 详解如何在ASP.NET Core配置请求超时中间件

    详解如何在ASP.NET Core配置请求超时中间件

    本文参考官方文档,为大家详细介绍如何使用Asp.net core 8.0 的最小API 模板项目,配置超时中间件,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下
    2024-01-01
  • c#制作屏幕保护程序步骤(字幕屏保)

    c#制作屏幕保护程序步骤(字幕屏保)

    本文介绍使用C#制作屏幕保护的方法,这个屏幕保护就是仿效视窗系统自带的字幕屏保。下面是用C#如何编写屏幕保护的整个过程
    2014-01-01

最新评论