WXWorkspace.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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.Pages;
  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)
  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);
  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()
  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. string to_path = Path.Combine(UserBakConfig.UserWorkspacePath, "OriginalDB", fileInfo.Name);
  65. File.Copy(file, to_path, true);
  66. }
  67. }
  68. files = Directory.GetFiles(sourceMulit);
  69. foreach (string file in files)
  70. {
  71. FileInfo fileInfo = new FileInfo(file);
  72. if (fileInfo.Extension == ".db")
  73. {
  74. string to_path = Path.Combine(UserBakConfig.UserWorkspacePath, "OriginalDB", fileInfo.Name);
  75. File.Copy(file, to_path, true);
  76. }
  77. }
  78. }
  79. public UserBakConfig ReturnConfig()
  80. {
  81. return UserBakConfig;
  82. }
  83. public static void SaveConfig(UserBakConfig userBakConfig)
  84. {
  85. if(userBakConfig.UserWorkspacePath != "")
  86. {
  87. DirectoryInfo directoryInfo = new DirectoryInfo(userBakConfig.UserWorkspacePath);
  88. if(directoryInfo.Parent != null)
  89. {
  90. string json_path = Path.Combine(directoryInfo.Parent.FullName, userBakConfig.UserName + ".json");
  91. string json = JsonConvert.SerializeObject(userBakConfig);
  92. File.WriteAllText(json_path, json);
  93. }
  94. }
  95. }
  96. private string Init(string path,string account = "")
  97. {
  98. string curPath = AppDomain.CurrentDomain.BaseDirectory;
  99. string md5 = GetMd5Hash(path);
  100. string[] paths = path.Split(new string[] { "/", "\\" }, StringSplitOptions.None);
  101. string username = paths[paths.Length - 1];
  102. UserBakConfig.UserResPath = path;
  103. UserBakConfig.UserWorkspacePath = Path.Combine(curPath, "workspace", md5);
  104. UserBakConfig.Hash = md5;
  105. UserBakConfig.UserName = username;
  106. UserBakConfig.Account = account;
  107. if (!Directory.Exists(UserBakConfig.UserResPath))
  108. {
  109. return "用户资源文件夹不存在,如需使用离线数据,请从工作区读取";
  110. }
  111. if (!Directory.Exists(UserBakConfig.UserWorkspacePath))
  112. {
  113. Directory.CreateDirectory(UserBakConfig.UserWorkspacePath);
  114. }
  115. string db = Path.Combine(UserBakConfig.UserWorkspacePath, "OriginalDB");
  116. string decDb = Path.Combine(UserBakConfig.UserWorkspacePath, "DecDB");
  117. if (!Directory.Exists(db))
  118. {
  119. Directory.CreateDirectory (db);
  120. }
  121. if (!Directory.Exists(decDb))
  122. {
  123. Directory.CreateDirectory(decDb);
  124. }
  125. SaveConfig(UserBakConfig);
  126. return "";
  127. }
  128. private static string GetMd5Hash(string input)
  129. {
  130. using (MD5 md5Hash = MD5.Create())
  131. {
  132. // Convert the input string to a byte array and compute the hash.
  133. byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
  134. // Create a new Stringbuilder to collect the bytes
  135. // and create a string.
  136. StringBuilder sBuilder = new StringBuilder();
  137. // Loop through each byte of the hashed data
  138. // and format each one as a hexadecimal string.
  139. for (int i = 0; i < data.Length; i++)
  140. {
  141. sBuilder.Append(data[i].ToString("x2"));
  142. }
  143. // Return the hexadecimal string.
  144. return sBuilder.ToString();
  145. }
  146. }
  147. }
  148. }