123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499 |
- using System;
- using System.IO;
- using System.Security.Cryptography;
- using System.Text;
- namespace BaseLibRWFile
- {
- /// <summary>
- /// AES加密解密
- /// </summary>
- public class AES
- {
- #region 加密
- #region 加密字符串
- /// <summary>
- /// AES 加密(高级加密标准,是下一代的加密算法标准,速度快,安全级别高,目前 AES 标准的一个实现是 Rijndael 算法)
- /// </summary>
- /// <param name="EncryptString">待加密密文</param>
- /// <param name="EncryptKey">加密密钥</param>
- public static string AESEncrypt(string EncryptString, string EncryptKey)
- {
- return Convert.ToBase64String(AESEncrypt(Encoding.Default.GetBytes(EncryptString), EncryptKey));
- }
- #endregion
- #region 加密字节数组
- /// <summary>
- /// AES 加密(高级加密标准,是下一代的加密算法标准,速度快,安全级别高,目前 AES 标准的一个实现是 Rijndael 算法)
- /// </summary>
- /// <param name="EncryptString">待加密密文</param>
- /// <param name="EncryptKey">加密密钥</param>
- public static byte[] AESEncrypt(byte[] EncryptByte, string EncryptKey)
- {
- if (EncryptByte.Length == 0) { throw (new Exception("明文不得为空")); }
- if (string.IsNullOrEmpty(EncryptKey)) { throw (new Exception("密钥不得为空")); }
- 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 = new PasswordDeriveBytes(EncryptKey, m_salt);
- ICryptoTransform transform = m_AESProvider.CreateEncryptor(pdb.GetBytes(32), m_btIV);
- CryptoStream m_csstream = new 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) { throw ex; }
- catch (ArgumentException ex) { throw ex; }
- catch (Exception ex) { throw ex; }
- finally { m_AESProvider.Clear(); }
- return m_strEncrypt;
- }
- #endregion
- #endregion
- #region 解密
- #region 解密字符串
- /// <summary>
- /// AES 解密(高级加密标准,是下一代的加密算法标准,速度快,安全级别高,目前 AES 标准的一个实现是 Rijndael 算法)
- /// </summary>
- /// <param name="DecryptString">待解密密文</param>
- /// <param name="DecryptKey">解密密钥</param>
- public static string AESDecrypt(string DecryptString, string DecryptKey)
- {
- return Convert.ToBase64String(AESDecrypt(Encoding.Default.GetBytes(DecryptString), DecryptKey));
- }
- #endregion
- #region 解密字节数组
- /// <summary>
- /// AES 解密(高级加密标准,是下一代的加密算法标准,速度快,安全级别高,目前 AES 标准的一个实现是 Rijndael 算法)
- /// </summary>
- /// <param name="DecryptString">待解密密文</param>
- /// <param name="DecryptKey">解密密钥</param>
- public static byte[] AESDecrypt(byte[] DecryptByte, string DecryptKey)
- {
- if (DecryptByte.Length == 0) { throw (new Exception("密文不得为空")); }
- if (string.IsNullOrEmpty(DecryptKey)) { throw (new Exception("密钥不得为空")); }
- byte[] m_strDecrypt;
- 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 = new PasswordDeriveBytes(DecryptKey, m_salt);
- ICryptoTransform transform = m_AESProvider.CreateDecryptor(pdb.GetBytes(32), m_btIV);
- CryptoStream m_csstream = new CryptoStream(m_stream, transform, CryptoStreamMode.Write);
- m_csstream.Write(DecryptByte, 0, DecryptByte.Length);
- m_csstream.FlushFinalBlock();
- m_strDecrypt = m_stream.ToArray();
- m_stream.Close(); m_stream.Dispose();
- m_csstream.Close(); m_csstream.Dispose();
- }
- catch (IOException ex) { throw ex; }
- catch (CryptographicException ex) { throw ex; }
- catch (ArgumentException ex) { throw ex; }
- catch (Exception ex) { throw ex; }
- finally { m_AESProvider.Clear(); }
- return m_strDecrypt;
- }
- #endregion
- #endregion
- }
- /// <summary>
- /// 文件加密类
- /// </summary>
- public class FileEncrypt
- {
- #region 变量
- /// <summary>
- /// 一次处理的明文字节数
- /// </summary>
- public static readonly int encryptSize = 10000000;
- /// <summary>
- /// 一次处理的密文字节数
- /// </summary>
- public static readonly int decryptSize = 10000016;
- #endregion
- #region 加密文件
- /// <summary>
- /// 加密文件
- /// </summary>
- public static void EncryptFile(string path, string pwd)
- {
- try
- {
- if (File.Exists(path + ".temp")) File.Delete(path + ".temp");
- using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
- {
- if (fs.Length > 0)
- {
- using (FileStream fsnew = new FileStream(path + ".temp", FileMode.OpenOrCreate, FileAccess.Write))
- {
- if (File.Exists(path + ".temp")) File.SetAttributes(path + ".temp", FileAttributes.Hidden);
- 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 byte[size];
- fs.Read(bArr, 0, size);
- byte[] result = AES.AESEncrypt(bArr, pwd);
- fsnew.Write(result, 0, 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 + ".temp", path);
- File.SetAttributes(path, fileAttr);
- }
- }
- }
- catch (Exception ex)
- {
- File.Delete(path + ".temp");
- throw ex;
- }
- }
- #endregion
- #region 解密文件
- /// <summary>
- /// 解密文件
- /// </summary>
- public static void DecryptFile(string path, string pwd)
- {
- try
- {
- if (File.Exists(path + ".temp")) File.Delete(path + ".temp");
- using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
- {
- if (fs.Length > 0)
- {
- using (FileStream fsnew = new FileStream(path + ".temp", FileMode.OpenOrCreate, FileAccess.Write))
- {
- if (File.Exists(path + ".temp")) File.SetAttributes(path + ".temp", FileAttributes.Hidden);
- int blockCount = ((int)fs.Length - 1) / decryptSize + 1;
- for (int i = 0; i < blockCount; i++)
- {
- int size = decryptSize;
- if (i == blockCount - 1) size = (int)(fs.Length - i * decryptSize);
- byte[] bArr = new byte[size];
- fs.Read(bArr, 0, size);
- byte[] result = AES.AESDecrypt(bArr, pwd);
- fsnew.Write(result, 0, 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 + ".temp", path);
- File.SetAttributes(path, fileAttr);
- }
- }
- }
- catch (Exception ex)
- {
- File.Delete(path + ".temp");
- throw ex;
- }
- }
- #endregion
- }
- /// <summary>
- /// 更新文件加密进度
- /// </summary>
- public delegate void RefreshFileProgress(int max, int value);
- /// <summary>
- /// 文件夹加密类
- /// </summary>
- public class DirectoryEncrypt
- {
- #region 加密文件夹及其子文件夹中的所有文件
- /// <summary>
- /// 加密文件夹及其子文件夹中的所有文件
- /// </summary>
- public static void EncryptDirectory(string dirPath, string pwd, RefreshDirProgress refreshDirProgress, RefreshFileProgress refreshFileProgress)
- {
- string[] filePaths = Directory.GetFiles(dirPath, "*", SearchOption.AllDirectories);
- for (int i = 0; i < filePaths.Length; i++)
- {
- FileEncrypt.EncryptFile(filePaths[i], pwd);
- refreshDirProgress(filePaths.Length, i + 1);
- }
- }
- #endregion
- #region 解密文件夹及其子文件夹中的所有文件
- /// <summary>
- /// 解密文件夹及其子文件夹中的所有文件
- /// </summary>
- public static void DecryptDirectory(string dirPath, string pwd, RefreshDirProgress refreshDirProgress, RefreshFileProgress refreshFileProgress)
- {
- string[] filePaths = Directory.GetFiles(dirPath, "*", SearchOption.AllDirectories);
- for (int i = 0; i < filePaths.Length; i++)
- {
- FileEncrypt.DecryptFile(filePaths[i], pwd);
- refreshDirProgress(filePaths.Length, i + 1);
- }
- }
- #endregion
- }
- /// <summary>
- /// 更新文件夹加密进度
- /// </summary>
- public delegate void RefreshDirProgress(int max, int value);
- }
|