c#如何實(shí)現(xiàn)程序加密隱藏
LiteDB
LiteDB是一個(gè)輕量級(jí)的嵌入式數(shù)據(jù)庫(kù),它是用C#編寫的,適用于.NET平臺(tái)。它的設(shè)計(jì)目標(biāo)是提供一個(gè)簡(jiǎn)單易用的數(shù)據(jù)庫(kù)解決方案,可以在各種應(yīng)用程序中使用。
LiteDB使用單個(gè)文件作為數(shù)據(jù)庫(kù)存儲(chǔ),這個(gè)文件可以在磁盤上或內(nèi)存中。它支持文檔存儲(chǔ)模型,類似于NoSQL數(shù)據(jù)庫(kù),每個(gè)文檔都是一個(gè)JSON格式的對(duì)象。這意味著你可以存儲(chǔ)和檢索任意類型的數(shù)據(jù),而不需要預(yù)定義模式。
LiteDB提供了一組簡(jiǎn)單的API來執(zhí)行各種數(shù)據(jù)庫(kù)操作,包括插入、更新、刪除和查詢。它還支持事務(wù),可以確保數(shù)據(jù)的一致性和完整性。
LiteDB還提供了一些高級(jí)功能,如索引、全文搜索和文件存儲(chǔ)。索引可以加快查詢的速度,全文搜索可以在文本數(shù)據(jù)中進(jìn)行關(guān)鍵字搜索,文件存儲(chǔ)可以將文件直接存儲(chǔ)在數(shù)據(jù)庫(kù)中。
LiteDB的優(yōu)點(diǎn)包括易于使用、輕量級(jí)、快速和可嵌入性。它的代碼庫(kù)非常小,可以很容易地集成到你的應(yīng)用程序中。此外,它還具有跨平臺(tái)的能力,可以在Windows、Linux和Mac等操作系統(tǒng)上運(yùn)行。
總之,LiteDB是一個(gè)簡(jiǎn)單易用的嵌入式數(shù)據(jù)庫(kù),適用于各種應(yīng)用程序。它提供了一組簡(jiǎn)單的API來執(zhí)行數(shù)據(jù)庫(kù)操作,并支持一些高級(jí)功能。如果你需要一個(gè)輕量級(jí)的數(shù)據(jù)庫(kù)解決方案,可以考慮使用LiteDB。
加密封裝
創(chuàng)建LiteDB.Service
的WebApi項(xiàng)目。
右鍵發(fā)布:
創(chuàng)建控制臺(tái)LiteDB.Launch
項(xiàng)目。
EntryPointDiscoverer.cs
用于尋找執(zhí)行方法。
internal class EntryPointDiscoverer { public static MethodInfo FindStaticEntryMethod(Assembly assembly, string? entryPointFullTypeName = null) { var candidates = new List<MethodInfo>(); if (!string.IsNullOrWhiteSpace(entryPointFullTypeName)) { var typeInfo = assembly.GetType(entryPointFullTypeName, false, false)?.GetTypeInfo(); if (typeInfo == null) { throw new InvalidProgramException($"Could not find '{entryPointFullTypeName}' specified for Main method. See <StartupObject> project property."); } FindMainMethodCandidates(typeInfo, candidates); } else { foreach (var type in assembly .DefinedTypes .Where(t => t.IsClass) .Where(t => t.GetCustomAttribute<CompilerGeneratedAttribute>() is null)) { FindMainMethodCandidates(type, candidates); } } string MainMethodFullName() { return string.IsNullOrWhiteSpace(entryPointFullTypeName) ? "Main" : $"{entryPointFullTypeName}.Main"; } if (candidates.Count > 1) { throw new AmbiguousMatchException( $"Ambiguous entry point. Found multiple static functions named '{MainMethodFullName()}'. Could not identify which method is the main entry point for this function."); } if (candidates.Count == 0) { throw new InvalidProgramException( $"Could not find a static entry point '{MainMethodFullName()}'."); } return candidates[0]; } private static void FindMainMethodCandidates(TypeInfo type, List<MethodInfo> candidates) { foreach (var method in type .GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic) .Where(m => string.Equals("Main", m.Name, StringComparison.OrdinalIgnoreCase))) { if (method.ReturnType == typeof(void) || method.ReturnType == typeof(int) || method.ReturnType == typeof(Task) || method.ReturnType == typeof(Task<int>)) { candidates.Add(method); } } } }
然后打開Program.cs
文件,請(qǐng)注意修改SaveDb的參數(shù)為自己項(xiàng)目打包的地址
// 用于打包指定程序。 SaveDb(@"E:\Project\LiteDB-Application\LiteDB.Service\bin\Release\net7.0\publish"); // 打開db var db = new LiteDatabase("Launch.db"); // 打開表。 var files = db.GetCollection<FileAssembly>("files"); // 接管未找到的程序集處理 AppDomain.CurrentDomain.AssemblyResolve += (sender, eventArgs) => { try { var name = eventArgs.Name.Split(",")[0]; if (!name.EndsWith(".dll")) { name += ".dll"; } var file = files.FindOne(x => x.Name == name); return file != null ? Assembly.Load(file.Bytes) : default; } catch (Exception) { return default; } }; // 啟動(dòng)程序。 StartServer("LiteDB.Service", new string[] { }); Console.ReadKey(); void StartServer(string assemblyName, string[] args) { var value = files!.FindOne(x => x.Name == assemblyName + ".dll"); var assembly = Assembly.Load(value!.Bytes); var entryPoint = EntryPointDiscoverer.FindStaticEntryMethod(assembly); try { var parameters = entryPoint.GetParameters(); if (parameters.Length != 0) { var parameterValues = parameters.Select(p => p.ParameterType.IsValueType ? Activator.CreateInstance(p.ParameterType) : null) .ToArray(); entryPoint.Invoke(null, parameterValues); } else { entryPoint.Invoke(null, null); } } catch (Exception e) { } } // 掃描指定目錄下所有文件和子目錄,保存到LiteDB數(shù)據(jù)庫(kù)中。 void SaveDb(string path) { var files = ScanDirectory(path); using var db = new LiteDatabase("Launch.db"); var col = db.GetCollection<FileAssembly>("files"); col.InsertBulk(files); } // 實(shí)現(xiàn)一個(gè)方法,掃描指定目錄下所有文件和子目錄,返回一個(gè)集合。 List<FileAssembly> ScanDirectory(string path) { var files = new List<FileAssembly>(); var dir = new DirectoryInfo(path); var fileInfos = dir.GetFiles("*", SearchOption.AllDirectories); foreach (var fileInfo in fileInfos) { var file = new FileAssembly { Name = fileInfo.Name, Bytes = File.ReadAllBytes(fileInfo.FullName) }; files.Add(file); } return files; } class FileAssembly { /// <summary> /// 文件名 /// </summary> public string Name { get; set; } /// <summary> /// 文件的內(nèi)容 /// </summary> public byte[] Bytes { get; set; } }
點(diǎn)擊LiteDB.Launch
項(xiàng)目文件,添加LiteDB依賴,并且修改SDK為Microsoft.NET.Sdk.Web
<Project Sdk="Microsoft.NET.Sdk.Web"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net7.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> </PropertyGroup> <ItemGroup> <PackageReference Include="LiteDB" Version="5.0.17" /> </ItemGroup> </Project>
在啟動(dòng)項(xiàng)目的時(shí)候先將LiteDB.Service
發(fā)布一下。然后修改SaveDb
參數(shù)為發(fā)布的目錄(會(huì)自動(dòng)掃描所有文件打包到LiteDB的文件中。)
然后啟動(dòng)項(xiàng)目;
當(dāng)我們啟動(dòng)了LiteDB.Launch
以后在StartServer
方法里面就會(huì)打開創(chuàng)建的LiteDB文件中搜索到指定的啟動(dòng)程序集。
然后在AppDomain.CurrentDomain.AssemblyResolve
中會(huì)將啟動(dòng)程序集缺少的程序集加載到域中。
AppDomain.CurrentDomain.AssemblyResolve
會(huì)在未找到依賴的時(shí)候觸發(fā)的一個(gè)事件。
在存儲(chǔ)到LiteDB的時(shí)候可以對(duì)于存儲(chǔ)的內(nèi)容進(jìn)行加密,然后在AppDomain.CurrentDomain.AssemblyResolve
觸發(fā)的時(shí)候?qū)⒆x取LiteDB的文件的內(nèi)容的時(shí)候進(jìn)行解密。
到此這篇關(guān)于c#如何實(shí)現(xiàn)程序加密隱藏的文章就介紹到這了,更多相關(guān)c#程序加密隱藏內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
VsCode使用EmmyLua插件調(diào)試Unity工程Lua代碼的詳細(xì)步驟
這篇文章主要介紹了VsCode使用EmmyLua插件調(diào)試Unity工程Lua代碼,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08c#一個(gè)定時(shí)重啟的小程序?qū)崿F(xiàn)代碼
今天有個(gè)朋友找我問有沒有一些能像Windows一樣計(jì)劃任務(wù)重啟的軟件,我也不清楚。他它說能讓我做一個(gè)給他它么?我考慮了一下,他的服務(wù)器都是有安裝.NET框架的,那可以用.NET來使下~~!2008-09-09
![字符串陣列String[]轉(zhuǎn)換為整型陣列Int[]的實(shí)例](http://img.jbzj.com/images/xgimg/bcimg5.png)
字符串陣列String[]轉(zhuǎn)換為整型陣列Int[]的實(shí)例

淺談C#手機(jī)號(hào)換成111XXXX1111 這種顯示的解決思路

關(guān)于C#繼承的簡(jiǎn)單應(yīng)用代碼分析