· 6 years ago · Jun 06, 2019, 01:40 PM
1import json
2from base64 import b64encode, b64decode
3from Crypto.Cipher import AES
4from Crypto import Random
5
6
7DATA = {
8 "client_id":"RICHHAMSTER",
9 "action":"pay",
10 "amount":"3.00",
11 "currency":"UAH",
12 "recipient_card":"23",
13 "email":"bill@microsoft.com",
14 "order_id":"1",
15 "success_url": "https://some-url.tld/thank-you",
16 "fail_url": "https://some-url.tld/error",
17 "callback_url": "https://some-url.tld/callback",
18 "description":"some description",
19 "language": "uk"
20}
21KEY = "d2c17f3c2e6972c2bef5cadc6200c08G".encode("utf8")
22
23
24def encode_request_data():
25 # $cipher = 'AES-256-CBC';
26 # $iv_len = openssl_cipher_iv_length($cipher);
27 # $iv = openssl_random_pseudo_bytes($iv_len);
28 # $data_raw = openssl_encrypt($json, $cipher, $secret_key, OPENSSL_RAW_DATA, $iv);
29 # $data = base64_encode($iv . $data_raw);
30
31 jdata = json.dumps(DATA).encode("utf8")
32 length = 16 - (len(jdata) % 16)
33 jdata += bytes([length])*length
34
35 iv = Random.new().read(16)
36
37 cipher = AES.new(KEY, AES.MODE_CBC, iv)
38 encrypted = cipher.encrypt(jdata)
39 return b64encode(encrypted)
40
41
42def decode_request_data(decode_me):
43 # $data = file_get_contents("php://input");
44 # $cipher = 'AES-256-CBC';
45 # $iv_len = openssl_cipher_iv_length($cipher);
46 # $buff = base64_decode($data);
47 # $iv = substr($buff, 0, $iv_len);
48 # $encrypted = substr($buff, $iv_len);
49 # $json = openssl_decrypt($encrypted, $cipher, $secret_key, OPENSSL_RAW_DATA, $iv);
50 # $data = json_decode($json, true);
51
52 buff = b64decode(decode_me)
53 iv = Random.new().read(16)
54 cipher = AES.new(KEY, AES.MODE_CBC, iv)
55 return cipher.decrypt(buff)
56
57
58if __name__ == "__main__":
59 encoded = encode_request_data()
60 print(encoded)
61 data = decode_request_data(encoded)
62 print(data)