-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinteractive.py
More file actions
67 lines (55 loc) · 1.46 KB
/
Copy pathinteractive.py
File metadata and controls
67 lines (55 loc) · 1.46 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
from math import gcd
from rich.console import Console
from rich.panel import Panel
from rich.prompt import IntPrompt
from rich.table import Table
console = Console()
# Header
console.print(
Panel.fit(
"[bold cyan]RSA Encryption & Decryption[/bold cyan]",
border_style="green"
)
)
# Input Prime Numbers
p = IntPrompt.ask("[yellow]Enter 1st Prime Number[/yellow]")
q = IntPrompt.ask("[yellow]Enter 2nd Prime Number[/yellow]")
# Computation of modulus and Euler's Totient
n = p * q
phi = (p - 1) * (q - 1)
# Selecting public exponent [e]
e = 2
while e < phi:
if gcd(e, phi) == 1:
break
e += 1
# Computation of private exponent [d]
d = pow(e, -1, phi)
# Key Display Table
key_table = Table(title="Generated RSA Keys", header_style="bold magenta")
key_table.add_column("Key Type", justify="center")
key_table.add_column("Value", justify="center")
key_table.add_row("Public Key (e, n)", str((e, n)))
key_table.add_row("Private Key (d, n)", str((d, n)))
console.print()
console.print(key_table)
# Message Input
msg = IntPrompt.ask(f"\n[cyan]Enter message (number < {n})[/cyan]")
# Encryption
cipher = pow(msg, e, n)
console.print(
Panel(
f"[bold green]{cipher}[/bold green]",
title="Encrypted Message",
border_style="green"
)
)
# Decryption
plain = pow(cipher, d, n)
console.print(
Panel(
f"[bold bright_white]{plain}[/bold bright_white]",
title="Decrypted Message",
border_style="blue"
)
)