C#对文件进行加密解密代码
作者:hebedich 时间:2023-03-22 12:17:18
加密代码
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#,加密解密
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
解决springcloud 配置gateway 出现错误的问题
2023-12-04 00:45:49
![](https://img.aspxhome.com/file/2023/7/79957_0s.png)
英雄联盟辅助lol挂机不被踢的方法(lol挂机脚本)
2022-03-15 12:57:59
Android仿支付宝微信支付密码界面弹窗封装dialog
2021-10-24 13:13:31
![](https://img.aspxhome.com/file/2023/1/90131_0s.jpg)
Java 使用getClass().getResourceAsStream()方法获取资源
2023-07-23 08:59:56
常见JavaWeb安全问题和解决方案
2023-11-09 01:02:17
Java中Druid连接池连接超时获取不到连接的解决
2022-09-15 04:49:59
![](https://img.aspxhome.com/file/2023/4/63004_0s.png)
Android实现拍照添加时间水印
2023-10-02 14:23:51
![](https://img.aspxhome.com/file/2023/5/130345_0s.jpg)
jenkins构建go及java项目的方法
2022-09-26 12:39:01
![](https://img.aspxhome.com/file/2023/0/128670_0s.png)
Spring应用抛出NoUniqueBeanDefinitionException异常的解决方案
2023-11-25 07:36:19
Android编程实现canvas绘制饼状统计图功能示例【自动适应条目数量与大小】
2023-05-10 23:27:41
![](https://img.aspxhome.com/file/2023/9/113299_0s.jpg)
SpringBoot遇到的坑@Qualifier报红的解决
2022-06-03 07:49:55
![](https://img.aspxhome.com/file/2023/5/66965_0s.png)
ThreadLocal数据存储结构原理解析
2023-04-27 19:54:55
![](https://img.aspxhome.com/file/2023/4/128234_0s.png)
Android数据存储几种方式讲解
2023-09-25 19:00:35
![](https://img.aspxhome.com/file/2023/7/86747_0s.png)
Java多线程事务管理的实现
2023-07-26 22:58:20
SpringBoot使用GraphQL开发Web API实现方案示例讲解
2023-05-17 16:50:22
![](https://img.aspxhome.com/file/2023/8/85418_0s.png)
android,不显示标题的方法小例子
2023-07-10 17:14:36
java 读取网页内容的实例详解
2023-11-27 22:38:11
Android仿考拉全局滑动返回及联动效果的实现方法
2021-06-01 03:41:03
![](https://img.aspxhome.com/file/2023/3/126763_0s.gif)
java使用链表实现约瑟夫环
2022-03-21 18:59:35
Java设计模式之java组合模式详解
2022-05-02 07:52:38
![](https://img.aspxhome.com/file/2023/2/115972_0s.png)