Skip to content

Commit

Permalink
重构工作区,理顺工作区概念。
Browse files Browse the repository at this point in the history
新增微信选择功能,实现多开微信时也能方便选择创建/解密。
UI更新,更贴合现在的想法和概念,更易于理解。
添加3.9.7.25版本信息。
  • Loading branch information
SuxueCode committed Sep 26, 2023
1 parent 77d3d36 commit 03074e7
Show file tree
Hide file tree
Showing 21 changed files with 874 additions and 545 deletions.
2 changes: 1 addition & 1 deletion App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WechatPCMsgBakTool"
StartupUri="MainWindow.xaml">
StartupUri="Main.xaml">
<Application.Resources>

</Application.Resources>
Expand Down
106 changes: 106 additions & 0 deletions Helpers/DecryptionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,112 @@ private static string BytesToHex(byte[] bytes)
{
return BitConverter.ToString(bytes, 0).Replace("-", string.Empty).ToLower().ToUpper();
}
public static byte[] DecImage(string source)
{
//读取数据
byte[] fileBytes = File.ReadAllBytes(source);
//算差异转换
byte key = GetImgKey(fileBytes);
fileBytes = ConvertData(fileBytes, key);
return fileBytes;
}
public static string CheckFileType(byte[] data)
{
switch (data[0])
{
case 0XFF: //byte[] jpg = new byte[] { 0xFF, 0xD8, 0xFF };
{
if (data[1] == 0xD8 && data[2] == 0xFF)
{
return ".jpg";
}
break;
}
case 0x89: //byte[] png = new byte[] { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A };
{
if (data[1] == 0x50 && data[2] == 0x4E && data[7] == 0x0A)
{
return ".png";
}
break;
}
case 0x42: //byte[] bmp = new byte[] { 0x42, 0x4D };
{
if (data[1] == 0X4D)
{
return ".bmp";
}
break;
}
case 0x47: //byte[] gif = new byte[] { 0x47, 0x49, 0x46, 0x38, 0x39(0x37), 0x61 };
{
if (data[1] == 0x49 && data[2] == 0x46 && data[3] == 0x38 && data[5] == 0x61)
{
return ".gif";
}
break;
}
case 0x49: // byte[] tif = new byte[] { 0x49, 0x49, 0x2A, 0x00 };
{
if (data[1] == 0x49 && data[2] == 0x2A && data[3] == 0x00)
{
return ".tif";
}
break;
}
case 0x4D: //byte[] tif = new byte[] { 0x4D, 0x4D, 0x2A, 0x00 };
{
if (data[1] == 0x4D && data[2] == 0x2A && data[3] == 0x00)
{
return ".tif";
}
break;
}
}

return ".dat";
}
private static byte GetImgKey(byte[] fileRaw)
{
byte[] raw = new byte[8];
for (int i = 0; i < 8; i++)
{
raw[i] = fileRaw[i];
}

for (byte key = 0x01; key < 0xFF; key++)
{
byte[] buf = new byte[8];
raw.CopyTo(buf, 0);

if (CheckFileType(ConvertData(buf, key)) != ".dat")
{
return key;
}
}
return 0x00;
}
private static byte[] ConvertData(byte[] data, byte key)
{
for (int i = 0; i < data.Length; i++)
{
data[i] ^= key;
}

return data;
}
public static string SaveDecImage(byte[] fileRaw,string source,string to_dir,string type)
{
FileInfo fileInfo = new FileInfo(source);
string fileName = fileInfo.Name.Substring(0, fileInfo.Name.Length - 4);
string saveFilePath = Path.Combine(to_dir, fileName + type);
using (FileStream fileStream = File.OpenWrite(saveFilePath))
{
fileStream.Write(fileRaw, 0, fileRaw.Length);
fileStream.Flush();
}
return saveFilePath;
}
}

}
13 changes: 13 additions & 0 deletions Helpers/ProcessHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace WechatPCMsgBakTool.Helpers
{
Expand All @@ -15,6 +16,18 @@ public class ProcessHelper
Process[] processes = Process.GetProcessesByName(ProcessName);
if (processes.Length == 0)
return null;
else if(processes.Length > 1) {
SelectWechat selectWechat = new SelectWechat();
MessageBox.Show("检测到有多个微信,请选择本工作区对应的微信");
selectWechat.ShowDialog();
if (selectWechat.SelectProcess == null)
return null;

Process? p = processes.ToList().Find(x => x.Id.ToString() == selectWechat.SelectProcess.ProcessId);
if (p == null)
return null;
return p;
}
else
return processes[0];
}
Expand Down
6 changes: 3 additions & 3 deletions Helpers/WechatDBHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@ public static string MoveUserData(string path)
}
return "请复制目录至文本框内";
}
public static void DecryUserData(byte[] key)
public static void DecryUserData(byte[] key,string source,string to)
{
string dbPath = Path.Combine(UserWorkPath, "DB");
string decPath = Path.Combine(UserWorkPath, "DecDB");
string dbPath = source;
string decPath = to;
if(!Directory.Exists(decPath))
Directory.CreateDirectory(decPath);

Expand Down
37 changes: 25 additions & 12 deletions HtmlExport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ public void InitTemplate(WXSession session)
HtmlBody += string.Format("<div class=\"msg\"><p class=\"nickname\"><b>导出时间:{0}</b></p><hr/>", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
}

public void InitTemplate(WXContact contact)
{
WXSession session = new WXSession();
session.NickName = contact.NickName;
session.UserName = contact.UserName;
InitTemplate(session);
}

public void Save(string path = "",bool append = false)
{
if (!append)
Expand All @@ -41,44 +49,49 @@ public void SetEnd()
HtmlBody += "</body></html>";
}

public void SetMsg(WXReader reader, WXSession session)
public void SetMsg(WXUserReader reader,WXContact contact)
{
List<WXMsg> msgList = reader.GetMsgs(session.UserName);
if (Session == null)
throw new Exception("请初始化模版:Not Use InitTemplate");

List<WXMsg>? msgList = reader.GetWXMsgs(contact.UserName);
if (msgList == null)
throw new Exception("获取消息失败,请确认数据库读取正常");

msgList.Sort((x, y) => x.CreateTime.CompareTo(y.CreateTime));

foreach (var msg in msgList)
{
if (Session == null)
throw new Exception("请初始化模版:Not Use InitTemplate");
HtmlBody += string.Format("<div class=\"msg\"><p class=\"nickname\">{0} <span style=\"padding-left:10px;\">{1}</span></p>", msg.IsSender ? "我" : Session.NickName, TimeStampToDateTime(msg.CreateTime).ToString("yyyy-MM-dd HH:mm:ss"));

if (msg.Type == 1)
HtmlBody += string.Format("<p class=\"content\">{0}</p></div>", msg.StrContent);
else if(msg.Type == 3)
else if (msg.Type == 3)
{
string? path = reader.GetImage(msg);
string? path = reader.GetAttachment(WXMsgType.Image, msg);
if (path == null)
{
HtmlBody += string.Format("<p class=\"content\">{0}</p></div>", "图片转换出现错误或文件不存在");
continue;
}
HtmlBody += string.Format("<p class=\"content\"><img src=\"{0}\" style=\"max-height:1000px;max-width:1000px;\"/></p></div>", path);
}
else if(msg.Type == 43)
else if (msg.Type == 43)
{
string? path = reader.GetVideo(msg);
string? path = reader.GetAttachment(WXMsgType.Video, msg);
if (path == null)
{
HtmlBody += string.Format("<p class=\"content\">{0}</p></div>", "视频不存在");
continue;
}
HtmlBody += string.Format("<p class=\"content\"><video controls style=\"max-height:300px;max-width:300px;\"><source src=\"{0}\" type=\"video/mp4\" /></video></p></div>", path);
}
else if(msg.Type == 34)
else if (msg.Type == 34)
{
string? path = reader.GetVoice(msg);
string? path = reader.GetAttachment(WXMsgType.Audio, msg);
if (path == null)
{
HtmlBody += string.Format("<p class=\"content\">{0}</p></div>", "视频不存在");
HtmlBody += string.Format("<p class=\"content\">{0}</p></div>", "语音不存在");
continue;
}
HtmlBody += string.Format("<p class=\"content\"><audio controls src=\"{0}\"></audio></p></div>", path);
Expand All @@ -88,8 +101,8 @@ public void SetMsg(WXReader reader, WXSession session)
HtmlBody += string.Format("<p class=\"content\">{0}</p></div>", "暂未支持的消息");
}
}
}

}
private static DateTime TimeStampToDateTime(long timeStamp, bool inMilli = false)
{
DateTimeOffset dateTimeOffset = inMilli ? DateTimeOffset.FromUnixTimeMilliseconds(timeStamp) : DateTimeOffset.FromUnixTimeSeconds(timeStamp);
Expand Down
3 changes: 2 additions & 1 deletion Interface/ExportInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ namespace WechatPCMsgBakTool.Interface
public interface IExport
{
void InitTemplate(WXSession session);
void SetMsg(WXReader reader, WXSession session);
void InitTemplate(WXContact session);
void SetMsg(WXUserReader reader, WXContact session);
void SetEnd();
void Save(string path = "", bool append = false);

Expand Down
38 changes: 38 additions & 0 deletions Main.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<Window x:Class="WechatPCMsgBakTool.Main"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WechatPCMsgBakTool"
mc:Ignorable="d"
WindowStartupLocation="CenterScreen"
Title="溯雪微信备份工具" Height="450" Width="800">
<Grid>
<ListView Name="list_workspace" Margin="15,50,0,20" HorizontalAlignment="Left" Width="230" Grid.RowSpan="2" SelectionChanged="list_workspace_SelectionChanged">
<ListView.View>
<GridView>
<GridViewColumn Header="原始id" Width="140" DisplayMemberBinding="{Binding UserName,Mode=TwoWay}" />
<GridViewColumn Header="是否解密" Width="80" DisplayMemberBinding="{Binding Decrypt,Mode=TwoWay}" />
</GridView>
</ListView.View>
</ListView>
<Label Content="工作区:" HorizontalAlignment="Left" Margin="15,15,0,0" VerticalAlignment="Top" Height="25" Width="58"/>
<Button Content="新增" Width="50" HorizontalAlignment="Left" Margin="194,20,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.343,0.521" Height="19" Click="Button_Click_1"/>
<Label Content="用户路径:-" Name="user_path" HorizontalAlignment="Left" Margin="278,50,0,0" VerticalAlignment="Top" Height="25" Width="500"/>
<Button Content="解密" IsEnabled="False" Width="50" HorizontalAlignment="Left" Margin="285,20,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.343,0.521" Name="btn_decrypt" Click="btn_decrypt_Click" Height="19"/>
<Button Content="读取" IsEnabled="False" Width="50" HorizontalAlignment="Left" Margin="365,20,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.343,0.521" Name="btn_read" Click="btn_read_Click" Height="19" />

<ListView Name="list_sessions" Margin="278,130,0,20" HorizontalAlignment="Left" Width="290" MouseDoubleClick="list_sessions_MouseDoubleClick">
<ListView.View>
<GridView>
<GridViewColumn Header="昵称" Width="120" DisplayMemberBinding="{Binding NickName}" />
<GridViewColumn Header="原始id" Width="140" DisplayMemberBinding="{Binding UserName}" />
</GridView>
</ListView.View>
</ListView>
<Button Content="导出所选人员聊天记录" HorizontalAlignment="Left" Margin="609,130,0,0" VerticalAlignment="Top" Width="142" Click="Button_Click"/>
<Label Content="搜索:" HorizontalAlignment="Left" Margin="278,92,0,0" VerticalAlignment="Top"/>
<TextBox Name="find_user" HorizontalAlignment="Left" Margin="323,96,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120" Height="20"/>
<Button Name="btn_search" Content="搜索" HorizontalAlignment="Left" Margin="451,96,0,0" VerticalAlignment="Top" Width="43" Click="btn_search_Click"/>
</Grid>
</Window>
Loading

0 comments on commit 03074e7

Please sign in to comment.