-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
executable file
·68 lines (56 loc) · 2.21 KB
/
app.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
62
63
64
65
66
67
68
from flask import Flask, request, jsonify
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import padding
from cryptography.hazmat.backends import default_backend
import base64
from flask_cors import CORS
from Cryptodome.Cipher import AES
app = Flask(__name__)
CORS(app)
def decrypt(key, encrypted_data, IV):
unpad = lambda s: s[:-ord(s[len(s) - 1:])]
key = key.encode('utf-8')
IV = IV.encode('utf-8')
data = base64.b64decode(encrypted_data)
cipher = AES.new(key, AES.MODE_CBC, IV)
# unpad
text_decrypted = unpad(cipher.decrypt(data))
print(text_decrypted)
text_decrypted = text_decrypted.decode('utf-8')
# print(text_decrypted)
return text_decrypted
@app.route('/reg', methods=['POST'])
def reg():
data = request.get_json()
encrypted_data = data.get('dataFile')
encrypted_aes_key = data.get('encryptedAesKey')
salt = encrypted_data[:16]
IV = encrypted_data[16:32]
encrypted_data = encrypted_data[32:]
print("salt:",salt)
print("IV: ", IV)
print("encrypted data: ", encrypted_data)
# Decrypt the encrypted AES key and data here using your RSA private key
# Load your RSA private key (replace with your own private key file)
with open("private_key.pem", "rb") as key_file:
private_key = serialization.load_pem_private_key(
key_file.read(),
password=None,
backend=default_backend()
)
# Example encrypted AES key (use the one received from the client)
encrypted_aes_key = base64.b64decode(encrypted_aes_key)
# Decrypt the AES key
# Using PKCS1v15 as padding standard. Actually OAEP is better.
decrypted_aes_key = private_key.decrypt(
encrypted_aes_key,
padding.PKCS1v15()
).decode()
print("Decrypted aes Key: ", decrypted_aes_key)
# Get the decrypted data
decrypted_data = decrypt(decrypted_aes_key, encrypted_data, IV)
# Process the decrypted data (username and password) here
print("Decrypted: ", decrypted_data)
return jsonify({"message": "Data received and decrypted successfully!"})
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True, port=8080, ssl_context='adhoc')