-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrsa
54 lines (45 loc) · 1.34 KB
/
rsa
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
#!/usr/bin/python3
import math
def rsa_factoring(n):
"""
The function receives a number (n) and returns its two prime factors (p and q) such that n = p * q.
Args:
n (int): An integer that is the product of two prime numbers.
Returns:
tuple: A tuple containing two prime numbers p and q such that n = p * q.
"""
def is_prime(num):
"""
Check if a number is prime or not.
Args:
num (int): Number to check.
Returns:
bool: True if the number is prime, False otherwise.
"""
if num < 2:
return False
for i in range(2, math.isqrt(num) + 1):
if num % i == 0:
return False
return True
for i in range(2, math.isqrt(n) + 1):
if n % i == 0:
factor = n // i
if is_prime(i) and is_prime(factor):
return (i, factor)
return None
def main():
"""
Read input file and perform the RSA factoring on the number present in the file.
"""
import sys
if len(sys.argv) < 2:
print("Usage: rsa.py <file>")
return
file_path = sys.argv[1]
with open(file_path, 'r') as file:
n = int(file.read().strip())
p, q = rsa_factoring(n)
print(f"{n}={max(p, q)}*{min(p, q)}")
if __name__ == "__main__":
main()