时间:2021-05-20
加密代码
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(); } }以上所述就是本文的全部内容了,希望大家能够喜欢。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了C#实现对文件进行加密解密的方法。分享给大家供大家参考。具体如下:usingSystem;usingSystem.IO;usingSystem.S
这个C#类是一个基于Base64的加密和解密类,用户可以可以使用默认的秘钥进行加密、解密,也可以自己设定秘钥进行加密和解密,非常实用代码一:非常精简的代码///
本文实例讲述了C#字符串加密解密方法。分享给大家供大家参考。具体如下:复制代码代码如下:#region加密解密staticstringencryptKey="O
本文实例为大家分享了C#使用RSA加密解密文件的具体代码,供大家参考,具体内容如下加密代码://加密代码,注意会覆盖原文件,里面有我的公钥,你要用时记得覆盖我的
Spire.Cloud.SDKfor.NET提供了接口PdfSecurityApi可用于加密、解密PDF文档。本文将通过C#代码演示具体加密及解密方法。使用工具