|
|
@@ -12,6 +12,9 @@ namespace Y.Utils.IOUtils.FileUtils
|
|
|
private static int AESKeyLength = 32;//AES加密的密码为32位
|
|
|
private static char AESFillChar = 'Y';//AES密码填充字符
|
|
|
private static string FileTypeDesc = "Oreo.FileMan.EncryptFile";
|
|
|
+ private static int FileHeadLength = 128;
|
|
|
+ private static int FileBuffer = 1024 * 1024;
|
|
|
+
|
|
|
/// <summary>
|
|
|
/// 文件加密
|
|
|
/// </summary>
|
|
|
@@ -22,6 +25,7 @@ namespace Y.Utils.IOUtils.FileUtils
|
|
|
public static bool Encrypt(string srcFile, string dstFile, string password)
|
|
|
{
|
|
|
string fmtPwd = FmtPassword(password);
|
|
|
+ string pwdMd5 = MD5Tool.Encrypt(fmtPwd);
|
|
|
//检测文件存在
|
|
|
if (File.Exists(srcFile) && !File.Exists(dstFile))
|
|
|
{
|
|
|
@@ -32,29 +36,60 @@ namespace Y.Utils.IOUtils.FileUtils
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
+ //文件头部数据定义
|
|
|
+ List<byte[]> headdata = new List<byte[]>()
|
|
|
+ {
|
|
|
+ Encoding.Default.GetBytes(FileTypeDesc),
|
|
|
+ Encoding.Default.GetBytes(md5),
|
|
|
+ Encoding.Default.GetBytes(fmtPwd),
|
|
|
+ Encoding.Default.GetBytes(pwdMd5)
|
|
|
+ };
|
|
|
+ //写入长度
|
|
|
+ for (int i = 0; i < FileHeadLength; i++)
|
|
|
+ {
|
|
|
+ if (headdata.Count > i)
|
|
|
+ {
|
|
|
+ byte[] length = BitConverter.GetBytes(headdata[i].Length);
|
|
|
+ fsWrite.Write(length, 0, length.Length);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ byte[] length = BitConverter.GetBytes(0);
|
|
|
+ fsWrite.Write(length, 0, length.Length);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //写入数据
|
|
|
+ for (int i = 0; i < headdata.Count; i++)
|
|
|
+ {
|
|
|
+ fsWrite.Write(headdata[i], 0, headdata[i].Length);
|
|
|
+ }
|
|
|
+
|
|
|
//设置文件头部信息
|
|
|
- byte[] typeByte = Encoding.Default.GetBytes(FileTypeDesc);
|
|
|
- byte[] md5Byte = Encoding.Default.GetBytes(md5);
|
|
|
- byte[] pwdByte = Encoding.Default.GetBytes(fmtPwd);
|
|
|
+ //byte[] typeByte = Encoding.Default.GetBytes(FileTypeDesc);
|
|
|
+ //byte[] md5Byte = Encoding.Default.GetBytes(md5);
|
|
|
+ //byte[] pwdByte = Encoding.Default.GetBytes(fmtPwd);
|
|
|
+ //byte[] pwdMd5Byte = Encoding.Default.GetBytes(pwdMd5);
|
|
|
//设置文件头部数据长度信息
|
|
|
- long[] headpart = new long[128];
|
|
|
- headpart[0] = typeByte.Length;
|
|
|
- headpart[1] = md5Byte.Length;
|
|
|
- headpart[2] = pwdByte.Length;
|
|
|
+ //long[] headpart = new long[FileHeadLength];
|
|
|
+ //headpart[0] = typeByte.Length;
|
|
|
+ //headpart[1] = md5Byte.Length;
|
|
|
+ //headpart[2] = pwdByte.Length;
|
|
|
+ //headpart[3] = pwdMd5Byte.Length;
|
|
|
//写入头部长度信息
|
|
|
- foreach (var h in headpart)
|
|
|
- {
|
|
|
- byte[] temp = BitConverter.GetBytes(h);
|
|
|
- fsWrite.Write(temp, 0, temp.Length);
|
|
|
- }
|
|
|
+ //foreach (var h in headpart)
|
|
|
+ //{
|
|
|
+ // byte[] temp = BitConverter.GetBytes(h);
|
|
|
+ // fsWrite.Write(temp, 0, temp.Length);
|
|
|
+ //}
|
|
|
//写入文件头部信息
|
|
|
- fsWrite.Write(typeByte, 0, typeByte.Length);
|
|
|
- fsWrite.Write(md5Byte, 0, md5Byte.Length);
|
|
|
- fsWrite.Write(pwdByte, 0, pwdByte.Length);
|
|
|
+ //fsWrite.Write(typeByte, 0, typeByte.Length);
|
|
|
+ //fsWrite.Write(md5Byte, 0, md5Byte.Length);
|
|
|
+ //fsWrite.Write(pwdByte, 0, pwdByte.Length);
|
|
|
+ //fsWrite.Write(pwdMd5Byte, 0, pwdMd5Byte.Length);
|
|
|
|
|
|
//写入文件源数据
|
|
|
int readCount = 0;
|
|
|
- byte[] buffer = new byte[1024 * 1024];
|
|
|
+ byte[] buffer = new byte[FileBuffer];
|
|
|
while ((readCount = fsRead.Read(buffer, 0, buffer.Length)) > 0)
|
|
|
{
|
|
|
if (readCount != buffer.Length)
|
|
|
@@ -90,18 +125,22 @@ namespace Y.Utils.IOUtils.FileUtils
|
|
|
public static bool Decrypt(string srcFile, string dstFile, string password)
|
|
|
{
|
|
|
string fmtPwd = FmtPassword(password);
|
|
|
+ string pwdMd5 = MD5Tool.Encrypt(fmtPwd);
|
|
|
//检测文件存在
|
|
|
if (File.Exists(srcFile) && !File.Exists(dstFile))
|
|
|
{
|
|
|
- string[] fileInfo = new string[128];
|
|
|
+ string[] fileInfo = new string[FileHeadLength];
|
|
|
using (FileStream fsRead = new FileStream(srcFile, FileMode.Open))
|
|
|
{
|
|
|
using (FileStream fsWrite = new FileStream(dstFile, FileMode.Create))
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
- byte[] headpart = new byte[8 * 128];//fmk文件头
|
|
|
- long[] dataLong = new long[128];//信息长度
|
|
|
+ List<byte[]> headdata = new List<byte[]>();
|
|
|
+ for()
|
|
|
+
|
|
|
+ byte[] headpart = new byte[8 * FileHeadLength];//fmk文件头
|
|
|
+ long[] dataLong = new long[FileHeadLength];//信息长度
|
|
|
if (fsRead.Read(headpart, 0, headpart.Length) == headpart.Length)
|
|
|
{
|
|
|
//读取信息长度
|
|
|
@@ -112,20 +151,23 @@ namespace Y.Utils.IOUtils.FileUtils
|
|
|
byte[] typeByte = new byte[dataLong[0]];
|
|
|
byte[] md5Byte = new byte[dataLong[1]];
|
|
|
byte[] pwdByte = new byte[dataLong[2]];
|
|
|
+ byte[] pwdMd5Byte = new byte[dataLong[3]];
|
|
|
|
|
|
fsRead.Read(typeByte, 0, typeByte.Length);
|
|
|
fsRead.Read(md5Byte, 0, md5Byte.Length);
|
|
|
fsRead.Read(pwdByte, 0, pwdByte.Length);
|
|
|
+ fsRead.Read(pwdMd5Byte, 0, pwdMd5Byte.Length);
|
|
|
|
|
|
fileInfo[0] = Encoding.Default.GetString(typeByte);
|
|
|
fileInfo[1] = Encoding.Default.GetString(md5Byte);
|
|
|
fileInfo[2] = Encoding.Default.GetString(pwdByte);
|
|
|
+ fileInfo[3] = Encoding.Default.GetString(pwdMd5Byte);
|
|
|
}
|
|
|
|
|
|
- if (fmtPwd == fileInfo[2])
|
|
|
+ if (pwdMd5 == fileInfo[3])
|
|
|
{
|
|
|
int readCount = 0;
|
|
|
- byte[] buffer = new byte[1024 * 1024 + 16];
|
|
|
+ byte[] buffer = new byte[FileBuffer + 16];
|
|
|
while ((readCount = fsRead.Read(buffer, 0, buffer.Length)) > 0)
|
|
|
{
|
|
|
if (readCount != buffer.Length)
|