-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathencrypt_token.py
61 lines (46 loc) · 1.45 KB
/
encrypt_token.py
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
import json
import base64
import os
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
CIPHER = 'AES-256-CBC'
SECRET_KEY = b'MySuperSecretKeyForParamsToken12' # 32 bytes key for AES-256
token_object = {
"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
}
}
}
def encrypt_token(value):
iv = os.urandom(16) # 16 bytes for AES
cipher = AES.new(SECRET_KEY, AES.MODE_CBC, iv)
# Convert value to JSON and pad it
padded_data = pad(json.dumps(value).encode(), AES.block_size)
# Encrypt data
encrypted_data = cipher.encrypt(padded_data)
# Encode the IV and encrypted data
data = {
'iv': base64.b64encode(iv).decode('utf-8'),
'value': base64.b64encode(encrypted_data).decode('utf-8')
}
# Convert the data dictionary to JSON and then encode it
json_data = json.dumps(data)
return base64.b64encode(json_data.encode()).decode('utf-8')
token = encrypt_token(token_object)
print("Parameters:")
print(json.dumps(token_object, indent=4))
print("\n\n")
print("Encrypted token:")
print(token)
print("\n\n")
print("Use this token in the URL:")
print("ws://localhost:8080/?token=" + token)