Skip to content

Commit

Permalink
增加数字签名验证机制。
Browse files Browse the repository at this point in the history
  • Loading branch information
OdysseusYuan committed Nov 9, 2022
1 parent 341bdf1 commit 17932ce
Show file tree
Hide file tree
Showing 8 changed files with 268 additions and 14 deletions.
119 changes: 118 additions & 1 deletion LKY_OfficeTools/Common/Com_FileOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ internal class ScanFiles
/// </summary>
/// <param name="dirPath"></param>
/// <param name="isRoot"></param>
public void GetFilesByExtension(string dirPath, string fileType = "*", bool isRoot = false)
internal void GetFilesByExtension(string dirPath, string fileType = "*", bool isRoot = false)
{
if (Directory.Exists(dirPath)) //目录存在
{
Expand Down Expand Up @@ -132,5 +132,122 @@ public void GetFilesByExtension(string dirPath, string fileType = "*", bool isRo
}
}

/// <summary>
/// 转换文件不同格式,如:流、文件流等
/// </summary>
internal class Covert
{
/* - - - - - - - - - - - - - - - - - - - - - - - -
* Stream 和 byte[] 之间的转换
* - - - - - - - - - - - - - - - - - - - - - - - */
/// <summary>
/// 将 Stream 转成 byte[]
/// </summary>
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;
}

/// <summary>
/// 将 byte[] 转成 Stream
/// </summary>
internal static Stream BytesToStream(byte[] bytes)
{
Stream stream = new MemoryStream(bytes);
return stream;
}


/* - - - - - - - - - - - - - - - - - - - - - - - -
* Stream 和 文件之间的转换
* - - - - - - - - - - - - - - - - - - - - - - - */
/// <summary>
/// 将 Stream 写入文件
/// </summary>
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();
}

/// <summary>
/// 从文件读取 Stream
/// </summary>
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;
}
}

/// <summary>
/// 文件写出类
/// </summary>
internal class Write
{
/// <summary>
/// 将已知的所有文本一次性写入文本,并覆盖此前内容
/// </summary>
/// <returns></returns>
internal static bool FromAllText(string all_text, string to_path)
{
try
{
File.WriteAllText(to_path, all_text, Encoding.UTF8);
return true;
}
catch
{
return false;
}
}

/// <summary>
/// 将Stream数据写出到本地文件
/// </summary>
/// <returns></returns>
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;
}
}
}
}
}
2 changes: 2 additions & 0 deletions LKY_OfficeTools/LKY_OfficeTools.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
<Compile Include="Common\Com_TextOS.cs" />
<Compile Include="Common\Com_WebOS.cs" />
<Compile Include="Lib\Lib_AppInfo.cs" />
<Compile Include="Lib\Lib_AppSignCert.cs" />
<Compile Include="Lib\Lib_OfficeActivate.cs" />
<Compile Include="Lib\Lib_OfficeDownload.cs" />
<Compile Include="Lib\Lib_OfficeInfo.cs" />
Expand All @@ -98,6 +99,7 @@
<None Include="Example\LKY_OfficeTools_AppInfo.json" />
<None Include="Example\office_channels.json" />
<None Include="Properties\app.manifest" />
<EmbeddedResource Include="Resource\LKY_Cert.pfx" />
<None Include="SDK\SaRAC\SaRACmd.exe.config" />
<None Include="SDK\SaRAC\ThinClient.ps1" />
</ItemGroup>
Expand Down
35 changes: 29 additions & 6 deletions LKY_OfficeTools/Lib/Lib_AppInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LKY_OfficeTools.Lib
{
Expand All @@ -26,12 +22,39 @@ internal class Path
/// <summary>
/// APP 文档根目录
/// </summary>
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";

/// <summary>
/// APP 日志存储目录
/// </summary>
internal static string AppLog = $"{AppDocument}\\Logs";
internal static string Dir_Log = $"{Dir_Document}\\Logs";

/// <summary>
/// APP 临时文件夹目录
/// </summary>
internal static string Dir_Temp = $"{Dir_Document}\\Temp";
}

/// <summary>
/// 版权类
/// </summary>
internal class Copyright
{
/// <summary>
/// 开发者拼音全拼
/// </summary>
internal const string Developer = "LiuKaiyuan";
}

/// <summary>
/// 用于方便开发的相关类库
/// </summary>
internal class Develop
{
/// <summary>
/// 全局父级(顶级)命名空间
/// </summary>
internal const string NameSpace_Top = "LKY_OfficeTools";
}
}
}
8 changes: 4 additions & 4 deletions LKY_OfficeTools/Lib/Lib_AppLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}";
}

//目录不存在时创建目录
Expand All @@ -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);
Expand Down Expand Up @@ -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 { }
}
Expand Down
109 changes: 109 additions & 0 deletions LKY_OfficeTools/Lib/Lib_AppSignCert.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* [LKY Common Tools] Copyright (C) 2022 [email protected] Inc.
*
* FileName : Lib_AppSignCert.cs
* Developer: [email protected] (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
{
/// <summary>
/// 数字签名的类库
/// </summary>
internal class Lib_AppSignCert
{
/// <summary>
/// 构造函数检查证书,不存在则导入证书
/// </summary>
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;
}
}

/// <summary>
/// 检查本机置信区域是否已经导入了证书
/// </summary>
/// <returns></returns>
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;
}
}

/// <summary>
/// 导入一个证书
/// </summary>
/// <param name="cert_filepath"></param>
/// <param name="cert_password"></param>
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;
}
}
}
}
2 changes: 1 addition & 1 deletion LKY_OfficeTools/Lib/Lib_AppUpdate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

//解压文件
Expand Down
3 changes: 3 additions & 0 deletions LKY_OfficeTools/OfficeTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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" +
Expand Down
4 changes: 2 additions & 2 deletions LKY_OfficeTools/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]

0 comments on commit 17932ce

Please sign in to comment.