-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncryptions-app.py
More file actions
160 lines (137 loc) · 5.32 KB
/
Encryptions-app.py
File metadata and controls
160 lines (137 loc) · 5.32 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# Encryption Simulator using Streamlit
import streamlit as st
import hashlib
from Crypto.Cipher import AES, DES, PKCS1_OAEP
from Crypto.PublicKey import RSA
from Crypto.Random import get_random_bytes
import base64
# ---------------- Encryption Functions ---------------- #
def xor_cipher(text, key):
return ''.join(chr(ord(c) ^ ord(key)) for c in text)
def hash_text(text, method):
if method == "MD5":
return hashlib.md5(text.encode()).hexdigest()
elif method == "SHA-1":
return hashlib.sha1(text.encode()).hexdigest()
elif method == "SHA-256":
return hashlib.sha256(text.encode()).hexdigest()
return "Unsupported"
def pad(text):
while len(text) % 16 != 0:
text += ' '
return text
def aes_encrypt(text, key):
key = hashlib.sha256(key.encode()).digest()
cipher = AES.new(key, AES.MODE_ECB)
return base64.b64encode(cipher.encrypt(pad(text).encode())).decode()
def aes_decrypt(cipher_text, key):
key = hashlib.sha256(key.encode()).digest()
cipher = AES.new(key, AES.MODE_ECB)
return cipher.decrypt(base64.b64decode(cipher_text)).decode().strip()
def des_encrypt(text, key):
key = key[:8].ljust(8).encode()
cipher = DES.new(key, DES.MODE_ECB)
return base64.b64encode(cipher.encrypt(pad(text).encode())).decode()
def des_decrypt(cipher_text, key):
key = key[:8].ljust(8).encode()
cipher = DES.new(key, DES.MODE_ECB)
return cipher.decrypt(base64.b64decode(cipher_text)).decode().strip()
# RSA key pair (small for demo purposes)
rsa_key = RSA.generate(2048)
rsa_pub_key = rsa_key.publickey()
def rsa_encrypt(text):
cipher = PKCS1_OAEP.new(rsa_pub_key)
return base64.b64encode(cipher.encrypt(text.encode())).decode()
def rsa_decrypt(cipher_text):
cipher = PKCS1_OAEP.new(rsa_key)
return cipher.decrypt(base64.b64decode(cipher_text)).decode()
# ---------------- Streamlit App ---------------- #
st.set_page_config(page_title="Encryption Simulator", layout="wide")
st.title("🔐 Encryption Simulator")
method = st.sidebar.selectbox("Choose Encryption Method", [
"Caesar Cipher",
"XOR Cipher",
"Hashing Algorithms",
"AES",
"DES",
"RSA"
])
# -------------- Caesar Cipher -------------- #
if method == "Caesar Cipher":
st.subheader("Caesar Cipher")
mode = st.radio("Mode", ["Encrypt", "Decrypt"], horizontal=True)
text = st.text_area("Enter text")
shift = st.slider("Shift", 1, 25, 3)
if st.button("Run Caesar Cipher"):
def caesar(text, shift):
result = ""
for c in text:
if c.isalpha():
base = ord('A') if c.isupper() else ord('a')
result += chr((ord(c) - base + shift) % 26 + base)
else:
result += c
return result
shift = shift if mode == "Encrypt" else -shift
st.code(caesar(text, shift))
# -------------- XOR Cipher -------------- #
elif method == "XOR Cipher":
st.subheader("XOR Cipher")
text = st.text_area("Enter text")
key = st.text_input("Enter key (1 character)")
if st.button("Run XOR Cipher"):
if len(key) == 1:
st.code(xor_cipher(text, key))
else:
st.warning("Key must be a single character.")
# -------------- Hashing Algorithms -------------- #
elif method == "Hashing Algorithms":
st.subheader("Hashing Algorithms")
text = st.text_input("Enter text to hash")
algo = st.selectbox("Choose hash algorithm", ["MD5", "SHA-1", "SHA-256"])
if st.button("Hash Text"):
st.code(hash_text(text, algo))
# -------------- AES Encryption -------------- #
elif method == "AES":
st.subheader("AES Encryption")
mode = st.radio("Mode", ["Encrypt", "Decrypt"], horizontal=True)
text = st.text_area("Enter text")
key = st.text_input("Enter key (any length)")
if st.button("Run AES"):
try:
if mode == "Encrypt":
st.code(aes_encrypt(text, key))
else:
st.code(aes_decrypt(text, key))
except Exception as e:
st.error(f"Error: {e}")
# -------------- DES Encryption -------------- #
elif method == "DES":
st.subheader("DES Encryption")
mode = st.radio("Mode", ["Encrypt", "Decrypt"], horizontal=True)
text = st.text_area("Enter text")
key = st.text_input("Enter key (up to 8 characters)")
if st.button("Run DES"):
try:
if mode == "Encrypt":
st.code(des_encrypt(text, key))
else:
st.code(des_decrypt(text, key))
except Exception as e:
st.error(f"Error: {e}")
# -------------- RSA Encryption -------------- #
elif method == "RSA":
st.subheader("RSA Encryption")
mode = st.radio("Mode", ["Encrypt", "Decrypt"], horizontal=True)
text = st.text_area("Enter text")
if st.button("Run RSA"):
try:
if mode == "Encrypt":
st.code(rsa_encrypt(text))
else:
st.code(rsa_decrypt(text))
except Exception as e:
st.error(f"Error: {e}")
# Footer
st.markdown("---")
st.caption("Use the sidebar to switch between encryption methods | Built using Streamlit | Part of my Cybersecurity Projects Portfolio")