-
Notifications
You must be signed in to change notification settings - Fork 78
/
encrypt_token.php
66 lines (50 loc) · 1.36 KB
/
encrypt_token.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
<?php
const CIPHER = 'AES-256-CBC';
const SECRET_KEY = 'MySuperSecretKeyForParamsToken12';
$tokenObject = [
"connection" => [
"type" => "rdp",
"settings" => [
"hostname" => "10.0.0.12",
"username" => "Administrator",
"password" => "pAsSwOrD",
"enable-drive" => true,
"create-drive-path" => true,
"security" => "any",
"ignore-cert" => true,
"enable-wallpaper" => false
]
]
];
function encryptToken($value): string
{
$iv = random_bytes(16);
$value = openssl_encrypt(
json_encode($value),
CIPHER,
SECRET_KEY,
0,
$iv
);
if ($value === false) {
throw new Exception('Could not encrypt the data.');
}
$data = [
'iv' => base64_encode($iv),
'value' => $value,
];
$json = json_encode($data);
if (!is_string($json)) {
throw new Exception('Could not encrypt the data.');
}
return base64_encode($json);
}
$token = encryptToken($tokenObject);
echo "Parameters:" . PHP_EOL;
echo json_encode($tokenObject, JSON_PRETTY_PRINT) . PHP_EOL;
echo PHP_EOL . PHP_EOL;
echo "Encrypted token:" . PHP_EOL;
echo $token . PHP_EOL;
echo PHP_EOL . PHP_EOL;
echo "Use this token in the URL:" . PHP_EOL;
echo "ws://localhost:8080/?token=" . urlencode($token) . PHP_EOL;