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