动态解密引用的dll

本文关键字:dll 引用 解密 动态 | 更新日期: 2023-09-27 18:19:42

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

请考虑下面的例子,其中子例程最初是加密的,但后来解密并实例化。注意,这只是一个概念性的想法,我不知道在代码方面需要如何做到这一点。

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

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

clsSo myRef = new clsSo();
myRef.myVoid();

控制台输出读取:

我们现在在这里。。。

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

这样的东西已经存在了吗?能做到吗?

我感谢大家抽出时间!

谢谢,

Evan

动态解密引用的dll

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

以下是的示例

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来自此发布。读一读,看看我为什么不只是做cryptoStream.Read(data, 0, decryptedLibArray.Length);最后的例子有一些错误,但他给出的内存流例子是完美的。

然而,有人可以转储你程序的ram并获得解密的二进制文件,或者可能反编译你的主程序并获得解密密钥,然后自己解密。因此,根据你的偏执程度,你可能仍然想投资一个模糊理论。