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();
  
  }
  
}

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

相关文章

  • TortoiseSVN使用教程

    TortoiseSVN使用教程

    TortoiseSVN 是 Subversion 版本控制系统的一个免费开源客户端,可以超越时间的管理文件和目录。本文给大家介绍TortoiseSVN使用教程,对tortoisesvn使用相关知识感兴趣的朋友一起学习吧
    2015-12-12
  • C#实现简单合并word文档的方法

    C#实现简单合并word文档的方法

    这篇文章主要介绍了C#实现简单合并word文档的方法,涉及C#针对word文档的读取、插入、保存等技巧,非常具有实用价值,需要的朋友可以参考下
    2015-09-09
  • WinForm使用正则表达式提取内容的方法示例

    WinForm使用正则表达式提取内容的方法示例

    这篇文章主要介绍了WinForm使用正则表达式提取内容的方法,结合实例形式分析了WinForm基于正则匹配获取指定内容的相关操作技巧,需要的朋友可以参考下
    2017-05-05
  • C#8.0默认接口实现的详细实例

    C#8.0默认接口实现的详细实例

    Microsoft使用C#8.0发布了许多新功能,他们引入的主要功能之一是默认接口方法。这篇文章主要给大家介绍了关于C#8.0默认接口实现的相关资料,需要的朋友可以参考下
    2021-05-05
  • C#连接数据库的方法

    C#连接数据库的方法

    ASP.NET连接数据库的技术叫ADO.NET,它是用来向数据库提交sql语句的一堆类。这里连接的是Sql Server 2008数据库,其他数据库用法差不多,就是调用的类名不一样
    2015-11-11
  • UnityRTS实现相机移动缩放功能

    UnityRTS实现相机移动缩放功能

    这篇文章主要为大家详细介绍了UnityRTS实现相机的移动缩放功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-03-03
  • C# 根据字符串生成二维码的实例代码

    C# 根据字符串生成二维码的实例代码

    这篇文章主要介绍了C# 根据字符串生成二维码的实例,文中示例代码非常详细,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-07-07
  • C#图像亮度调整的方法

    C#图像亮度调整的方法

    这篇文章主要介绍了C#图像亮度调整的方法,涉及C#操作图像亮度的相关技巧,需要的朋友可以参考下
    2015-04-04
  • 基于C#实现哈夫曼树算法

    基于C#实现哈夫曼树算法

    哈夫曼树又称最优二叉树,也就是带权路径最短的树,对于哈夫曼树,我想大家对它是非常的熟悉,使用下面我们就来学习一下如何通过C#实现哈夫曼树算法吧
    2023-11-11
  • 详解C# Lazy Loading(延迟加载)

    详解C# Lazy Loading(延迟加载)

    这篇文章主要介绍了C# Lazy Loading(延迟加载)的相关资料,帮助大家更好的理解和学习使用c#,感兴趣的朋友可以了解下
    2021-04-04

最新评论