欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

c#如何實(shí)現(xiàn)程序加密隱藏

 更新時(shí)間:2023年08月15日 14:29:10   作者:tokengo  
LiteDB是一個(gè)輕量級(jí)的嵌入式數(shù)據(jù)庫(kù),它是用C#編寫的,適用于.NET平臺(tái),這篇文章主要介紹了如何通過LiteDB將自己的程序進(jì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)文章

  • 字符串陣列String[]轉(zhuǎn)換為整型陣列Int[]的實(shí)例

    字符串陣列String[]轉(zhuǎn)換為整型陣列Int[]的實(shí)例

    下面小編就為大家分享一篇字符串陣列String[]轉(zhuǎn)換為整型陣列Int[]的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2017-12-12
  • 淺談C#手機(jī)號(hào)換成111XXXX1111 這種顯示的解決思路

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

    下面小編就為大家?guī)硪黄獪\談C#手機(jī)號(hào)換成111XXXX1111 這種顯示的解決思路。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-11-11
  • 關(guān)于C#繼承的簡(jiǎn)單應(yīng)用代碼分析

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

    在本篇文章里小編給大家整理了一篇關(guān)于C#繼承的簡(jiǎn)單應(yīng)用代碼分析內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。
    2021-05-05
  • C#實(shí)現(xiàn)搶紅包算法的示例代碼

    C#實(shí)現(xiàn)搶紅包算法的示例代碼

    很多商家都會(huì)使用紅包進(jìn)行促銷,那么你知道紅包算法是怎么實(shí)現(xiàn)的嗎,本文主要介紹了C#實(shí)現(xiàn)搶紅包算法,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • C# Path類---文件路徑解讀

    C# Path類---文件路徑解讀

    這篇文章主要介紹了C# Path類---文件路徑,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • 最新評(píng)論