WXWorkspace.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Security.Cryptography;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using WechatBakTool.Helpers;
  12. using WechatBakTool.Model;
  13. namespace WechatBakTool
  14. {
  15. public class WXWorkspace
  16. {
  17. private UserBakConfig UserBakConfig = new UserBakConfig();
  18. public WXWorkspace(string path,string account = "") {
  19. string checkResult = Init(path, account);
  20. if (checkResult != "")
  21. new Exception(checkResult);
  22. }
  23. public WXWorkspace(UserBakConfig userBakConfig)
  24. {
  25. UserBakConfig = userBakConfig;
  26. }
  27. public void DecryptDB(string pid,int type)
  28. {
  29. if (UserBakConfig == null)
  30. {
  31. throw new Exception("没有工作区文件,无法解密");
  32. }
  33. if (!UserBakConfig.Decrypt)
  34. {
  35. byte[]? key = null;
  36. key = DecryptionHelper.GetWechatKey(pid, type == 2, UserBakConfig.Account);
  37. if (key == null)
  38. {
  39. throw new Exception("获取到的密钥为空,获取失败");
  40. }
  41. string key_string = BitConverter.ToString(key, 0).Replace("-", string.Empty).ToLower().ToUpper();
  42. string source = Path.Combine(UserBakConfig.UserWorkspacePath, "OriginalDB");
  43. string to = Path.Combine(UserBakConfig.UserWorkspacePath, "DecDB");
  44. WechatDBHelper.DecryUserData(key, source, to);
  45. UserBakConfig.Decrypt = true;
  46. SaveConfig(UserBakConfig);
  47. }
  48. }
  49. public void MoveDB()
  50. {
  51. string sourceBase = Path.Combine(UserBakConfig.UserResPath, "Msg");
  52. string sourceMulit = Path.Combine(UserBakConfig.UserResPath, "Msg/Multi");
  53. string[] files = Directory.GetFiles(sourceBase);
  54. foreach (string file in files)
  55. {
  56. FileInfo fileInfo = new FileInfo(file);
  57. if(fileInfo.Extension == ".db")
  58. {
  59. string to_path = Path.Combine(UserBakConfig.UserWorkspacePath, "OriginalDB", fileInfo.Name);
  60. File.Copy(file, to_path, true);
  61. }
  62. }
  63. files = Directory.GetFiles(sourceMulit);
  64. foreach (string file in files)
  65. {
  66. FileInfo fileInfo = new FileInfo(file);
  67. if (fileInfo.Extension == ".db")
  68. {
  69. string to_path = Path.Combine(UserBakConfig.UserWorkspacePath, "OriginalDB", fileInfo.Name);
  70. File.Copy(file, to_path, true);
  71. }
  72. }
  73. }
  74. public UserBakConfig ReturnConfig()
  75. {
  76. return UserBakConfig;
  77. }
  78. public static void SaveConfig(UserBakConfig userBakConfig)
  79. {
  80. if(userBakConfig.UserWorkspacePath != "")
  81. {
  82. DirectoryInfo directoryInfo = new DirectoryInfo(userBakConfig.UserWorkspacePath);
  83. if(directoryInfo.Parent != null)
  84. {
  85. string json_path = Path.Combine(directoryInfo.Parent.FullName, userBakConfig.UserName + ".json");
  86. string json = JsonConvert.SerializeObject(userBakConfig);
  87. File.WriteAllText(json_path, json);
  88. }
  89. }
  90. }
  91. private string Init(string path,string account = "")
  92. {
  93. string curPath = AppDomain.CurrentDomain.BaseDirectory;
  94. string md5 = GetMd5Hash(path);
  95. string[] paths = path.Split(new string[] { "/", "\\" }, StringSplitOptions.None);
  96. string username = paths[paths.Length - 1];
  97. UserBakConfig.UserResPath = path;
  98. UserBakConfig.UserWorkspacePath = Path.Combine(curPath, "workspace", md5);
  99. UserBakConfig.Hash = md5;
  100. UserBakConfig.UserName = username;
  101. UserBakConfig.Account = account;
  102. if (!Directory.Exists(UserBakConfig.UserResPath))
  103. {
  104. return "用户资源文件夹不存在,如需使用离线数据,请从工作区读取";
  105. }
  106. if (!Directory.Exists(UserBakConfig.UserWorkspacePath))
  107. {
  108. Directory.CreateDirectory(UserBakConfig.UserWorkspacePath);
  109. }
  110. string db = Path.Combine(UserBakConfig.UserWorkspacePath, "OriginalDB");
  111. string decDb = Path.Combine(UserBakConfig.UserWorkspacePath, "DecDB");
  112. if (!Directory.Exists(db))
  113. {
  114. Directory.CreateDirectory (db);
  115. }
  116. if (!Directory.Exists(decDb))
  117. {
  118. Directory.CreateDirectory(decDb);
  119. }
  120. SaveConfig(UserBakConfig);
  121. return "";
  122. }
  123. private static string GetMd5Hash(string input)
  124. {
  125. using (MD5 md5Hash = MD5.Create())
  126. {
  127. // Convert the input string to a byte array and compute the hash.
  128. byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
  129. // Create a new Stringbuilder to collect the bytes
  130. // and create a string.
  131. StringBuilder sBuilder = new StringBuilder();
  132. // Loop through each byte of the hashed data
  133. // and format each one as a hexadecimal string.
  134. for (int i = 0; i < data.Length; i++)
  135. {
  136. sBuilder.Append(data[i].ToString("x2"));
  137. }
  138. // Return the hexadecimal string.
  139. return sBuilder.ToString();
  140. }
  141. }
  142. }
  143. }