-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
70 lines (60 loc) · 1.9 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
69
import os
from flask import Flask, request, render_template, jsonify
import unicodedata
app = Flask(__name__)
keypad = {
'1': '.,?!:',
'2': 'abc',
'3': 'def',
'4': 'ghi',
'5': 'jkl',
'6': 'mno',
'7': 'pqrs',
'8': 'tuv',
'9': 'wxyz',
'0': ' '
}
reverse_keypad = {char: (key, index + 1) for key, chars in keypad.items() for index, char in enumerate(chars)}
def encrypt(message):
encrypted_message = ''
for char in message.lower():
if char.isdigit():
encrypted_message += f"#{char} "
else:
for key, chars in keypad.items():
if char in chars:
encrypted_message += key * (chars.index(char) + 1) + ' '
break
return encrypted_message.strip()
def decrypt(encrypted_message):
decrypted_message = ''
tokens = encrypted_message.split()
for token in tokens:
if token.startswith('#'):
decrypted_message += token[1]
else:
key = token[0]
count = len(token)
for char, (k, cnt) in reverse_keypad.items():
if k == key and cnt == count:
decrypted_message += char
break
return decrypted_message
def remove_special_characters(message):
string = unicodedata.normalize('NFKD', message).encode('ASCII', 'ignore').decode('ASCII')
return string
@app.route('/')
def index():
return render_template('index.html')
@app.route('/process', methods=['POST'])
def process():
message = request.form['message']
message = remove_special_characters(message)
if not all(char.isdigit() or char.isspace() or char == "#" for char in message):
result = encrypt(message)
else:
result = decrypt(message)
return jsonify(result=result)
if __name__ == '__main__':
port = int(os.environ.get('PORT', 8000))
app.run(host='0.0.0.0', port=port)