C# · 12月 19, 2021

文件加密

代码:

1、AES加密类

using System; System.IO; System.Security.Cryptography; System.Text;namespace Utils{ /// <summary> /// AES加密解密 </summary> public class AES { #region 加密 #region 加密字符串 <summary> AES 加密(高级加密标准,是下一代的加密算法标准,速度快,安全级别高,目前 AES 标准的一个实现是 Rijndael 算法) </summary> <param name=”EncryptString”>待加密密文</param> <param name=”EncryptKey”>加密密钥</param> static string AESEncrypt(string EncryptString,string EncryptKey) { return Convert.ToBase64String(AESEncrypt(Encoding.Default.GetBytes(EncryptString),EncryptKey)); } #endregion #region 加密字节数组 byte[] AESEncrypt(byte[] EncryptByte,1)”>if (EncryptByte.Length == 0) { throw (new Exception(“明文不得为空”)); } if (string.IsNullOrEmpty(EncryptKey)) { 密钥不得为空byte[] m_strEncrypt; byte[] m_btIV = Convert.FromBase64String(Rkb4jvUy/ye7Cd7k89QQgQ==); byte[] m_salt = Convert.FromBase64String(gsf4jvkyhye5/d7k8OrLgM==); Rijndael m_AESProvider = Rijndael.Create(); try { MemoryStream m_stream = new MemoryStream(); PasswordDeriveBytes pdb = PasswordDeriveBytes(EncryptKey,m_salt); ICryptoTransform transform = m_AESProvider.CreateEncryptor(pdb.GetBytes(32),m_btIV); CryptoStream m_csstream = CryptoStream(m_stream,transform,CryptoStreamMode.Write); m_csstream.Write(EncryptByte,0,EncryptByte.Length); m_csstream.FlushFinalBlock(); m_strEncrypt = m_stream.ToArray(); m_stream.Close(); m_stream.Dispose(); m_csstream.Close(); m_csstream.Dispose(); } catch (IOException ex) { throw ex; } catch (CryptographicException ex) { catch (ArgumentException ex) { catch (Exception ex) { finally { m_AESProvider.Clear(); } m_strEncrypt; } #endregion #region 解密 #region 解密字符串 AES 解密(高级加密标准,是下一代的加密算法标准,速度快,安全级别高,目前 AES 标准的一个实现是 Rijndael 算法) <param name=”DecryptString”>待解密密文<param name=”DecryptKey”>解密密钥string AESDecrypt(string DecryptString,1)”> DecryptKey) { Convert.ToBase64String(AESDecrypt(Encoding.Default.GetBytes(DecryptString),DecryptKey)); } #region 解密字节数组 byte[] AESDecrypt(byte[] DecryptByte,1)”>if (DecryptByte.Length == 密文不得为空string.IsNullOrEmpty(DecryptKey)) { [] m_strDecrypt; PasswordDeriveBytes(DecryptKey,m_salt); ICryptoTransform transform = m_AESProvider.CreateDecryptor(pdb.GetBytes(= m_strDecrypt; } #endregion }}View Code

2、文件加密类

System; 文件加密类 FileEncrypt { #region 变量 一次处理的明文字节数 </summary> readonly int encryptSize = 10000000; 一次处理的密文字节数 int decryptSize = 10000016; #region 加密文件 加密文件 void EncryptFile(string path,1)”> pwd,RefreshFileProgress refreshFileProgress) { { if (File.Exists(path + .temp”)) File.Delete(path + ); using (FileStream fs = FileStream(path,FileMode.Open,FileAccess.Read)) { if (fs.Length > ) { using (FileStream fsnew = new FileStream(path + “)) File.SetAttributes(path + int blockCount = ((int)fs.Length – 1) / encryptSize + 1; for (int i = 0; i < blockCount; i++) { int size = encryptSize; if (i == blockCount – 1) size = (int)(fs.Length – i * encryptSize); byte[] bArr = new [size]; fs.Read(bArr,size); byte[] result = AES.AESEncrypt(bArr,pwd); fsnew.Write(result,result.Length); fsnew.Flush(); refreshFileProgress(blockCount,i + 1); //更新进度 } fsnew.Close(); fsnew.Dispose(); } fs.Close(); fs.Dispose(); FileAttributes fileAttr = File.GetAttributes(path); File.SetAttributes(path,FileAttributes.Archive); File.Delete(path); File.Move(path + catch (Exception ex) { File.Delete(path + ex; } } #region 解密文件 解密文件 void DecryptFile(1) / decryptSize + decryptSize; decryptSize); AES.AESDecrypt(bArr,1)”> } 更新文件加密进度 delegate void RefreshFileProgress(int max,1)”>int value);}View Code

 3、文件夹加密类

System.Collections.Generic; System.Linq; System.Text; Utils; EncryptFile.Utils{ 文件夹加密类 DirectoryEncrypt { #region 加密文件夹及其子文件夹中的所有文件 加密文件夹及其子文件夹中的所有文件 void EncryptDirectory(string dirPath,RefreshDirProgress refreshDirProgress,1)”>string[] filePaths = Directory.GetFiles(dirPath,*0; i < filePaths.Length; i++) { FileEncrypt.EncryptFile(filePaths[i],pwd,refreshFileProgress); refreshDirProgress(filePaths.Length,1)”>); } } #region 解密文件夹及其子文件夹中的所有文件 解密文件夹及其子文件夹中的所有文件 void DecryptDirectory() { FileEncrypt.DecryptFile(filePaths[i],1)”> 更新文件夹加密进度 void RefreshDirProgress( value);}View Code

4、跨线程访问控制委托

System.Windows.Forms; 跨线程访问控件的委托 void InvokeDelegate(); 跨线程访问控件类 InvokeUtil { 跨线程访问控件 <param name=”ctrl”>Form对象<param name=”de”>委托 Invoke(Control ctrl,Delegate de) { if (ctrl.IsHandleCreated) { ctrl.BeginInvoke(de); } } }}View Code

5、Form1.cs

System.ComponentModel; System.Data; System.Drawing; System.Windows.Forms; Utils; System.Threading; EncryptFile.Utils; EncryptFile{ partial Form1 : Form { #region 构造函数 public Form1() { InitializeComponent(); } #region 加密文件 private void btnEncrypt_Click(object sender,EventArgs e) { #region 验证 if (txtPwd.Text == “”) { Message@R_682_2419@.Show(密码不能为空”,1)”>提示; } if (txtPwdCfm.Text == 确认密码不能为空if (txtPwdCfm.Text != txtPwd.Text) { Message@R_682_2419@.Show(两次输入的密码不相同; } #endregion if (openFileDialog1.ShowDialog() == DialogResult.OK) { Thread thread = new Thread(new ParameterizedThreadStart(delegate( obj) { { InvokeDelegate invokeDelegate = delegate() { pbFile.Value = ; lblProgressFile.Text = 0%; pbDir.Visible = false; lblProgressDir.Visible = ; pbFile.Visible = ; lblProgressFile.Visible = ; lblShowPath.Text = 文件:” + openFileDialog1.FileName; lblShowPath.Visible = true; DisableBtns(); }; InvokeUtil.Invoke(this= DateTime.Now; FileEncrypt.EncryptFile(openFileDialog1.FileName,txtPwd.Text,RefreshFileProgress); DateTime t2 = DateTime.Now; string t = t2.Subtract(t1).TotalSeconds.ToString(0.00); if (Message@R_682_2419@.Show(加密成功,耗时” + t + 秒”) == DialogResult.OK) { invokeDelegate = () { EnableBtns(); }; InvokeUtil.Invoke( (Exception ex) { 加密失败:” + ex.Message,1)”> DialogResult.OK) { InvokeDelegate invokeDelegate = #region 解密文件 void btnDecrypt_Click( DateTime.Now; FileEncrypt.DecryptFile(openFileDialog1.FileName,1)”>解密成功,耗时解密失败:#region 文件夹加密 void btnEncryptDir_Click(if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) { if (Message@R_682_2419@.Show(string.Format(确定加密文件夹{0}? DialogResult.Cancel) { ; } Thread thread = () { pbDir.Value = ; lblProgressDir.Text = ; pbFile.Value = 文件夹: folderBrowserDialog1.SelectedPath; lblShowPath.Visible = DateTime.Now; DirectoryEncrypt.EncryptDirectory(folderBrowserDialog1.SelectedPath,RefreshDirProgress,1)”>加密失败:#region 文件夹解密 void btnDecryptDir_Click(确定解密文件夹{0}? DateTime.Now; DirectoryEncrypt.DecryptDirectory(folderBrowserDialog1.SelectedPath,1)”>#region 更新文件加密进度 更新文件加密进度 value) { InvokeDelegate invokeDelegate = () { if (max > ) { pbFile.Visible = ; lblProgressFile.Visible = ; } else { pbFile.Visible = ; } pbFile.Maximum = max; pbFile.Value = value; lblProgressFile.Text = value * 100 / max + %; }; InvokeUtil.Invoke(#region 更新文件夹加密进度 更新文件夹加密进度 () { pbDir.Maximum = max; pbDir.Value = value; lblProgressDir.Text = value * #region 显示密码 void cbxShowPwd_CheckedChanged( (cbxShowPwd.Checked) { txtPwd.PasswordChar = default(char); txtPwdCfm.PasswordChar = ); } { txtPwd.PasswordChar = ”; txtPwdCfm.PasswordChar = ; } } #region 关闭窗体事件 void Form1_FormClosing( (progressPanel.Visible) { Message@R_682_2419@.Show(正在处理文件,请等待…); e.Cancel = #region 控制按钮状态 禁用按钮 DisableBtns() { progressPanel.Visible = ; btnEncrypt.Enabled = ; btnDecrypt.Enabled = ; btnEncryptDir.Enabled = ; btnDecryptDir.Enabled = ; } 启用按钮 EnableBtns() { lblShowPath.Visible = ; progressPanel.Visible = ; } }}View Code

源码:

源码下载:下载地址