WXWorkspace.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. using WechatBakTool.ViewModel;
  14. namespace WechatBakTool
  15. {
  16. public class WXWorkspace
  17. {
  18. private UserBakConfig UserBakConfig = new UserBakConfig();
  19. public WXWorkspace(string path,string account = "") {
  20. string checkResult = Init(path, false, account);
  21. if (checkResult != "")
  22. new Exception(checkResult);
  23. }
  24. public WXWorkspace(UserBakConfig userBakConfig)
  25. {
  26. UserBakConfig = userBakConfig;
  27. }
  28. public void DecryptDB(string pid,int type,CreateWorkViewModel viewModel,string pwd = "")
  29. {
  30. if (UserBakConfig == null)
  31. {
  32. throw new Exception("没有工作区文件,无法解密");
  33. }
  34. if (!UserBakConfig.Decrypt)
  35. {
  36. byte[]? key = null;
  37. viewModel.LabelStatus = "正在获取秘钥,需要1 - 10秒左右";
  38. if(pwd == "")
  39. key = DecryptionHelper.GetWechatKey(pid, type, UserBakConfig.Account);
  40. else
  41. {
  42. key = new byte[pwd.Length / 2];
  43. for(int i = 0;i<pwd.Length / 2; i++)
  44. {
  45. key[i] = Convert.ToByte(pwd.Substring(i * 2, 2), 16);
  46. }
  47. }
  48. #if DEBUG
  49. File.WriteAllText("key.log", BitConverter.ToString(key!));
  50. #endif
  51. if (key == null)
  52. {
  53. throw new Exception("获取到的密钥为空,获取失败");
  54. }
  55. string source = Path.Combine(UserBakConfig.UserWorkspacePath, "OriginalDB");
  56. string to = Path.Combine(UserBakConfig.UserWorkspacePath, "DecDB");
  57. DecryptionHelper.DecryUserData(key, source, to, viewModel);
  58. UserBakConfig.Decrypt = true;
  59. WXUserReader reader = new WXUserReader(UserBakConfig);
  60. int[] count = reader.GetWXCount();
  61. UserBakConfig.Friends_Number = count[0].ToString();
  62. UserBakConfig.Msg_Number = count[1].ToString();
  63. SaveConfig(UserBakConfig);
  64. }
  65. }
  66. public void MoveDB(CreateWorkViewModel viewModel)
  67. {
  68. string sourceBase = Path.Combine(UserBakConfig.UserResPath, "Msg");
  69. string sourceMulit = Path.Combine(UserBakConfig.UserResPath, "Msg/Multi");
  70. string[] files = Directory.GetFiles(sourceBase);
  71. foreach (string file in files)
  72. {
  73. FileInfo fileInfo = new FileInfo(file);
  74. if (fileInfo.Extension == ".db")
  75. {
  76. viewModel.LabelStatus = "正在迁移" + fileInfo.Name;
  77. string to_path = Path.Combine(UserBakConfig.UserWorkspacePath, "OriginalDB", fileInfo.Name);
  78. File.Copy(file, to_path, true);
  79. }
  80. }
  81. files = Directory.GetFiles(sourceMulit);
  82. foreach (string file in files)
  83. {
  84. FileInfo fileInfo = new FileInfo(file);
  85. if (fileInfo.Extension == ".db")
  86. {
  87. viewModel.LabelStatus = "正在迁移" + fileInfo.Name;
  88. string to_path = Path.Combine(UserBakConfig.UserWorkspacePath, "OriginalDB", fileInfo.Name);
  89. File.Copy(file, to_path, true);
  90. }
  91. }
  92. }
  93. public UserBakConfig ReturnConfig()
  94. {
  95. return UserBakConfig;
  96. }
  97. public static void SaveConfig(UserBakConfig userBakConfig)
  98. {
  99. if(userBakConfig.UserWorkspacePath != "")
  100. {
  101. DirectoryInfo directoryInfo = new DirectoryInfo(userBakConfig.UserWorkspacePath);
  102. if(directoryInfo.Parent != null)
  103. {
  104. string json_path = Path.Combine(directoryInfo.Parent.FullName, userBakConfig.UserName + ".json");
  105. string json = JsonConvert.SerializeObject(userBakConfig);
  106. File.WriteAllText(json_path, json);
  107. }
  108. }
  109. }
  110. private string Init(string path,bool manual,string account = "")
  111. {
  112. string curPath = AppDomain.CurrentDomain.BaseDirectory;
  113. string md5 = GetMd5Hash(path);
  114. string[] paths = path.Split(new string[] { "/", "\\" }, StringSplitOptions.None);
  115. string username = paths[paths.Length - 1];
  116. UserBakConfig.UserResPath = path;
  117. UserBakConfig.UserWorkspacePath = Path.Combine(curPath, "workspace", md5);
  118. UserBakConfig.Hash = md5;
  119. UserBakConfig.UserName = username;
  120. UserBakConfig.Account = account;
  121. if (!Directory.Exists(UserBakConfig.UserResPath))
  122. {
  123. return "用户资源文件夹不存在,如需使用离线数据,请从工作区读取";
  124. }
  125. if (!Directory.Exists(UserBakConfig.UserWorkspacePath))
  126. {
  127. Directory.CreateDirectory(UserBakConfig.UserWorkspacePath);
  128. }
  129. string db = Path.Combine(UserBakConfig.UserWorkspacePath, "OriginalDB");
  130. string decDb = Path.Combine(UserBakConfig.UserWorkspacePath, "DecDB");
  131. if (!Directory.Exists(db))
  132. {
  133. Directory.CreateDirectory (db);
  134. }
  135. if (!Directory.Exists(decDb))
  136. {
  137. Directory.CreateDirectory(decDb);
  138. }
  139. SaveConfig(UserBakConfig);
  140. return "";
  141. }
  142. private static string GetMd5Hash(string input)
  143. {
  144. using (MD5 md5Hash = MD5.Create())
  145. {
  146. // Convert the input string to a byte array and compute the hash.
  147. byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
  148. // Create a new Stringbuilder to collect the bytes
  149. // and create a string.
  150. StringBuilder sBuilder = new StringBuilder();
  151. // Loop through each byte of the hashed data
  152. // and format each one as a hexadecimal string.
  153. for (int i = 0; i < data.Length; i++)
  154. {
  155. sBuilder.Append(data[i].ToString("x2"));
  156. }
  157. // Return the hexadecimal string.
  158. return sBuilder.ToString();
  159. }
  160. }
  161. }
  162. }