-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaesar_code.py
More file actions
60 lines (49 loc) 路 1.83 KB
/
Copy pathcaesar_code.py
File metadata and controls
60 lines (49 loc) 路 1.83 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# My first attempt at a Caesar Cipher tool.
# Simple but gets the job quicker.
# Caesar Cipher tool which help (e)encrypt and (d)decrypt text with the help of an a shift key.
# Shift Value is the key to (d)decrypt the (e)encrypted text
# Created by [Glen.F] aka [cyb3rPh03n1x]
from flask import Flask, render_template, request
# Initialize the Flask application
app = Flask(__name__)
def cipher(text: str, shift: int, mode: str) -> str:
"""
Encrypts or decrypts a text using the Caesar Cipher.
Args:
text (str): The message to process.
shift (int): The number of positions to shift.
mode (str): 'encrypt' or 'decrypt'.
Returns:
str: The processed message.
"""
result = ""
if mode == 'decrypt':
shift = -shift
for char in text:
if 'a' <= char <= 'z':
start = ord('a')
shifted = (ord(char) - start + shift) % 26
result += chr(shifted + start)
elif 'A' <= char <= 'Z':
start = ord('A')
shifted = (ord(char) - start + shift) % 26
result += chr(shifted + start)
else:
result += char
return result
@app.route('/', methods=['GET', 'POST'])
def index():
"""Handles the web page for the cipher tool."""
result = ""
if request.method == 'POST':
text = request.form.get('text')
try:
shift = int(request.form.get('shift'))
except (ValueError, TypeError):
shift = 0
mode = request.form.get('mode')
result = cipher(text, shift, mode)
return render_template('index.html', result=result)
# This part is only for local testing and will be ignored by PythonAnywhere's web server
if __name__ == "__main__":
app.run(debug=True)