-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
88 lines (80 loc) · 3.22 KB
/
app.py
File metadata and controls
88 lines (80 loc) · 3.22 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import streamlit as st
from io import StringIO
st.markdown(
"""
<style>
.stApp {
background-color: #DDD8E6;
}
</style>
""",
unsafe_allow_html=True
)
#### Caesar Cipher Encrypt & Decrypt
class caesarCipher:
alpha = 'abcdefghijklmnopqrstuvwxyz'
# @staticmethod
def decrypt(userInput, key):
userInput = ''.join(c.lower() for c in userInput if c.isalpha())
output = ''
for c in userInput:
if c in caesarCipher.alpha:
output += caesarCipher.alpha[(caesarCipher.alpha.index(c) - key) % len(caesarCipher.alpha)]
return output
# @staticmethod
def encrypt(userInput, key):
userInput = ''.join(c.lower() for c in userInput if c.isalpha())
output = ''
for c in userInput:
if c in caesarCipher.alpha:
output += caesarCipher.alpha[(caesarCipher.alpha.index(c) + key) % len(caesarCipher.alpha)]
return output
#### Vigenere Cipher Encrypt & Decrypt
class vigenereCipher:
alpha = 'abcdefghijklmnopqrstuvwxyz'
# @staticmethod
def decrypt(userInput, key):
userInput = ''.join(c.lower() for c in userInput if c.isalpha())
output = ''
for (i, c) in enumerate(userInput):
i = i % len(key)
output += vigenereCipher.alpha[(vigenereCipher.alpha.index(c) - vigenereCipher.alpha.index(key[i % len(key)])) % len(vigenereCipher.alpha)]
return output
# @staticmethod
def encrypt(userInput, key):
userInput = ''.join(c.lower() for c in userInput if c.isalpha())
output = ''
for(i, c) in enumerate(userInput):
i = i%len(key)
output += vigenereCipher.alpha[(vigenereCipher.alpha.index(c) + vigenereCipher.alpha.index(key[i % len(key)])) % len(vigenereCipher.alpha)]
return output
# Streamlit UI
st.title("Encryption & Decryption App")
st.write("Choose a cipher and perform encryption or decryption.")
st.write("Either upload a .txt file or enter your text in the textbox below")
# Input fields
file = st.file_uploader(label="Text File", type="txt", accept_multiple_files=False, label_visibility="visible")
if file is not None:
file_string = StringIO(file.getvalue().decode('utf-8'))
text = st.text_area(label="File Contents: ", value=file_string.read())
else:
text = st.text_area("Enter your text:")
cipher_choice = st.selectbox("Choose a cipher:", ["Caesar Cipher", "Vigenere Cipher"])
action = st.radio("Action:", ["Encrypt", "Decrypt"])
if cipher_choice == "Caesar Cipher":
key = st.number_input("Enter the shift key (integer):", min_value=1, max_value=25, step=1, value=3)
elif cipher_choice == "Vigenere Cipher":
key = st.text_input("Enter the keyword:", value="key")
# Button to process encryption/decryption
if st.button("Submit"):
if cipher_choice == "Caesar Cipher":
if action == "Encrypt":
result = caesarCipher.encrypt(text, key)
else:
result = caesarCipher.decrypt(text, key)
elif cipher_choice == "Vigenere Cipher":
if action == "Encrypt":
result = vigenereCipher.encrypt(text, key)
else:
result = vigenereCipher.decrypt(text, key)
st.write(f"**Result:** {result}")