-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecryption.php
More file actions
134 lines (113 loc) · 3.91 KB
/
Copy pathdecryption.php
File metadata and controls
134 lines (113 loc) · 3.91 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
<?php
/**
* @package File Encryption
* @license https://www.gnu.org/licenses/gpl-3.0.en.html
* @author Cory <cobb208@gmail.com>
*/
namespace Encryption\Decryption;
require_once 'connection.php';
require_once 'validation.php';
require_once 'globals.php';
use DateTime;
use DateTimeZone;
use Encryption\Globals\GlobalVars;
use Encryption\Validation\ValidationRules;
use Exception;
use Encryption\Connection\DatabaseConnection;
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
session_start();
ValidationRules::validate_csrf_token();
$receiver_email = ValidationRules::validate_email($_POST['yourEmail']);
if(!$receiver_email) {
http_response_code(500);
exit();
}
$password = $_POST['password'];
$passphrase = $_POST['passphrase'];
$db = new DatabaseConnection();
$mysqli = $db->mysqli;
$stmt = $mysqli->prepare('SELECT iv, filepath, tag, file_ext FROM files WHERE passphrase = ? AND receiver_email = ? LIMIT 1');
$stmt->bind_param('ss', $passphrase, $receiver_email);
if(!$stmt->execute())
{
http_response_code(500);
exit();
}
$result = $stmt->get_result();
if(!$result)
{
http_response_code(404);
}
$result_filepath = '';
$result_iv = '';
$result_tag = '';
$result_file_extension = '';
while($row = $result->fetch_assoc())
{
$result_filepath = $row['filepath'];
$result_iv = $row['iv'];
$result_tag = $row['tag'];
$result_file_extension = $row['file_ext'];
}
try {
$date = new DateTime('now', new DateTimeZone('UTC'));
$date_stamp = $date->format('is');
} catch (Exception $e) {
http_response_code(500);
exit;
}
$cipher = 'aes-128-gcm';
$file_contents = file_get_contents($result_filepath);
$plain_text = openssl_decrypt($file_contents, $cipher, $password, 0, $result_iv, $result_tag);
$file_prefix = explode("@", $receiver_email);
$tmp_file = fopen('/tmp/' . $file_prefix[0] . $date_stamp . '.' . $result_file_extension, 'w');
$true_file_name = $file_prefix[0] . $date_stamp . '.' . $result_file_extension;
fwrite($tmp_file, $plain_text);
fclose($tmp_file);
header('Content-Type: multi-part/form-data');
header("Content-Disposition: attachment; filename=$true_file_name");
if(readfile('/tmp/' . $true_file_name)) {
unlink('/tmp/' . $true_file_name);
exit;
}
http_response_code(500);
exit;
}
$site_title = 'Decrypt Your File';
$active_page = 'decryption';
$base_url = GlobalVars::$base_url;
$additional_header_tags = array(
"<link rel='stylesheet' href='{$base_url}static/css/forms.css' >"
);
require_once 'template/header.php';
?>
<article>
<h1>Decrypt Your File</h1>
<section id="sectionForm" class="center-it">
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<?php ValidationRules::generate_csrf_form_token(); ?>
<fieldset>
<legend>Emails</legend>
<div class="form-input-block">
<label for="yourEmail">Your Email:</label>
<input type="email" name="yourEmail" id="yourEmail" required placeholder="your_email@mail.com"/>
</div>
</fieldset>
<fieldset>
<legend>Passcodes</legend>
<div class="form-input-block">
<label for="retrievePassword">Enter the Pass Phrase:</label>
<input type="password" id="retrievePassword" name="password" required/>
</div>
<div class="form-input-block">
<label for="passphrase">Unique Code:</label>
<input type="password" id="passphrase" name="passphrase" />
</div>
<input type="submit" value="Retrieve" />
</fieldset>
</form>
</section>
</article>
<?php
require_once 'template/footer.php';