-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.py
More file actions
239 lines (190 loc) · 9.28 KB
/
app.py
File metadata and controls
239 lines (190 loc) · 9.28 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import streamlit as st
import pandas as pd
import string
import math
import ciphers
import graphviz
import altair as alt
st.set_page_config(page_title="Cryptography Simulator", layout="wide")
st.title("🔐 Cryptography Simulator")
st.markdown("**Developed by Muhammad Almas (for educational simulation purposes only)**")
# English benchmark distribution (percentages)
ENGLISH_FREQ = {
'A': 8.2, 'B': 1.5, 'C': 2.8, 'D': 4.3, 'E': 13.0,
'F': 2.2, 'G': 2.0, 'H': 6.1, 'I': 7.0, 'J': 0.15,
'K': 0.77, 'L': 4.0, 'M': 2.4, 'N': 6.7, 'O': 7.5,
'P': 1.9, 'Q': 0.095,'R': 6.0, 'S': 6.3, 'T': 9.1,
'U': 2.8, 'V': 0.98,'W': 2.4, 'X': 0.15,'Y': 2.0,
'Z': 0.074
}
LETTERS = list(string.ascii_uppercase)
# ------- helper functions -------
def counts_to_percent(counts):
total = sum(counts.values()) or 1
return {k: (v / total) * 100 for k, v in counts.items()}
def get_letter_counts_from_text(text):
text = (text or "").upper()
counts = {ch: 0 for ch in LETTERS}
for ch in text:
if ch in counts:
counts[ch] += 1
return counts
def coprimes_with_26():
return [a for a in range(1, 26) if math.gcd(a, 26) == 1]
# ------- Layout: Tabs for separation -------
tab_cipher, tab_analysis, tab_block = st.tabs(["Cipher Schemes", "Cryptoanalysis", "Block Ciphers"])
# ----------------- Cipher Schemes Tab -----------------
with tab_cipher:
st.header("Cipher Schemes")
col1, col2 = st.columns([2, 1])
with col1:
algorithm = st.selectbox("Choose cipher", ["Caesar", "ROT13", "Affine", "Rail Fence", "Columnar"])
input_text = st.text_area("Input text (plaintext or ciphertext)", height=180)
with col2:
st.markdown("### Parameters")
if algorithm == "Caesar":
shift = st.number_input("Shift", value=3, step=1)
elif algorithm == "Affine":
a = st.number_input("a (must be coprime with 26)", value=5, step=1)
b = st.number_input("b", value=8, step=1)
elif algorithm == "Rail Fence":
rails = st.number_input("Rails (key)", value=2, min_value=2, step=1)
elif algorithm == "Columnar":
col_key = st.text_input("Key (columnar)", value="HACK")
operation = st.radio("Operation", ["Encrypt", "Decrypt"])
if st.button("Run"):
if algorithm == "Caesar":
func = ciphers.caesar_encrypt if operation == "Encrypt" else ciphers.caesar_decrypt
result = func(input_text, shift)
elif algorithm == "ROT13":
result = ciphers.rot13(input_text)
elif algorithm == "Affine":
func = ciphers.affine_encrypt if operation == "Encrypt" else ciphers.affine_decrypt
result = func(input_text, a, b)
elif algorithm == "Rail Fence":
func = ciphers.rail_fence_encrypt if operation == "Encrypt" else ciphers.rail_fence_decrypt
result = func(input_text, rails)
elif algorithm == "Columnar":
func = ciphers.columnar_encrypt if operation == "Encrypt" else ciphers.columnar_decrypt
result = func(input_text, col_key)
else:
result = "Unsupported option."
st.subheader("Result")
st.code(result)
# ----------------- Cryptoanalysis Tab -----------------
with tab_analysis:
st.header("Cryptoanalysis Techniques")
fa_tab, bf_tab = st.tabs(["Frequency Analysis", "Brute-force / Exhaustive Search"])
with fa_tab:
st.subheader("Frequency Analysis (with English benchmark)")
fa_text = st.text_area("Enter ciphertext to analyze", height=200, key="fa_text")
if st.button("Analyze Frequency", key="analyze_freq"):
if not fa_text.strip():
st.warning("Please enter some text to analyze.")
else:
counts = get_letter_counts_from_text(fa_text)
perc = counts_to_percent(counts)
df = pd.DataFrame({
"Letter": LETTERS,
"Count": [counts[ch] for ch in LETTERS],
"Ciphertext %": [perc[ch] for ch in LETTERS],
"English %": [ENGLISH_FREQ[ch] for ch in LETTERS]
}).set_index("Letter")
st.subheader("Frequency Table")
st.dataframe(df, use_container_width=True)
# --- prepare long-form DF for grouped bar chart
letters = df.reset_index()['Letter'].tolist()
df_long = df.reset_index().melt(
id_vars='Letter',
value_vars=['Ciphertext %', 'English %'],
var_name='Distribution',
value_name='Frequency'
)
# --- Grouped side-by-side bar chart
chart = alt.Chart(df_long).mark_bar().encode(
x=alt.X('Letter:N', sort=letters, title='Letter'),
xOffset='Distribution:N',
y=alt.Y('Frequency:Q', title='Frequency (%)'),
color=alt.Color('Distribution:N',
scale=alt.Scale(domain=['Ciphertext %','English %'],
range=['#1f77b4','#ff7f0e'])),
tooltip=['Letter','Distribution', alt.Tooltip('Frequency:Q', format='.2f')]
).properties(width=800, height=360)
st.subheader("Distribution Comparison")
st.altair_chart(chart, use_container_width=True)
# --- Normalized frequency vector
st.subheader("Normalized Frequencies (F(p))")
total_letters = df['Count'].sum() or 1
st.write(f"Total alphabetic letters counted = {total_letters}")
F_table = pd.DataFrame({
'Letter': df.index,
'Count': df['Count'].values,
'F(p) %': df['Ciphertext %'].values
}).set_index('Letter')
st.dataframe(F_table, use_container_width=True)
# Brute-force Affine/Caesar left as-is
with bf_tab:
st.subheader("Brute-force & Exhaustive Key Search Simulations")
st.info("This section is unchanged — only Frequency Analysis plotting updated.")
# ----------------- Block Ciphers Tab -----------------
with tab_block:
st.header("Modern Block Ciphers (DES Simulation)")
des_text = st.text_input("Enter plaintext (e.g., A):", value="A", key="des_plaintext")
# key = st.text_input("Key (8 characters)", value="12345678", max_chars=8, key="des_key")
if st.button("Run DES Simulation", key="des_button"):
from ciphers import des_simulate
steps = des_simulate(des_text, key="12345678")
for step, value in steps.items():
st.subheader(step)
st.write(value)
# with tab_block:
# st.header("Modern Block Ciphers (DES Simulation)")
# des_text = st.text_input("Enter plaintext (e.g., A):", value="A")
# key = st.text_input("Key (8 characters)", value="12345678", max_chars=8)
# if st.button("Run DES Simulation"):
# from ciphers import des_simulate
# steps = des_simulate(des_text, key)
# for step, value in steps.items():
# st.subheader(step)
# st.write(value)
# --- Optional: Animation of DES steps ---
import time
if st.checkbox("Show DES Flow Animation"):
st.subheader("DES Process Animation")
stages = [
"1. ASCII → Binary",
"2. Pad to 64-bit block",
"3. Initial Permutation",
"4. Split into L0 and R0",
"5. 16 Feistel Rounds",
"6. Swap Halves",
"7. Final Permutation",
"8. Ciphertext"
]
progress_bar = st.progress(0)
status_text = st.empty()
for i, stage in enumerate(stages):
status_text.text(stage)
progress_bar.progress((i + 1) / len(stages))
time.sleep(0.8)
st.success("DES Simulation Complete ✅")
# --- Optional: DES Flow Diagram ---
if st.checkbox("Show DES Flow Diagram", key="des_diag"):
import graphviz
dot = graphviz.Digraph(format="png")
dot.attr(rankdir="LR", size="8") # Left to right flow
# Nodes
dot.node("PT", "Plaintext\n(64-bit)", shape="box", style="filled", color="lightblue")
dot.node("IP", "Initial Permutation\n(IP)", shape="box", style="rounded,filled", color="lightyellow")
dot.node("Split", "Split into L0 (32) | R0 (32)", shape="parallelogram", style="filled", color="lightgrey")
dot.node("Rounds", "16 Feistel Rounds\n(L,R updates + Subkeys)", shape="box3d", style="filled", color="lightgreen")
dot.node("Swap", "Swap Halves", shape="ellipse", style="filled", color="lightpink")
dot.node("FP", "Final Permutation\n(IP⁻¹)", shape="box", style="rounded,filled", color="lightyellow")
dot.node("CT", "Ciphertext\n(64-bit)", shape="box", style="filled", color="lightblue")
# Edges
dot.edges([("PT", "IP"), ("IP", "Split"), ("Split", "Rounds"),
("Rounds", "Swap"), ("Swap", "FP"), ("FP", "CT")])
st.graphviz_chart(dot)
# ----------------- Footer -----------------
st.markdown("---")
st.markdown("**Tips:** Classical = old ciphers, Cryptoanalysis = attacks, Block Ciphers = modern algorithms like DES (insecure in practice, but good for teaching).")