Skip to content

Instantly share code, notes, and snippets.

@yutao8
Created January 6, 2024 02:31
Show Gist options
  • Save yutao8/3694ac8f74b4d9c13acb22d360678af3 to your computer and use it in GitHub Desktop.
Save yutao8/3694ac8f74b4d9c13acb22d360678af3 to your computer and use it in GitHub Desktop.
微信dat图片还原(PHP版)
<?php
function DatToImage($datPath) {
$b = file_get_contents($datPath);
if ($b === false) {
throw new Exception("Failed to read DAT file");
}
if (strlen($b) < 2) {
throw new Exception("Image size error");
}
$j0 = ord($b[0]) ^ 0xFF;
$j1 = ord($b[1]) ^ 0xD8;
$g0 = ord($b[0]) ^ 0x47;
$g1 = ord($b[1]) ^ 0x49;
$p0 = ord($b[0]) ^ 0x89;
$p1 = ord($b[1]) ^ 0x50;
$v = 0;
$ext = '';
if ($j0 == $j1) {
$v = $j0;
$ext = 'jpg';
} else if ($g0 == $g1) {
$v = $g0;
$ext = 'gif';
} else if ($p0 == $p1) {
$v = $p0;
$ext = 'png';
} else {
throw new Exception("Unknown image format");
}
for ($i = 0; $i < strlen($b); $i++) {
$b[$i] = chr(ord($b[$i]) ^ $v);
}
$imgPath = substr($datPath, 0, -strlen($ext)).$ext;
if (file_put_contents($imgPath, $b) === false) {
throw new Exception("Failed to write image file");
}
return $imgPath;
}
try {
$datPath = 'f8bdeb8a1cc3586ec6e29ff883257c2f.dat';
$imgPath = DatToImage($datPath);
echo "Image successfully converted: ".$imgPath;
} catch (Exception $e) {
echo "Error converting image: ".$e->getMessage();
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment