DllHelper.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Security.Cryptography;
  5. using System.Text;
  6. namespace Flyinsono.Client.Test
  7. {
  8. public class DllHelper
  9. {
  10. public static void EncryptDllToBin(IList<string> dllFilePathList)
  11. {
  12. byte[] key = GetKey();
  13. foreach (var dllFilePath in dllFilePathList)
  14. {
  15. if (!File.Exists(dllFilePath))
  16. {
  17. Logger.WriteLineInfo($"{dllFilePath} is not exist");
  18. continue;
  19. }
  20. Logger.WriteLineInfo($"{dllFilePath} start dllEncryption");
  21. // 读取 DLL 文件内容
  22. byte[] dllData = File.ReadAllBytes(dllFilePath);
  23. // 加密 DLL 数据
  24. byte[] encryptedData = EncryptData(dllData, key);
  25. string directoryPath = Path.GetDirectoryName(dllFilePath);
  26. var fileName = Path.GetFileNameWithoutExtension(dllFilePath);
  27. var binFilePath = Path.Combine(directoryPath, $"{fileName}.bin");
  28. var destinationFilePath = System.IO.Path.Combine(directoryPath, binFilePath);
  29. // 保存加密后的数据到二进制文件
  30. File.WriteAllBytes(destinationFilePath, encryptedData);
  31. Logger.WriteLineInfo($"{dllFilePath} save as {binFilePath}");
  32. //删除dll
  33. File.Delete(dllFilePath);
  34. }
  35. Logger.WriteLineInfo($"Dll Encryption Completed");
  36. }
  37. public static byte[] GetDllBytes(string binName)
  38. {
  39. byte[] encryptedData = File.ReadAllBytes(binName);
  40. byte[] key = GetKey();
  41. // 解密数据
  42. byte[] decryptedData = DecryptData(encryptedData, key);
  43. return decryptedData;
  44. }
  45. static byte[] EncryptData(byte[] data, byte[] key)
  46. {
  47. using (Aes aesAlg = Aes.Create())
  48. {
  49. aesAlg.Key = key;
  50. aesAlg.IV = new byte[16]; // 使用默认 IV
  51. // 创建加密器并加密数据
  52. ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
  53. using (MemoryStream msEncrypt = new MemoryStream())
  54. {
  55. using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
  56. {
  57. csEncrypt.Write(data, 0, data.Length);
  58. csEncrypt.FlushFinalBlock();
  59. return msEncrypt.ToArray();
  60. }
  61. }
  62. }
  63. }
  64. static byte[] DecryptData(byte[] data, byte[] key)
  65. {
  66. using (Aes aesAlg = Aes.Create())
  67. {
  68. aesAlg.Key = key;
  69. aesAlg.IV = new byte[16]; // 使用默认 IV
  70. // 创建解密器并解密数据
  71. ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
  72. using (MemoryStream msDecrypt = new MemoryStream(data))
  73. {
  74. using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
  75. {
  76. using (MemoryStream msDecrypted = new MemoryStream())
  77. {
  78. csDecrypt.CopyTo(msDecrypted);
  79. return msDecrypted.ToArray();
  80. }
  81. }
  82. }
  83. }
  84. }
  85. static byte[] GetKey()
  86. {
  87. //todo 读取配置
  88. string inputString = "vinno123";
  89. byte[] key = Encoding.UTF8.GetBytes(inputString);
  90. // 确保密钥长度符合 AES 要求
  91. Array.Resize(ref key, 32); // AES-256 使用 32 字节的密钥
  92. return key;
  93. }
  94. }
  95. }