-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencdec.php
221 lines (195 loc) · 5.91 KB
/
encdec.php
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
// v2
if (!isset($argv[1])) {
die("type 'help' for instructions\n");
}
$firstArg = $argv[1];
Encdec::init();
switch ($firstArg) {
case 'help':
echo "Type 'php encdec.php key' in order to generate a physical key.\n";
echo "Type 'php encdec.php enc' in order to encrypt a directory.\n";
echo "Type 'php encdec.php dec' in order to decrypt a directory.\n";
echo "You can adjust settings in encdec.php in the init() function.\n";
break;
case 'key':
$key = Encdec::generateKey();
$pathName = Encdec::$settings->keyDir.Encdec::$settings->keyFile;
if(!file_exists($pathName)){
Encdec::saveFile($pathName, $key);
echo "Key saved as '{$pathName}'\n";
} else { die("Could not save file '{$pathName}'. File already exists\n"); }
break;
case 'enc':
Encdec::encryptProcess();
break;
case 'dec':
Encdec::decryptProcess();
break;
default:
echo "Unknown argument\n";
}
class Encdec {
public static $settings;
public static function init () {
self::$settings = (object) [
'keyFile' => 'key',
'keyDir' => '',
'dataDir' => 'data/',
'encDir' => '',
'encFile' => 'enc',
'decDir' => 'dec/',
'addMax' => 444, // must be higher than any possible sum
'defaultKeyLength' => 255,
'encryptType' => 'password' // physical or password
];
}
public static function readFileAndDirectoryArray ($array, $dir) {
$subFiles = array();
foreach ($array as $k => $sub) {
$pathName = $dir.$sub->fileName;
if ($sub->string) {
self::saveFile($pathName, $sub->string);
} else if ($sub->files) {
$pathName = $pathName."/";
if (!file_exists($pathName)) {
mkdir($pathName);
}
$subFiles = $sub->files;
self::readFileAndDirectoryArray($subFiles, $pathName);
}
}
}
public static function readFileAndDirectory ($dir) {
// http://www.php.net/manual/en/function.readdir.php#87733
$listDir = array();
if ($handler = opendir($dir)) {
while (($sub = readdir($handler)) !== false) {
if ($sub != '.' && $sub != '..' && $sub != '.DS_Store') {
if (is_file($dir.'/'.$sub)) {
$fileData = file_get_contents($dir."/".$sub);
$fileData = (object) [
'fileName' => $sub,
'string' => $fileData
];
$listDir[] = $fileData;
} elseif(is_dir($dir.'/'.$sub)) {
$fileData = (object) [
'fileName' => $sub,
'files' => self::readFileAndDirectory($dir.'/'.$sub)
];
$listDir[$sub] = $fileData;
}
}
}
closedir($handler);
}
return $listDir;
}
public static function saveFile ($path, $data) {
$file = fopen($path, 'w') or die('Unable to open file');
fwrite($file, $data);
fclose($file);
}
public static function generateKey () {
// $keyLength = length of to-encrypt datastring would be high security
$keyLength = self::$settings->defaultKeyLength;
$key = '';
for ($i = 0; $i<$keyLength; $i++) {
$key .= chr(rand(1, 100));
}
return $key;
}
public static function getUsersPassword () {
echo "Please enter your master password:\n";
system('stty -echo'); // stop showing characters in terminal
$password = trim(fgets(STDIN));
system('stty echo'); // show characters in terminal again
// http://php.net/manual/en/function.hash-pbkdf2.php
$iterations = 1000;
$hashLength = self::$settings->defaultKeyLength;
return hash_pbkdf2("sha256", $password, '', $iterations, $hashLength); // using without salt
}
public static function encrypt ($string, $key) {
$string = str_split($string);
$key = str_split($key);
$secretString = '';
$keyUnit = 0;
$keyLength = count($key);
foreach ($string as $k => $char) {
$num = abs((ord($char) + ord($key[$keyUnit])) - self::$settings->addMax);
$secretString .= chr($num);
$keyUnit++;
if ($keyUnit == $keyLength) {
$keyUnit = 0;
}
}
return $secretString;
}
public static function decrypt ($string, $key) {
$string = str_split($string);
$key = str_split($key);
$message = "";
$keyUnit = 0;
$keyLength = count($key);
foreach ($string as $k => $char) {
$num = abs(ord($char) - self::$settings->addMax) - ord($key[$keyUnit]);
$message .= chr($num);
$keyUnit++;
if($keyUnit == $keyLength){
$keyUnit = 0;
}
}
return $message;
}
public static function encryptProcess () {
// get key
if (self::$settings->encryptType == 'password' ) {
$key = self::getUsersPassword();
} else {
$keyPathName = self::$settings->keyDir.self::$settings->keyFile;
if (file_exists($keyPathName)) {
$key = file_get_contents($keyPathName);
} else {
$key = self::generateKey();
self::saveFile($keyPathName, $key);
echo "Key saved as '{$keyPathName}'\n";
}
}
// encrypt files
$dataDir = self::$settings->dataDir;
if (!file_exists($dataDir)) { die("Path $dataDir does not exist\n"); }
$dataDir = rtrim($dataDir, "/");
$files = self::readFileAndDirectory($dataDir);
$files = json_encode($files);
$encryptedString = self::encrypt($files, $key);
// save file local
$pathName = self::$settings->encDir.self::$settings->encFile;
self::saveFile($pathName, $encryptedString);
echo "File saved as $pathName\n";
echo "Encryption done\n";
}
public static function decryptProcess () {
// get key
if (self::$settings->encryptType == 'password' ) {
$key = self::getUsersPassword();
} else {
$keyPathName = self::$settings->keyDir.self::$settings->keyFile;
if(!file_exists($keyPathName)){
die('no key avaiable');
}
$key = file_get_contents($keyPathName);
}
$stringPathName = self::$settings->encDir.self::$settings->encFile;
$pathName = self::$settings->decDir;
if (!file_exists($pathName)) {
die("Path $pathName does not exist\n");
}
$files = json_decode(self::decrypt(file_get_contents($stringPathName), $key));
if (empty($files)) {
die("No data to decrypt avaiable. Data is probably corrupt.\n");
}
self::readFileAndDirectoryArray($files, $pathName);
echo "Decryption done\n";
}
}