5 Star 20 Fork 14

Gitee 极速下载/WechatBakTool

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/SuxueCode/WechatBakTool
克隆/下载
WXWorkspace.cs 6.96 KB
一键复制 编辑 原始数据 按行查看 历史
Suxue 提交于 2024-03-26 23:48 . v0.9.7.3 Release!
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();
}
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/mirrors/WechatBakTool.git
[email protected]:mirrors/WechatBakTool.git
mirrors
WechatBakTool
WechatBakTool
master

搜索帮助