gpt4 book ai didi

c# - 动态解密引用的 dll

转载 作者:太空狗 更新时间:2023-10-30 01:24:39 25 4
gpt4 key购买 nike

我想在我的 Windows 窗体应用程序项目中有一个引用的 dll。对我来说,必须首先对 dll 进行加密,然后在需要使用时在运行时解密。

请考虑以下示例,其中子例程最初是加密的,但随后被解密和实例化。请注意,这只是一个概念性想法,我不知道如何在代码方面完成这件事。

Class clsSO
{
void myVoid()
{
Console.WriteLine("We are here now ...");
}
} // End class

上面的代码将被包装在一个 .dll 中,并作为引用的 dll 添加到我的项目中。然后将引用 dll 并调用子例程:

clsSo myRef = new clsSo();

myRef.myVoid();

控制台输出读取:

We are here now...

我需要做什么:包装器 dll 的内容将被加密,因此不可读/无法直接引用该类。因此,dll 将需要以某种方式解密,并使用解密后的数据动态更新,以便我随后可以引用它。

这样的东西已经存在了吗?甚至可以做到吗?

我感谢大家的时间!

谢谢,

埃文

最佳答案

您需要调用 Assembly.Load(Byte[])在文件的解密字节上。一旦你有了它,你将需要使用反射或将类转换为接口(interface),以便你可以访问这些方法。

举个例子

class Example
{
static void Main(string[] args)
{
byte[] decryptedLibArray;
using (var fs = new FileStream("clsSo.cDll", FileMode.Open, FileAccess.Read))
using (var aesProvider = new AesCryptoServiceProvider())
using (var aesTransform = aesProvider.CreateDecryptor(YourKey, YourIV))
using (var cryptoStream = new CryptoStream(fs, aesTransform, CryptoStreamMode.Read))
{
decryptedLibArray = ReadFully(cryptoStream);
}

var lib = Assembly.Load(decryptedLibArray);

IclsSO classInstance = (IclsSO)lib.CreateInstance("clsSO"); //If you use a namespace this will be namespace.clsSO

classInstance.myVoid();

}

/// <summary>
/// Reads data from a stream until the end is reached. The
/// data is returned as a byte array. An IOException is
/// thrown if any of the underlying IO calls fail.
/// </summary>
/// <param name="stream">The stream to read data from</param>
public static byte[] ReadFully (Stream stream)
{
byte[] buffer = new byte[32768];
using (MemoryStream ms = new MemoryStream())
{
while (true)
{
int read = stream.Read (buffer, 0, buffer.Length);
if (read <= 0)
return ms.ToArray();
ms.Write (buffer, 0, read);
}
}
}
}

interface IclsSO
{
void myVoid();
}

ReadFully来自this posting .阅读它以了解为什么我不只是做 cryptoStream.Read(data, 0, decryptedLibArray.Length);最后一个例子有一些错误,但他给出的内存流例子是完美的。

然而,有人可能只是转储您的程序的 ram 并获得解密的二进制文件,或者可能反编译您的主程序并获得解密 key 并自行解密。因此,根据您的偏执程度,您可能仍想投资混淆器。

关于c# - 动态解密引用的 dll,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8759622/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com