C#实现对文件进行加密解密的方法
作者:heishui 发布时间:2023-05-28 14:02:44
标签:C#,加密,解密
本文实例讲述了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();
}
}
希望本文所述对大家的C#程序设计有所帮助。
0
投稿
猜你喜欢
- Java.util.Collections类下有一个静态的shuffle()方法,如下:1)static void shuffle(List
- 如有错误,望指正;SpringBoot可以有三种方式定义初始化器,来为容器中增加自定义的对象,具体如下:1、定义在spring.factor
- 一.Worker Thread模式Worker的意思是工作的人,在Worker Thread模式中,工人线程Worker thread会逐个
- 配置文件请看上篇Java实现redis https://www.jb51.net/article/190922.htm下面测试redis的集
- 本文实例讲述了Java数组的定义、初始化、及二维数组用法。分享给大家供大家参考,具体如下:数组的定义1.数组是有序数据的集合,数组中的每个元
- 1.获取签名与模板进入阿里云平台,进入短信服务模块,在以下位置添加签名和模板(格式一定按照要求填写 审批的比较严格)2.编写模板与签名的枚举
- 这篇文章主要介绍了Java使用Collections工具类对List集合进行排序,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一
- 下面对Java反射的基础知识进行说明和总结:首先定义一个MyBase类,其中有私有字段,也有公有字段。同时也有公有方法和私有方法。MyBas
- 本文实例为大家分享了java实现仿射密码加密解密的具体代码,供大家参考,具体内容如下加密:将明文转化为对应的数字,如 ‘a'->
- 这篇会深化View拖拽实例,利用Flutter Animation、插值器以及AnimatedBuilder教大家实现带动画的抽屉效果。先来
- 1、jdbc1) 含义:JDBC是java语言连接数据库,Java Date Base Connectivity2) jdbc的本质:在编程
- 一.导入Netty依赖<dependency> <groupId>io.netty</group
- intellij idea是一款非常优秀的软件开发工具,它拥有这强大的插件体系,可以帮助开发者完成很多重量级的功能。今天,我们来学习一下如何
- 目录前言错误实例演示实现ApplicationContextAware接口lookup methodlookup method签名总结前言看
- 呐呐呐,做Java呢,最重要是要把自己的“作品” 部署到公网上去啦。特别是初学者,需要向面试官证明自己会什么,这个真的就很重要啦,空口无凭,
- 一:什么是classpath?classpath指的就是 *.java文件,资源文件等编译后存放的位置,对于maven项目就是指 targe
- 本文实例为大家分享了Android绝对布局AbsoluteLayout的具体代码,供大家参考,具体内容如下1>AbsoluteLayo
- 依赖<dependency> <groupId>io.springfox</groupId> <a
- 前言《黄金矿工》游戏是一个经典的抓金子小游戏,它可以锻炼人的反应能力。。该游戏中,可以通过“挖矿”获
- java中引用数据类型有哪些Java中有俩种数据类型,其中主要有8中基本数据类型和引用数据类型,除了8中基本数据类型以外都是引用数据类型,8