C#对文件进行加密解密代码

 更新时间:2015年07月08日 11:05:08   投稿:hebedich  
本文给大家分享的是使用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();
  
  }
  
}

以上所述就是本文的全部内容了,希望大家能够喜欢。

相关文章

  • 判断图片-判断位图是否是黑白图片的方法

    判断图片-判断位图是否是黑白图片的方法

    近来工作需要判断图片否是是彩色的,由于是十万张以上的大批量理处,所以通过序程来动自判断。
    2013-05-05
  • C#实现将程序运行信息写入日志的方法

    C#实现将程序运行信息写入日志的方法

    这篇文章主要介绍了C#实现将程序运行信息写入日志的方法,可实现将程序运行信息写入日志并存储在Debug目录下的"/Log/PRG"下的功能,涉及C#针对日志的相关写入技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-08-08
  • C#实现绑定Combobox的方法

    C#实现绑定Combobox的方法

    这篇文章主要介绍了C#实现绑定Combobox的方法,涉及Combobox参数设置的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-08-08
  • WPF TextBox和PasswordBox添加水印

    WPF TextBox和PasswordBox添加水印

    这篇文章主要为大家详细介绍了WPF TextBox和PasswordBox添加水印的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-11-11
  • c# List和Dictionary常用的操作

    c# List和Dictionary常用的操作

    这篇文章主要介绍了c# List和Dictionary常用的操作,帮助大家更好的理解和学习使用c#,感兴趣的朋友可以了解下
    2021-04-04
  • Unity实现相机截图功能

    Unity实现相机截图功能

    这篇文章主要为大家详细介绍了Unity实现相机截图功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-04-04
  • Unity Shader实现裁切效果

    Unity Shader实现裁切效果

    这篇文章主要为大家详细介绍了Unity Shader实现裁切效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-04-04
  • DevExpress TreeList 常见问题解决方法

    DevExpress TreeList 常见问题解决方法

    这篇文章主要介绍了DevExpress TreeList 常见问题解决方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2016-12-12
  • Unity实现模型点击事件的方法

    Unity实现模型点击事件的方法

    这篇文章主要介绍了Unity实现模型点击事件的方法,本文通过多种方法给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-05-05
  • C#反射调用dll文件中的方法操作泛型与属性字段

    C#反射调用dll文件中的方法操作泛型与属性字段

    这篇文章介绍了C#反射调用dll文件中的方法操作泛型与属性字段,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-05-05

最新评论