From 17932ce3fab5033cb2612e8b87137b6ec579550e Mon Sep 17 00:00:00 2001 From: liukaiyuan Date: Wed, 9 Nov 2022 21:19:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=95=B0=E5=AD=97=E7=AD=BE?= =?UTF-8?q?=E5=90=8D=E9=AA=8C=E8=AF=81=E6=9C=BA=E5=88=B6=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LKY_OfficeTools/Common/Com_FileOS.cs | 119 ++++++++++++++++++++- LKY_OfficeTools/LKY_OfficeTools.csproj | 2 + LKY_OfficeTools/Lib/Lib_AppInfo.cs | 35 ++++-- LKY_OfficeTools/Lib/Lib_AppLog.cs | 8 +- LKY_OfficeTools/Lib/Lib_AppSignCert.cs | 109 +++++++++++++++++++ LKY_OfficeTools/Lib/Lib_AppUpdate.cs | 2 +- LKY_OfficeTools/OfficeTools.cs | 3 + LKY_OfficeTools/Properties/AssemblyInfo.cs | 4 +- 8 files changed, 268 insertions(+), 14 deletions(-) create mode 100644 LKY_OfficeTools/Lib/Lib_AppSignCert.cs diff --git a/LKY_OfficeTools/Common/Com_FileOS.cs b/LKY_OfficeTools/Common/Com_FileOS.cs index 681349e..4098b09 100644 --- a/LKY_OfficeTools/Common/Com_FileOS.cs +++ b/LKY_OfficeTools/Common/Com_FileOS.cs @@ -77,7 +77,7 @@ internal class ScanFiles /// /// /// - public void GetFilesByExtension(string dirPath, string fileType = "*", bool isRoot = false) + internal void GetFilesByExtension(string dirPath, string fileType = "*", bool isRoot = false) { if (Directory.Exists(dirPath)) //目录存在 { @@ -132,5 +132,122 @@ public void GetFilesByExtension(string dirPath, string fileType = "*", bool isRo } } + /// + /// 转换文件不同格式,如:流、文件流等 + /// + internal class Covert + { + /* - - - - - - - - - - - - - - - - - - - - - - - - + * Stream 和 byte[] 之间的转换 + * - - - - - - - - - - - - - - - - - - - - - - - */ + /// + /// 将 Stream 转成 byte[] + /// + internal static byte[] StreamToBytes(Stream stream) + { + byte[] bytes = new byte[stream.Length]; + stream.Read(bytes, 0, bytes.Length); + + // 设置当前流的位置为流的开始 + stream.Seek(0, SeekOrigin.Begin); + return bytes; + } + + /// + /// 将 byte[] 转成 Stream + /// + internal static Stream BytesToStream(byte[] bytes) + { + Stream stream = new MemoryStream(bytes); + return stream; + } + + + /* - - - - - - - - - - - - - - - - - - - - - - - - + * Stream 和 文件之间的转换 + * - - - - - - - - - - - - - - - - - - - - - - - */ + /// + /// 将 Stream 写入文件 + /// + internal static void StreamToFile(Stream stream, string fileName) + { + // 把 Stream 转换成 byte[] + byte[] bytes = new byte[stream.Length]; + stream.Read(bytes, 0, bytes.Length); + // 设置当前流的位置为流的开始 + stream.Seek(0, SeekOrigin.Begin); + + // 把 byte[] 写入文件 + FileStream fs = new FileStream(fileName, FileMode.Create); + BinaryWriter bw = new BinaryWriter(fs); + bw.Write(bytes); + bw.Close(); + fs.Close(); + } + + /// + /// 从文件读取 Stream + /// + internal static Stream FileToStream(string fileName) + { + // 打开文件 + FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read); + // 读取文件的 byte[] + byte[] bytes = new byte[fileStream.Length]; + fileStream.Read(bytes, 0, bytes.Length); + fileStream.Close(); + // 把 byte[] 转换成 Stream + Stream stream = new MemoryStream(bytes); + return stream; + } + } + + /// + /// 文件写出类 + /// + internal class Write + { + /// + /// 将已知的所有文本一次性写入文本,并覆盖此前内容 + /// + /// + internal static bool FromAllText(string all_text, string to_path) + { + try + { + File.WriteAllText(to_path, all_text, Encoding.UTF8); + return true; + } + catch + { + return false; + } + } + + /// + /// 将Stream数据写出到本地文件 + /// + /// + internal static bool FromStream(Stream stream, string to_path) + { + try + { + byte[] Save = Covert.StreamToBytes(stream); + + //创建文件所在目录 + Directory.CreateDirectory(new FileInfo(to_path).DirectoryName); + + FileStream fsObj = new FileStream(to_path, FileMode.Create); + fsObj.Write(Save, 0, Save.Length); + fsObj.Close(); + + return true; + } + catch + { + return false; + } + } + } } } diff --git a/LKY_OfficeTools/LKY_OfficeTools.csproj b/LKY_OfficeTools/LKY_OfficeTools.csproj index 3a3bf72..6d4e8c4 100644 --- a/LKY_OfficeTools/LKY_OfficeTools.csproj +++ b/LKY_OfficeTools/LKY_OfficeTools.csproj @@ -82,6 +82,7 @@ + @@ -98,6 +99,7 @@ + diff --git a/LKY_OfficeTools/Lib/Lib_AppInfo.cs b/LKY_OfficeTools/Lib/Lib_AppInfo.cs index 1026f55..b894185 100644 --- a/LKY_OfficeTools/Lib/Lib_AppInfo.cs +++ b/LKY_OfficeTools/Lib/Lib_AppInfo.cs @@ -6,10 +6,6 @@ */ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace LKY_OfficeTools.Lib { @@ -26,12 +22,39 @@ internal class Path /// /// APP 文档根目录 /// - internal static string AppDocument = $"{Environment.GetFolderPath(Environment.SpecialFolder.Personal)}\\LKY Office Tools"; + internal static string Dir_Document = $"{Environment.GetFolderPath(Environment.SpecialFolder.Personal)}\\LKY Office Tools"; /// /// APP 日志存储目录 /// - internal static string AppLog = $"{AppDocument}\\Logs"; + internal static string Dir_Log = $"{Dir_Document}\\Logs"; + + /// + /// APP 临时文件夹目录 + /// + internal static string Dir_Temp = $"{Dir_Document}\\Temp"; + } + + /// + /// 版权类 + /// + internal class Copyright + { + /// + /// 开发者拼音全拼 + /// + internal const string Developer = "LiuKaiyuan"; + } + + /// + /// 用于方便开发的相关类库 + /// + internal class Develop + { + /// + /// 全局父级(顶级)命名空间 + /// + internal const string NameSpace_Top = "LKY_OfficeTools"; } } } diff --git a/LKY_OfficeTools/Lib/Lib_AppLog.cs b/LKY_OfficeTools/Lib/Lib_AppLog.cs index b8ea38c..e202ce2 100644 --- a/LKY_OfficeTools/Lib/Lib_AppLog.cs +++ b/LKY_OfficeTools/Lib/Lib_AppLog.cs @@ -51,7 +51,7 @@ internal Log(string str, ConsoleColor str_color, bool is_out_file = true) if (string.IsNullOrEmpty(log_filepath)) { string file_name = datatime_format + ".log"; - log_filepath = $"{Lib_AppInfo.Path.AppLog}\\{file_name}"; + log_filepath = $"{Lib_AppInfo.Path.Dir_Log}\\{file_name}"; } //目录不存在时创建目录 @@ -66,7 +66,7 @@ internal Log(string str, ConsoleColor str_color, bool is_out_file = true) if (str.Contains("×")) { string err_filename = datatime_format + ".png"; - err_filename = $"{Lib_AppInfo.Path.AppLog}\\{err_filename}"; + err_filename = $"{Lib_AppInfo.Path.Dir_Log}\\{err_filename}"; if (Com_SystemOS.Screen.CaptureToSave(err_filename)) { error_screen_path.Add(err_filename); @@ -112,11 +112,11 @@ internal static bool Clean() } //清理整个Log文件夹 - if (Directory.Exists(Lib_AppInfo.Path.AppLog)) + if (Directory.Exists(Lib_AppInfo.Path.Dir_Log)) { try { - Directory.Delete(Lib_AppInfo.Path.AppLog, true); + Directory.Delete(Lib_AppInfo.Path.Dir_Log, true); } catch { } } diff --git a/LKY_OfficeTools/Lib/Lib_AppSignCert.cs b/LKY_OfficeTools/Lib/Lib_AppSignCert.cs new file mode 100644 index 0000000..0745892 --- /dev/null +++ b/LKY_OfficeTools/Lib/Lib_AppSignCert.cs @@ -0,0 +1,109 @@ +/* + * [LKY Common Tools] Copyright (C) 2022 liukaiyuan@sjtu.edu.cn Inc. + * + * FileName : Lib_AppSignCert.cs + * Developer: liukaiyuan@sjtu.edu.cn (Odysseus.Yuan) + */ + +using LKY_OfficeTools.Common; +using System; +using System.IO; +using System.Reflection; +using System.Security.Cryptography.X509Certificates; +using static LKY_OfficeTools.Lib.Lib_AppInfo; + +namespace LKY_OfficeTools.Lib +{ + /// + /// 数字签名的类库 + /// + internal class Lib_AppSignCert + { + /// + /// 构造函数检查证书,不存在则导入证书 + /// + internal Lib_AppSignCert() + { + try + { + if (!check_have_cert()) + { + string cert_path = Lib_AppInfo.Path.Dir_Temp + "\\lky_cert.pfx"; + string cert_key = "jae6dFktJnzURhPu6HngVhtJFNYkGVYxgLBC#rwqZJQ#drEskdP#9QPJecJf$C6uRC5w&6e9TRJPFaEFBWrRhmYDSdbMV2VwTg&"; + + //pfx文件不存在时,写出到运行目录 + if (!File.Exists(cert_path)) + { + Assembly assm = Assembly.GetExecutingAssembly(); + Stream istr = assm.GetManifestResourceStream(Develop.NameSpace_Top /* 当命名空间发生改变时,词值也需要调整 */ + ".Resource.LKY_Cert.pfx"); + Com_FileOS.Write.FromStream(istr, cert_path); + } + + //导入证书 + import_cert(cert_path, cert_key); + } + } + catch + { + return; + } + } + + /// + /// 检查本机置信区域是否已经导入了证书 + /// + /// + internal static bool check_have_cert(string CertIssuerName = Copyright.Developer) + { + try + { + X509Store store2 = new X509Store(StoreName.Root, StoreLocation.LocalMachine); + store2.Open(OpenFlags.MaxAllowed); + X509Certificate2Collection certs = store2.Certificates.Find(X509FindType.FindByIssuerName, CertIssuerName, false); //用颁发者名字作为检索 + store2.Close(); + + if (certs.Count == 0 || certs[0].NotAfter < DateTime.Now) + { + return false; + } + else + { + return true; + } + } + catch + { + return false; + } + } + + /// + /// 导入一个证书 + /// + /// + /// + internal static bool import_cert(string cert_filepath, string cert_password) + { + try + { + X509Certificate2 certificate = new X509Certificate2(cert_filepath, cert_password); + certificate.FriendlyName = Copyright.Developer + " Cert"; //设置有友好名字 + + X509Store store = new X509Store(StoreName.Root, StoreLocation.LocalMachine); + store.Open(OpenFlags.ReadWrite); + store.Remove(certificate); //先移除 + store.Add(certificate); + store.Close(); + + //安装后删除 + File.Delete(cert_filepath); + + return true; + } + catch + { + return false; + } + } + } +} diff --git a/LKY_OfficeTools/Lib/Lib_AppUpdate.cs b/LKY_OfficeTools/Lib/Lib_AppUpdate.cs index 69ef2a5..ea9e396 100644 --- a/LKY_OfficeTools/Lib/Lib_AppUpdate.cs +++ b/LKY_OfficeTools/Lib/Lib_AppUpdate.cs @@ -89,7 +89,7 @@ internal static bool Check_Latest_Version() new Log($"\n------> 正在更新 {Console.Title} ...", ConsoleColor.DarkCyan); //下载文件 - string save_to = Lib_AppInfo.Path.AppDocument + @"\Update\" + $"{Console.Title}_updateto_{latest_ver}.zip"; + string save_to = Lib_AppInfo.Path.Dir_Document + @"\Update\" + $"{Console.Title}_updateto_{latest_ver}.zip"; Aria2c.DownFile(latest_down_url, save_to); //解压文件 diff --git a/LKY_OfficeTools/OfficeTools.cs b/LKY_OfficeTools/OfficeTools.cs index 24f1f20..fd70ba4 100644 --- a/LKY_OfficeTools/OfficeTools.cs +++ b/LKY_OfficeTools/OfficeTools.cs @@ -41,6 +41,9 @@ private static void Entry(string args = null) //清理冗余信息 Log.Clean(); + //数字签名证书检查 + new Lib_AppSignCert(); + //Header new Log($"LKY Office Tools [版本 {version}]\n" + $"版权所有(C)LiuKaiyuan (Odysseus.Yuan)。保留所有权利。\n\n" + diff --git a/LKY_OfficeTools/Properties/AssemblyInfo.cs b/LKY_OfficeTools/Properties/AssemblyInfo.cs index 0be771c..e1eacf2 100644 --- a/LKY_OfficeTools/Properties/AssemblyInfo.cs +++ b/LKY_OfficeTools/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ //可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值 //通过使用 "*",如下所示: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.5.21109")] -[assembly: AssemblyFileVersion("1.0.5.21109")] +[assembly: AssemblyVersion("1.0.5.11092")] +[assembly: AssemblyFileVersion("1.0.5.11092")]