• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >C#教程 > C#实现对文件进行加密解密的方法

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

作者:heishui 字体:[增加 减小] 来源:互联网 时间:2017-05-28

heishui 通过本文主要向大家介绍了c#实现文件加密,c#实现文件上传,c#实现文件传输,c#实现文件下载,c#实现文件下载实例等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文实例讲述了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();
 }
}
</div>

解密代码如下:

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

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

</div>
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • C#实现rar压缩与解压缩文件的方法
  • C#实现对文件进行加密解密的方法
  • C#实现彻底删除文件的方法
  • C#文件加密方法汇总
  • C#为配置文件加密的实现方法

相关文章

  • 2017-05-28C#中Array与ArrayList用法及转换的方法
  • 2017-05-28C# 动画窗体(AnimateWindow)的小例子
  • 2017-05-28visio二次开发--判断文档是否已发生变化(变化就加星号*)
  • 2017-05-28C#实现读取被进程占用的文件实现方法
  • 2017-05-28C#采用Winform实现类似Android的Listener
  • 2017-05-28C#提高编程能力的50个要点总结
  • 2017-05-28C#把整个文件内容读入字符串变量的方法
  • 2017-05-28C#中方法的详细介绍
  • 2017-05-28C#窗体显示留言时间的方法
  • 2017-05-28C#学习基础概念二十五问第1/4页

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • C#获取字符串后几位数的方法
    • C#:foreach与yield语句的介绍
    • C#中矩阵运算方法实例分析
    • C#画图之饼图折线图的实现方法
    • c#获取gridview的值代码分享
    • c#入门之循环语句使用详解(for循环、do/while)
    • C#获取关键字附近文字算法实例
    • c#中executereader执行查询示例分享
    • C#中for循环、while循环循环执行的方法
    • 协定需要会话,但是绑定“BasicHttpBinding”不支持它或者因配置不正确而无法支持它

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有