国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#實現對文件進行加密解密的方法

2020-01-24 02:01:22
字體:
來源:轉載
供稿:網友

本文實例講述了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#程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 海城市| 赣州市| 昌都县| 北碚区| 确山县| 南汇区| 衢州市| 阿城市| 新竹市| 旺苍县| 太和县| 保靖县| 儋州市| 遵义县| 泽州县| 丰镇市| 临邑县| 汝州市| 永康市| 东乡族自治县| 吉木萨尔县| 北川| 西昌市| 民县| 乡城县| 吉水县| 蒲城县| 红河县| 社会| 婺源县| 巩义市| 新巴尔虎左旗| 楚雄市| 道孚县| 凭祥市| 玉环县| 夏津县| 景德镇市| 文昌市| 绿春县| 泾川县|