WXWorkspace.cs 6.5 KB

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