-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencrypt.php
More file actions
executable file
·188 lines (152 loc) · 4.84 KB
/
encrypt.php
File metadata and controls
executable file
·188 lines (152 loc) · 4.84 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
180
181
182
183
184
185
186
187
188
#!/usr/bin/env php
<?php
// Constants
define('ENCRYPTION_ALGO', 'aes-256-cbc');
define('SALT_PREFIX', 'Salted__');
define('KEY_LENGTH', 32);
define('PBKDF2_ALGO', 'sha256');
define('PBKDF2_ROUNDS', 10000);
define('ENCRYPTED_CONTENT_ENCODING', 'base64');
define('UNENCRYPTED_CONTENT_ENCODING', 'utf8');
function showHelp() {
echo "Usage: php encrypt.php [encrypt|decrypt] [options]\n";
echo "\n";
echo "Options:\n";
echo " --text=<text> Input text\n";
echo " --file=<file> Input file\n";
echo " --password=<pass> Password for encryption/decryption. Optional.\n";
echo " --output=<file> Output file (default: print to stdout)\n";
echo " --dec-encoding=<enc> Encoding of the decrypted input/output (base64|utf8) (default: utf8)\n";
echo " --help Show this help message\n";
}
function parseArgs() {
global $argv;
$args = array_slice($argv, 1);
if (empty($args) || in_array('--help', $args)) {
showHelp();
exit(1);
}
$action = $args[0];
$options = [];
for ($i = 1; $i < count($args); $i++) {
$arg = $args[$i];
if (strpos($arg, '--') === 0) {
$parts = explode('=', $arg, 2);
$key = substr($parts[0], 2);
$value = isset($parts[1]) ? trim($parts[1]) : true;
$options[$key] = $value;
}
}
return [$action, $options];
}
function getInputContent($options) {
if (isset($options['file'])) {
if (!file_exists($options['file'])) {
echo "Error: Input file does not exist: {$options['file']}\n";
exit(1);
}
return file_get_contents($options['file']);
}
if (isset($options['text'])) {
return $options['text'];
}
return readline("Enter content: ");
}
function getPassword($options) {
if (isset($options['password'])) {
return $options['password'];
}
// Use readline with hidden input simulation (not perfect but works)
// For better security, consider using system('stty -echo') on Unix
echo "Enter password: ";
system('stty -echo');
$password = trim(fgets(STDIN));
system('stty echo');
echo "\n";
return $password;
}
function deriveKeyAndIV($password, $salt) {
$derived = hash_pbkdf2(
PBKDF2_ALGO,
$password,
$salt,
PBKDF2_ROUNDS,
KEY_LENGTH + 16,
true // raw binary output
);
$key = substr($derived, 0, KEY_LENGTH);
$iv = substr($derived, KEY_LENGTH, 16);
return [$key, $iv];
}
function encrypt($content, $password) {
// Generate salt
$salt = random_bytes(8);
// Derive key and IV from password and salt using PBKDF2
[$key, $iv] = deriveKeyAndIV($password, $salt);
// Encrypt using AES-256-CBC
$encryptedContent = openssl_encrypt(
$content,
ENCRYPTION_ALGO,
$key,
OPENSSL_RAW_DATA,
$iv
);
if ($encryptedContent === false) {
echo "Error: Encryption failed\n";
exit(1);
}
// Build final buffer: prefix + salt + encrypted content
$finalBuffer = SALT_PREFIX . $salt . $encryptedContent;
// Encode to base64
return base64_encode($finalBuffer);
}
function decrypt($encrypted, $password) {
// Decode from base64
$buffer = base64_decode($encrypted, true);
if ($buffer === false) {
echo "Error: Invalid base64 input\n";
exit(1);
}
// Extract salt (skip the prefix)
$salt = substr($buffer, strlen(SALT_PREFIX), 8);
$encryptedContent = substr($buffer, 16);
// Derive key and IV from password and salt using PBKDF2
[$key, $iv] = deriveKeyAndIV($password, $salt);
// Decrypt using AES-256-CBC
$decryptedContent = openssl_decrypt(
$encryptedContent,
ENCRYPTION_ALGO,
$key,
OPENSSL_RAW_DATA,
$iv
);
if ($decryptedContent === false) {
echo "Error: Decryption failed\n";
exit(1);
}
return $decryptedContent;
}
function main() {
[$action, $options] = parseArgs();
if (!in_array($action, ['encrypt', 'decrypt'])) {
showHelp();
exit(1);
}
$content = getInputContent($options);
$password = getPassword($options);
if ($action === 'encrypt') {
$output = encrypt($content, $password);
} else {
$output = decrypt($content, $password);
// Handle dec-encoding option (though not used in decrypt function, keeping for compatibility)
if (isset($options['dec-encoding']) && $options['dec-encoding'] === 'base64') {
$output = base64_encode($output);
}
}
if (isset($options['output'])) {
file_put_contents($options['output'], $output);
} else {
echo $output . "\n";
}
}
main();