博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何加密和解密文件
阅读量:6939 次
发布时间:2019-06-27

本文共 3872 字,大约阅读时间需要 12 分钟。

using System;using System.IO;using System.Security;using System.Security.Cryptography;using System.Runtime.InteropServices;using System.Text;namespace CSEncryptDecrypt{   class Class1   {      //  Call this function to remove the key from memory after use for security      [System.Runtime.InteropServices.DllImport("KERNEL32.DLL", EntryPoint="RtlZeroMemory")]      public static extern bool ZeroMemory(IntPtr Destination, int Length);		      // Function to Generate a 64 bits Key.      static string GenerateKey()       {         // Create an instance of Symetric Algorithm. Key and IV is generated automatically.         DESCryptoServiceProvider desCrypto =(DESCryptoServiceProvider)DESCryptoServiceProvider.Create();         // Use the Automatically generated key for Encryption.          return ASCIIEncoding.ASCII.GetString(desCrypto.Key);      }      static void EncryptFile(string sInputFilename,         string sOutputFilename,          string sKey)       {         FileStream fsInput = new FileStream(sInputFilename,             FileMode.Open,             FileAccess.Read);         FileStream fsEncrypted = new FileStream(sOutputFilename,             FileMode.Create,             FileAccess.Write);         DESCryptoServiceProvider DES = new DESCryptoServiceProvider();         DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);         DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);         ICryptoTransform desencrypt = DES.CreateEncryptor();         CryptoStream cryptostream = new CryptoStream(fsEncrypted,             desencrypt,             CryptoStreamMode.Write);          byte[] bytearrayinput = new byte[fsInput.Length];         fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);         cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);         cryptostream.Close();         fsInput.Close();         fsEncrypted.Close();      }      static void DecryptFile(string sInputFilename,          string sOutputFilename,         string sKey)      {         DESCryptoServiceProvider DES = new DESCryptoServiceProvider();         //A 64 bit key and IV is required for this provider.         //Set secret key For DES algorithm.         DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);         //Set initialization vector.         DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);         //Create a file stream to read the encrypted file back.         FileStream fsread = new FileStream(sInputFilename,             FileMode.Open,             FileAccess.Read);         //Create a DES decryptor from the DES instance.         ICryptoTransform desdecrypt = DES.CreateDecryptor();         //Create crypto stream set to read and do a          //DES decryption transform on incoming bytes.         CryptoStream cryptostreamDecr = new CryptoStream(fsread,             desdecrypt,            CryptoStreamMode.Read);         //Print the contents of the decrypted file.         StreamWriter fsDecrypted = new StreamWriter(sOutputFilename);         fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());         fsDecrypted.Flush();         fsDecrypted.Close();      }       static void Main()      {         // Must be 64 bits, 8 bytes.         // Distribute this key to the user who will decrypt this file.         string sSecretKey;                  // Get the Key for the file to Encrypt.         sSecretKey = GenerateKey();         // For additional security Pin the key.         GCHandle gch = GCHandle.Alloc( sSecretKey,GCHandleType.Pinned );                  // Encrypt the file.                 EncryptFile(@"C:\MyData.txt",             @"C:\Encrypted.txt",             sSecretKey);         // Decrypt the file.         DecryptFile(@"C:\Encrypted.txt",             @"C:\Decrypted.txt",             sSecretKey);         // Remove the Key from memory.          ZeroMemory(gch.AddrOfPinnedObject(), sSecretKey.Length * 2);         gch.Free();      }   }}

转载地址:http://ciljl.baihongyu.com/

你可能感兴趣的文章
常见的网页布局二
查看>>
WINFORM中treeview 节点显示不全
查看>>
测试之美---测试员的心思你不懂
查看>>
Apache中KeepAlive 配置
查看>>
删除桌面上有文件但提示项目不存在的方法
查看>>
Office Web Apps 错误
查看>>
SharePoint 2013 工作流之使用Visio设计篇
查看>>
C#程序使用SQLite数据库
查看>>
C#中关于@的用法
查看>>
车辆管理系统之搭建框架 添加必要的数据 安装svn(二)
查看>>
【Generate Parentheses】cpp
查看>>
设计一套网盘接口设计
查看>>
Hadoop(C#) 资料
查看>>
基础语法
查看>>
常用开发工具介绍
查看>>
C# 从注册表判断指定ocx控件是否已注册 以及获取它的注册路径
查看>>
html基础
查看>>
Numpy学习练习代码 ——(一)
查看>>
python的float函数不常用方法汇总
查看>>
第十周周总结
查看>>