This repository was archived by the owner on Jan 29, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathencrypt.php
More file actions
179 lines (158 loc) · 4.85 KB
/
encrypt.php
File metadata and controls
179 lines (158 loc) · 4.85 KB
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
<?php
declare(strict_types=1);
/*
Portable program to encrypt resource packs
*/
if(version_compare(phpversion(), '8.0.0', '<')){
echo "PHP 8.0.0 or higher is required to run this script. You are using PHP ".phpversion().PHP_EOL;
exit(1);
}
//check opelssl ext
if(!extension_loaded("openssl")){
echo "openssl extension is required to run this script.".PHP_EOL;
exit(1);
}
[$dir, $contentKey, $outputdir] = readOptions($argv);
if(!is_dir($dir)){
throw new \RuntimeException("\"".$dir."\" is not a directory");
}
$exclude = [
".git",
".zip",
".mcpack",
".github",
".7z",
".gitignore",
".idea",
".bat",
"output",
"pack_icon.png",
"manifest.json",
".php",
];
echo "path: ".$dir."\n";
echo "output dir: ".$outputdir."\n";
$content = [];
foreach(new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir, FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::SKIP_DOTS)) as $realpath => $file){
foreach($exclude as $item){
if(str_contains($realpath, $item)){
continue 2;
}
}
//r_dump($file);
$path = str_replace($dir, "", $realpath);
if($path[0] === "/"||$path[1] === ":"){
throw new \LogicException("The path cannot be processed correctly: base: ".$dir.", target: ".$realpath." => ".$path);
}
$outputFile = $outputdir.$path;
$key = CryptoHelper::random(32);
$content[] = [
"path" => str_replace("\\", "/", $path),
"key" => $key,
];
echo $path." with ".$key."\n";
$data = file_get_contents($realpath);
$data = CryptoHelper::encrypt($data, $key);
@mkdir(dirname($outputFile), 0777, true);
file_put_contents($outputFile, $data);
}
$content1["content"] = $content;
$manifestFile = $dir."manifest.json";
if(!file_exists($manifestFile)){
throw new \RuntimeException("\"".$manifestFile."\" not found");
}
$manifest = json_decode(file_get_contents($manifestFile), true, flags: JSON_THROW_ON_ERROR);
$uuid = $manifest["header"]["uuid"] ?? null;
if($uuid === null){
throw new \RuntimeException("manifest.json: uuid not found");
}
//var_dump($manifest);
$json = json_encode($content1, JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES);
$contentFile = CryptoHelper::generateContentsFile($uuid, $json, $contentKey);
//var_dump($json, hexentities1($contentFile));
echo $outputdir."contents.json with ".$contentKey."\n";
file_put_contents($outputdir."contents.json", $contentFile);
file_put_contents($outputdir.basename($outputdir).".zip.key", $contentKey);
//var_dump($content1);
echo "copy: ".$outputdir."manifest.json\n";
copy($manifestFile, $outputdir."manifest.json");
if(file_exists($dir."pack_icon.png")){
echo "copy: ".$outputdir."pack_icon.png\n";
copy($dir."pack_icon.png", $outputdir."pack_icon.png");
}
echo "key: ".$contentKey."\n";
echo "done!\n";
exit;
class CryptoHelper{
public static function encrypt(string $contents, string $key) : string{
if(strlen($key) !== 32){
throw new RuntimeException("The argument key must be 32 bytes long");
}
return openssl_encrypt($contents, "AES-256-CFB8", $key, OPENSSL_RAW_DATA, substr($key, 0, 16));
}
// https://qiita.com/ngyuki/items/dd947aae213327cbeb70
public static function random($n = 32) : string{
$random = substr(base_convert(bin2hex(openssl_random_pseudo_bytes($n)), 16, 36), 0, $n);
return strtoupper($random);
}
public const HEADER = "\x0\x0\x0\x0\xfc\xb9\xcf\x9b\x0\x0\x0\x0\x0\x0\x0\x0";
public static function generateContentsFile(string $uuid, string $json, string $key) : string{
$metadata = self::HEADER."$".$uuid.str_repeat("\x0", 0xCB);
$encrypted = self::encrypt($json, $key);
$content = $metadata.$encrypted;
if(strlen($uuid) !== 36){
throw new RuntimeException("The argument uuid must be 36 bytes long");
}
if(strlen($metadata) !== 0x100){
throw new RuntimeException("The Metadata must be 0x100 bytes long");
}
return $content;
}
}
//readOption
function readOptions(array $argv){
unset($argv[0]);
if(!isset($argv[1])){
help();
}
$argv = array_values($argv);
$key = readShortOpt($argv, "k") ?? CryptoHelper::random();
$output = readShortOpt($argv, "o") ?? "encrypted";
//key must 32bytes
if(strlen($key) !== 32){
echo "Key length should be 32 bytes.\n";
help();
}
$path = $argv[0] ?? null;
if($path === null){
echo "Path not specified.\n";
help();
}
$path = realpath($path);
if($path === false){
echo "Path not found.\n";
help();
}
$path = rtrim($path, "/\\").DIRECTORY_SEPARATOR;
$output = rtrim($output, "/\\").DIRECTORY_SEPARATOR;
return [$path, $key, $output];
}
function readShortOpt(array &$argv, $opt): ?string{
$result = null;
foreach($argv as $i => $value){
if(str_starts_with($value, "-".$opt)){
$result = substr($value, 2);
unset($argv[$i]);
if($result === ""||$result === false){
$result = $argv[$i+1] ?? null;
unset($argv[$i+1]);
}
}
}
$argv = array_values($argv);
return $result;
}
function help(){
echo "Usage: php encrypt.php <path> -k <key> -o <output>\n";
exit;
}