|
@@ -1,7 +1,7 @@
|
|
|
//************************************************************************
|
|
//************************************************************************
|
|
|
// https://github.com/yuzhengyang
|
|
// https://github.com/yuzhengyang
|
|
|
// author: yuzhengyang
|
|
// author: yuzhengyang
|
|
|
-// date: 2017.6.8 - 2017.6.14
|
|
|
|
|
|
|
+// date: 2017.6.8 - 2017.6.16
|
|
|
// desc: 文件加密工具
|
|
// desc: 文件加密工具
|
|
|
// Copyright (c) yuzhengyang. All rights reserved.
|
|
// Copyright (c) yuzhengyang. All rights reserved.
|
|
|
//************************************************************************
|
|
//************************************************************************
|
|
@@ -11,6 +11,7 @@ using System.IO;
|
|
|
using System.Linq;
|
|
using System.Linq;
|
|
|
using System.Text;
|
|
using System.Text;
|
|
|
using Y.Utils.DataUtils.EncryptUtils;
|
|
using Y.Utils.DataUtils.EncryptUtils;
|
|
|
|
|
+using Y.Utils.DelegateUtils;
|
|
|
|
|
|
|
|
namespace Y.Utils.IOUtils.FileUtils
|
|
namespace Y.Utils.IOUtils.FileUtils
|
|
|
{
|
|
{
|
|
@@ -36,7 +37,7 @@ namespace Y.Utils.IOUtils.FileUtils
|
|
|
/// -12:加密后的目标文件已存在
|
|
/// -12:加密后的目标文件已存在
|
|
|
/// -404:未知错误,操作失败
|
|
/// -404:未知错误,操作失败
|
|
|
/// </returns>
|
|
/// </returns>
|
|
|
- public static int Encrypt(string srcFile, string dstFile, string password, bool overwrite = true)
|
|
|
|
|
|
|
+ public static int Encrypt(string srcFile, string dstFile, string password, ProgressDelegate.ProgressHandler progress, bool overwrite = true)
|
|
|
{
|
|
{
|
|
|
DateTime beginTime = DateTime.Now;
|
|
DateTime beginTime = DateTime.Now;
|
|
|
if (!File.Exists(srcFile)) return -11; //要加密的文件不存在
|
|
if (!File.Exists(srcFile)) return -11; //要加密的文件不存在
|
|
@@ -46,6 +47,7 @@ namespace Y.Utils.IOUtils.FileUtils
|
|
|
string pwdMd5 = MD5Tool.Encrypt(MD5Tool.Encrypt(fmtPwd));
|
|
string pwdMd5 = MD5Tool.Encrypt(MD5Tool.Encrypt(fmtPwd));
|
|
|
|
|
|
|
|
string md5 = FileTool.GetMD5(srcFile);
|
|
string md5 = FileTool.GetMD5(srcFile);
|
|
|
|
|
+ long current = 0;
|
|
|
using (FileStream fsRead = new FileStream(srcFile, FileMode.Open))
|
|
using (FileStream fsRead = new FileStream(srcFile, FileMode.Open))
|
|
|
{
|
|
{
|
|
|
using (FileStream fsWrite = new FileStream(dstFile, FileMode.Create))
|
|
using (FileStream fsWrite = new FileStream(dstFile, FileMode.Create))
|
|
@@ -97,6 +99,8 @@ namespace Y.Utils.IOUtils.FileUtils
|
|
|
byte[] enbyte = AesTool.Encrypt(buffer, fmtPwd);
|
|
byte[] enbyte = AesTool.Encrypt(buffer, fmtPwd);
|
|
|
fsWrite.Write(enbyte, 0, enbyte.Length);
|
|
fsWrite.Write(enbyte, 0, enbyte.Length);
|
|
|
}
|
|
}
|
|
|
|
|
+ current += readCount;
|
|
|
|
|
+ progress?.Invoke(current, fsRead.Length);
|
|
|
}
|
|
}
|
|
|
return (int)Math.Ceiling((DateTime.Now - beginTime).TotalSeconds);//操作成功
|
|
return (int)Math.Ceiling((DateTime.Now - beginTime).TotalSeconds);//操作成功
|
|
|
}
|
|
}
|
|
@@ -123,7 +127,7 @@ namespace Y.Utils.IOUtils.FileUtils
|
|
|
/// -90:解锁密码错误
|
|
/// -90:解锁密码错误
|
|
|
/// -404:未知错误,操作失败
|
|
/// -404:未知错误,操作失败
|
|
|
/// </returns>
|
|
/// </returns>
|
|
|
- public static int Decrypt(string srcFile, string dstFile, string password, bool overwrite = true)
|
|
|
|
|
|
|
+ public static int Decrypt(string srcFile, string dstFile, string password, ProgressDelegate.ProgressHandler progress, bool overwrite = true)
|
|
|
{
|
|
{
|
|
|
DateTime beginTime = DateTime.Now;
|
|
DateTime beginTime = DateTime.Now;
|
|
|
if (!File.Exists(srcFile)) return -11;//要解密的文件不存在
|
|
if (!File.Exists(srcFile)) return -11;//要解密的文件不存在
|
|
@@ -132,6 +136,8 @@ namespace Y.Utils.IOUtils.FileUtils
|
|
|
string fmtPwd = AesTool.FmtPassword(password);
|
|
string fmtPwd = AesTool.FmtPassword(password);
|
|
|
string pwdMd5 = MD5Tool.Encrypt(MD5Tool.Encrypt(fmtPwd));
|
|
string pwdMd5 = MD5Tool.Encrypt(MD5Tool.Encrypt(fmtPwd));
|
|
|
List<string> headdata = new List<string>();
|
|
List<string> headdata = new List<string>();
|
|
|
|
|
+
|
|
|
|
|
+ long current = 0;
|
|
|
using (FileStream fsRead = new FileStream(srcFile, FileMode.Open))
|
|
using (FileStream fsRead = new FileStream(srcFile, FileMode.Open))
|
|
|
{
|
|
{
|
|
|
using (FileStream fsWrite = new FileStream(dstFile, FileMode.Create))
|
|
using (FileStream fsWrite = new FileStream(dstFile, FileMode.Create))
|
|
@@ -140,26 +146,27 @@ namespace Y.Utils.IOUtils.FileUtils
|
|
|
{
|
|
{
|
|
|
//读取文件类型标识和版本号
|
|
//读取文件类型标识和版本号
|
|
|
byte[] filetype = Encoding.Default.GetBytes(FileType);
|
|
byte[] filetype = Encoding.Default.GetBytes(FileType);
|
|
|
- fsRead.Read(filetype, 0, filetype.Length);
|
|
|
|
|
|
|
+ current += fsRead.Read(filetype, 0, filetype.Length);
|
|
|
string filetypestr = Encoding.Default.GetString(filetype);
|
|
string filetypestr = Encoding.Default.GetString(filetype);
|
|
|
byte[] fileversion = Encoding.Default.GetBytes(FileVersion);
|
|
byte[] fileversion = Encoding.Default.GetBytes(FileVersion);
|
|
|
- fsRead.Read(fileversion, 0, fileversion.Length);
|
|
|
|
|
|
|
+ current += fsRead.Read(fileversion, 0, fileversion.Length);
|
|
|
string fileversionstr = Encoding.Default.GetString(fileversion);
|
|
string fileversionstr = Encoding.Default.GetString(fileversion);
|
|
|
if (filetypestr != FileType && fileversionstr != FileVersion) return -20;//文件类型不匹配
|
|
if (filetypestr != FileType && fileversionstr != FileVersion) return -20;//文件类型不匹配
|
|
|
|
|
|
|
|
//读取头部信息个数
|
|
//读取头部信息个数
|
|
|
byte[] count = new byte[4];
|
|
byte[] count = new byte[4];
|
|
|
- fsRead.Read(count, 0, count.Length);
|
|
|
|
|
|
|
+ current += fsRead.Read(count, 0, count.Length);
|
|
|
int countint = BitConverter.ToInt32(count, 0);
|
|
int countint = BitConverter.ToInt32(count, 0);
|
|
|
//读取各部分长度和数据
|
|
//读取各部分长度和数据
|
|
|
byte[] headlength = new byte[4 * countint];
|
|
byte[] headlength = new byte[4 * countint];
|
|
|
if (fsRead.Read(headlength, 0, headlength.Length) == headlength.Length)
|
|
if (fsRead.Read(headlength, 0, headlength.Length) == headlength.Length)
|
|
|
{
|
|
{
|
|
|
|
|
+ current += headlength.Length;
|
|
|
for (int i = 0; i < countint; i++)
|
|
for (int i = 0; i < countint; i++)
|
|
|
{
|
|
{
|
|
|
int datalong = BitConverter.ToInt32(headlength, i * 4);
|
|
int datalong = BitConverter.ToInt32(headlength, i * 4);
|
|
|
byte[] tempdata = new byte[datalong];
|
|
byte[] tempdata = new byte[datalong];
|
|
|
- fsRead.Read(tempdata, 0, datalong);
|
|
|
|
|
|
|
+ current += fsRead.Read(tempdata, 0, datalong);
|
|
|
headdata.Add(Encoding.Default.GetString(tempdata));
|
|
headdata.Add(Encoding.Default.GetString(tempdata));
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -182,6 +189,8 @@ namespace Y.Utils.IOUtils.FileUtils
|
|
|
byte[] debyte = AesTool.Decrypt(buffer, fmtPwd);
|
|
byte[] debyte = AesTool.Decrypt(buffer, fmtPwd);
|
|
|
fsWrite.Write(debyte, 0, debyte.Length);
|
|
fsWrite.Write(debyte, 0, debyte.Length);
|
|
|
}
|
|
}
|
|
|
|
|
+ current += readCount;
|
|
|
|
|
+ progress?.Invoke(current, fsRead.Length);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
catch (Exception e) { }
|
|
catch (Exception e) { }
|