forked from bstocker/EFREI_CryptoPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
46 lines (34 loc) · 1.6 KB
/
__init__.py
File metadata and controls
46 lines (34 loc) · 1.6 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
from cryptography.fernet import Fernet
from flask import Flask, render_template_string, render_template, jsonify, request, json
from flask import render_template
from urllib.request import urlopen
import sqlite3
app = Flask(__name__)
def generate_key():
key = Fernet.generate_key()
return key.decode()
@app.route('/')
def hello_world():
return render_template('hello.html')
@app.route('/encrypt', methods=['POST'])
def encryptage():
data = request.get_json()
valeur = data.get('valeur')
custom_key = data.get('key') if data.get('key') else generate_key()
f = Fernet(custom_key.encode())
valeur_bytes = valeur.encode() # Conversion du texte en bytes
token = f.encrypt(valeur_bytes) # Chiffrement de la valeur
return jsonify({'key': custom_key, 'valeur_encryptee': token.decode()})
@app.route('/decrypt', methods=['POST'])
def decryptage():
data = request.get_json() # Récupérer les données JSON envoyées
custom_key = data.get('key') # La clé fournie pour le déchiffrement
token = data.get('token') # Le token chiffré à déchiffrer
if not custom_key or not token:
return jsonify({'message': 'Clé ou token manquants. Veuillez fournir une clé et un token valides.'})
f = Fernet(custom_key.encode())
token_bytes = token.encode()
valeur_dechiffree = f.decrypt(token_bytes)
return jsonify({'valeur_decryptee': valeur_dechiffree.decode()})
if __name__ == "__main__":
app.run(debug=True)