2
0

WXWorkspace.cs 6.2 KB

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