C#如何加密,解密文件

[C#] 纯文本查看 复制代码using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.IO;using System.Linq;using System.Runtime.InteropServices;using System.Security.Cryptography;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace 文件加密(求助){ public partial class Form1 : Form { public Form1() { InitializeComponent(); } /// <summary> /// 文件加密 /// </summary> public class FileSecretHelper { /// <summary> /// 密钥,这个密码可以随便指定 /// </summary> public static string sSecretKey = "kn666"; /// <summary> /// 调用该函数从内存中删除的Key后使用 /// </summary> [DllImport("KERNEL32.DLL", EntryPoint = "RtlZeroMemory")] public static extern bool ZeroMemory(IntPtr Destination, int Length); /// <summary> /// 生成一个64位的密钥 /// </summary> /// <returns>string</returns> public static string GenerateKey() { //创建对称算法的一个实例。自动生成的密钥和IV。 DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create(); // 使用自动生成的密钥进行加密。 return ASCIIEncoding.ASCII.GetString(desCrypto.Key); } /// <summary> /// 加密文件 /// </summary> /// <param name="sInputFilename">待加密的文件的完整路径</param> /// <param name="sOutputFilename">加密后的文件的完整路径</param> /// -------- 疑惑:如何让button1点击后加密 "D:\t\IP地址定位.exe"并删除源文件,在源目录生成IP地址定位.kn------ /// "D:\t\IP地址定位.exe" /// "D:\t\IP地址定位.kn" public static void EncryptFile(string sInputFilename, string sOutputFilename) { 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(sSecretKey); DES.IV = ASCIIEncoding.ASCII.GetBytes(sSecretKey); 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.Flush(); fsInput.Flush(); fsEncrypted.Flush(); cryptostream.Close(); fsInput.Close(); fsEncrypted.Close(); } /// <summary> /// 解密文件 /// </summary> /// <param name="sInputFilename">待解密的文件的完整路径</param> /// <param name="sOutputFilename">解密后的文件的完整路径</param> /// 疑惑:如何让button2点击后解密 "D:\t\IP地址定位.kn"并删除源文件,在源目录生成“IP地址定位.exe public static void DecryptFile(string sInputFilename, string sOutputFilename) { DESCryptoServiceProvider DES = new DESCryptoServiceProvider(); DES.Key = ASCIIEncoding.ASCII.GetBytes(sSecretKey); DES.IV = ASCIIEncoding.ASCII.GetBytes(sSecretKey); FileStream fsread = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read); ICryptoTransform desdecrypt = DES.CreateDecryptor(); CryptoStream cryptostreamDecr = new CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read); StreamWriter fsDecrypted = new StreamWriter(sOutputFilename); fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd()); fsDecrypted.Flush(); fsDecrypted.Close(); } } private void button1_Click(object sender, EventArgs e) { } } }}
--以上为大部分核心源码--
求助:
1.如何实现自动生成一个密钥保存在C:/下,在解密时导入保存的密钥才能解密【密钥暂定为pass.kps】2.程序大致运行过程:
选择要加密的文件/目录 —— 点击加密,在C:/下生成唯一密钥文件 —— 解密时需导入密钥文件 【麻烦大佬指点下彩色部分】


求大佬帮忙做个DEMO(可以参考下上面的核心代码),
跪谢大佬!!


上一篇:求猿来入此springboot脚手架的资源。。求大佬下一篇:求一篇《农业工程学院》投稿模板,或则帮代下一下百度文档,谢谢了

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/d9d77834173794b887ee65fe2531e82d.html