• 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#对文件进行加密解密代码

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

通过本文主要向大家介绍了马桶c的个人空间,c语言,欲情 c max,维生素c,crh2c等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

加密代码

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>

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

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

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

  • C#利用ReportViewer生成报表
  • C#基于正则去掉注释的方法示例
  • C#中new的用法及与override的区别分析
  • C#实现两个richtextbox控件滚动条同步滚动的简单方法
  • C# for循环的经典案例集锦
  • C#操作word的方法示例
  • C#使用WebClient登录网站并抓取登录后的网页信息实现方法
  • C# WinForm制作异形窗体与控件的方法
  • C#实现Excel表数据导入Sql Server数据库中的方法
  • C#使用NPOI上传excel

相关文章

  • 2017-05-28C#基础之泛型委托实例教程
  • 2017-05-28C#使用NOPI库实现导入Excel文档
  • 2017-05-28C# 面向对象的基本原则
  • 2017-05-28c#固定长度的随机字符串例子
  • 2017-05-28C# dynamic关键字的使用方法
  • 2017-05-28C#实现在启动目录创建快捷方式的方法
  • 2017-05-28浅谈关于C#的垃圾回收机制
  • 2017-05-28C#约瑟夫问题解决方法
  • 2017-05-28C#简单实现在网页上发邮件的案例
  • 2017-05-28C#非矩形窗体实现方法

文章分类

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

最近更新的内容

    • WinForm项目开发中Excel用法实例解析
    • C#算法设计之关于1000瓶水的问题
    • WinForm实现为ComboBox绑定数据源并提供下拉提示功能
    • winform拦截关闭按钮触发的事件示例
    • C#画笔Pen绘制曲线的方法
    • Jquery+Ajax+Json+存储过程实现高效分页
    • C#操作数据库总结(vs2005+sql2005)
    • c#使用反射调用类型成员示例
    • C#遍历得到checkboxlist选中值和设置选中项的代码
    • C# linq查询之动态OrderBy用法实例

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

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