Browse Source

整理加密文件头信息

yuzhengyang 8 years ago
parent
commit
b7505a3b63

+ 1 - 1
Fork.Net/Oreo.Plugins/Oreo.FileMan/Views/MainForm.cs

@@ -53,7 +53,7 @@ namespace Oreo.FileMan.Views
 
 
         private void BtFileDecrypt_Click(object sender, EventArgs e)
         private void BtFileDecrypt_Click(object sender, EventArgs e)
         {
         {
-            string pwd = "12345678901234567890123456789012";
+            string pwd = "123456789012";
             string[] fileInfo = new string[128];
             string[] fileInfo = new string[128];
             OpenFileDialog fileDialog = new OpenFileDialog();
             OpenFileDialog fileDialog = new OpenFileDialog();
             fileDialog.Title = "请选择要解密的文件";
             fileDialog.Title = "请选择要解密的文件";

+ 26 - 0
Fork.Net/Y.Utils/DataUtils/EncryptUtils/MD5Tool.cs

@@ -0,0 +1,26 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Security.Cryptography;
+using System.Text;
+
+namespace Y.Utils.DataUtils.EncryptUtils
+{
+    public class MD5Tool
+    {
+        /// <summary>
+        /// 给一个字符串进行MD5加密
+        /// </summary>
+        /// <param name="s">待加密字符串</param>
+        /// <returns>加密后的字符串</returns>
+        public static string Encrypt(string s)
+        {
+            string result = "";
+            byte[] buffer = Encoding.Default.GetBytes(s);
+            HashAlgorithm algorithm = MD5.Create();
+            byte[] hashBytes = algorithm.ComputeHash(buffer);
+            result = BitConverter.ToString(hashBytes).Replace("-", "");
+            return result;
+        }
+    }
+}

+ 63 - 21
Fork.Net/Y.Utils/IOUtils/FileUtils/FileEncryptTool.cs

@@ -12,6 +12,9 @@ namespace Y.Utils.IOUtils.FileUtils
         private static int AESKeyLength = 32;//AES加密的密码为32位
         private static int AESKeyLength = 32;//AES加密的密码为32位
         private static char AESFillChar = 'Y';//AES密码填充字符
         private static char AESFillChar = 'Y';//AES密码填充字符
         private static string FileTypeDesc = "Oreo.FileMan.EncryptFile";
         private static string FileTypeDesc = "Oreo.FileMan.EncryptFile";
+        private static int FileHeadLength = 128;
+        private static int FileBuffer = 1024 * 1024;
+
         /// <summary>
         /// <summary>
         /// 文件加密
         /// 文件加密
         /// </summary>
         /// </summary>
@@ -22,6 +25,7 @@ namespace Y.Utils.IOUtils.FileUtils
         public static bool Encrypt(string srcFile, string dstFile, string password)
         public static bool Encrypt(string srcFile, string dstFile, string password)
         {
         {
             string fmtPwd = FmtPassword(password);
             string fmtPwd = FmtPassword(password);
+            string pwdMd5 = MD5Tool.Encrypt(fmtPwd);
             //检测文件存在
             //检测文件存在
             if (File.Exists(srcFile) && !File.Exists(dstFile))
             if (File.Exists(srcFile) && !File.Exists(dstFile))
             {
             {
@@ -32,29 +36,60 @@ namespace Y.Utils.IOUtils.FileUtils
                     {
                     {
                         try
                         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;
                             int readCount = 0;
-                            byte[] buffer = new byte[1024 * 1024];
+                            byte[] buffer = new byte[FileBuffer];
                             while ((readCount = fsRead.Read(buffer, 0, buffer.Length)) > 0)
                             while ((readCount = fsRead.Read(buffer, 0, buffer.Length)) > 0)
                             {
                             {
                                 if (readCount != buffer.Length)
                                 if (readCount != buffer.Length)
@@ -90,18 +125,22 @@ namespace Y.Utils.IOUtils.FileUtils
         public static bool Decrypt(string srcFile, string dstFile, string password)
         public static bool Decrypt(string srcFile, string dstFile, string password)
         {
         {
             string fmtPwd = FmtPassword(password);
             string fmtPwd = FmtPassword(password);
+            string pwdMd5 = MD5Tool.Encrypt(fmtPwd);
             //检测文件存在
             //检测文件存在
             if (File.Exists(srcFile) && !File.Exists(dstFile))
             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 fsRead = new FileStream(srcFile, FileMode.Open))
                 {
                 {
                     using (FileStream fsWrite = new FileStream(dstFile, FileMode.Create))
                     using (FileStream fsWrite = new FileStream(dstFile, FileMode.Create))
                     {
                     {
                         try
                         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)
                             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[] typeByte = new byte[dataLong[0]];
                                 byte[] md5Byte = new byte[dataLong[1]];
                                 byte[] md5Byte = new byte[dataLong[1]];
                                 byte[] pwdByte = new byte[dataLong[2]];
                                 byte[] pwdByte = new byte[dataLong[2]];
+                                byte[] pwdMd5Byte = new byte[dataLong[3]];
 
 
                                 fsRead.Read(typeByte, 0, typeByte.Length);
                                 fsRead.Read(typeByte, 0, typeByte.Length);
                                 fsRead.Read(md5Byte, 0, md5Byte.Length);
                                 fsRead.Read(md5Byte, 0, md5Byte.Length);
                                 fsRead.Read(pwdByte, 0, pwdByte.Length);
                                 fsRead.Read(pwdByte, 0, pwdByte.Length);
+                                fsRead.Read(pwdMd5Byte, 0, pwdMd5Byte.Length);
 
 
                                 fileInfo[0] = Encoding.Default.GetString(typeByte);
                                 fileInfo[0] = Encoding.Default.GetString(typeByte);
                                 fileInfo[1] = Encoding.Default.GetString(md5Byte);
                                 fileInfo[1] = Encoding.Default.GetString(md5Byte);
                                 fileInfo[2] = Encoding.Default.GetString(pwdByte);
                                 fileInfo[2] = Encoding.Default.GetString(pwdByte);
+                                fileInfo[3] = Encoding.Default.GetString(pwdMd5Byte);
                             }
                             }
 
 
-                            if (fmtPwd == fileInfo[2])
+                            if (pwdMd5 == fileInfo[3])
                             {
                             {
                                 int readCount = 0;
                                 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)
                                 while ((readCount = fsRead.Read(buffer, 0, buffer.Length)) > 0)
                                 {
                                 {
                                     if (readCount != buffer.Length)
                                     if (readCount != buffer.Length)

+ 1 - 0
Fork.Net/Y.Utils/Y.Utils.csproj

@@ -54,6 +54,7 @@
   </ItemGroup>
   </ItemGroup>
   <ItemGroup>
   <ItemGroup>
     <Compile Include="AppUtils\AppUnique.cs" />
     <Compile Include="AppUtils\AppUnique.cs" />
+    <Compile Include="DataUtils\EncryptUtils\MD5Tool.cs" />
     <Compile Include="DataUtils\UnitConvertUtils\ByteConvertUtils.cs" />
     <Compile Include="DataUtils\UnitConvertUtils\ByteConvertUtils.cs" />
     <Compile Include="IOUtils\FileUtils\FileEncryptTool.cs" />
     <Compile Include="IOUtils\FileUtils\FileEncryptTool.cs" />
     <Compile Include="IOUtils\ImageUtils\IconTool.cs" />
     <Compile Include="IOUtils\ImageUtils\IconTool.cs" />