-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
68 lines (57 loc) · 1.93 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, render_template, request, jsonify
app = Flask(__name__)
# Fungsi enkripsi Vigenere
def vigenere_encrypt(plaintext, key):
ciphertext = ""
key = key.upper()
key_length = len(key)
key_index = 0
for char in plaintext:
if char.isalpha():
shift = ord(key[key_index]) - ord('A')
if char.isupper():
encrypted_char = chr((ord(char) - ord('A') + shift) % 26 + ord('A'))
else:
encrypted_char = chr((ord(char) - ord('a') + shift) % 26 + ord('a'))
ciphertext += encrypted_char
key_index = (key_index + 1) % key_length
else:
ciphertext += char
return ciphertext
# Fungsi dekripsi Vigenere
def vigenere_decrypt(ciphertext, key):
plaintext = ""
key = key.upper()
key_length = len(key)
key_index = 0
for char in ciphertext:
if char.isalpha():
shift = ord(key[key_index]) - ord('A')
if char.isupper():
decrypted_char = chr((ord(char) - ord('A') - shift + 26) % 26 + ord('A'))
else:
decrypted_char = chr((ord(char) - ord('a') - shift + 26) % 26 + ord('a'))
plaintext += decrypted_char
key_index = (key_index + 1) % key_length
else:
plaintext += char
return plaintext
@app.route('/')
def index():
return render_template('index.html')
@app.route('/encrypt', methods=['POST'])
def encrypt():
data = request.json
plaintext = data['plaintext']
key = data['key']
ciphertext = vigenere_encrypt(plaintext, key)
return jsonify({'ciphertext': ciphertext})
@app.route('/decrypt', methods=['POST'])
def decrypt():
data = request.json
ciphertext = data['ciphertext']
key = data['key']
decrypted_text = vigenere_decrypt(ciphertext, key)
return jsonify({'decrypted_text': decrypted_text})
if __name__ == '__main__':
app.run(debug=True)