-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhelpers.py
More file actions
107 lines (75 loc) · 2.43 KB
/
Copy pathhelpers.py
File metadata and controls
107 lines (75 loc) · 2.43 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
import tkinter as tk
from tkinter import messagebox
def factor_with_primes(n, primes):
for p in primes:
if p * p > n:
break
if n % p == 0:
return p, n // p
return None
def break_rsa_with_primes(n, e, primes):
factors = factor_with_primes(n, primes)
if not factors:
raise ValueError("No small prime factor found")
p, q = factors
phi = (p - 1) * (q - 1)
d = pow(e, -1, phi)
return d
def read_primes_from_file(filename):
primes = []
with open(filename, "r") as f:
for line in f:
# Split by whitespace
for token in line.split():
# Keep only numbers
if token.isdigit():
primes.append(int(token))
return primes
def rsa_encrypt(m, n, e):
if m >= n:
raise ValueError("Message " + str(m) + " is too large for modulus " + str(n))
return pow(m, e, n)
def rsa_encrypt_text(message, n, e):
# Convert text to integer
m = int.from_bytes(message.encode("utf-8"), byteorder="big")
return rsa_encrypt(m, n, e)
def rsa_decrypt(c, d, n):
return pow(c, d, n)
def rsa_decrypt_text(c, d, n):
m = rsa_decrypt(c, d, n)
# Convert integer back to bytes
length = (m.bit_length() + 7) // 8
return m.to_bytes(length, byteorder="big").decode("utf-8")
def display_gui(default_text="Hello"):
result = {"value": None} # mutable container
def submit():
result["value"] = entry.get()
root.destroy()
root = tk.Tk()
root.title("RSA Message Encryption")
root.geometry("350x150")
root.resizable(False, False)
lbl = tk.Label(root, text="Message to encrypt")
lbl.pack(pady=10)
entry = tk.Entry(root, width=40)
entry.pack(pady=5)
entry.insert(0, default_text)
entry.focus()
btn = tk.Button(root, text="OK", command=submit)
btn.pack(pady=10)
root.mainloop()
return result["value"]
def show_text_gui(message, title="Result", label="Encrypted message:"):
root = tk.Tk()
root.title(title)
root.geometry("400x200")
root.resizable(False, False)
lbl = tk.Label(root, text=label)
lbl.pack(pady=10)
text_box = tk.Text(root, height=4, width=45, wrap="word")
text_box.pack(pady=5)
text_box.insert("1.0", str(message))
text_box.config(state="disabled") # read‑only
btn = tk.Button(root, text="OK", command=root.destroy)
btn.pack(pady=10)
root.mainloop()