Without Comments Rsa: #!/usr/bin/python3
Without Comments Rsa: #!/usr/bin/python3
Rsa
#!/usr/bin/python3
import sys
import random
import time
d = num - 1
r = 0
while d % 2 == 0:
d //= 2
r += 1
for _ in range(k):
a = random.randint(2, num - 2)
x = pow(a, d, num)
if x == 1 or x == num - 1:
continue
for _ in range(r - 1):
x = pow(x, 2, num)
if x == num - 1:
break
else:
return False
return True
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: rsa <file>")
exit()
input_file = sys.argv[1]
try:
with open(input_file, 'r') as f:
lines = f.readlines()
except FileNotFoundError:
print("File not found")
exit()
start_time = time.time()
num = int(lines[0].strip())
With comments
Rsa
# This is a Python script that factors large RSA numbers using the trial
division method
import sys
import time
from math import sqrt, ceil
# Define a function to factor a single RSA number using the trial division
method
def factor_rsa_number(n):
# Initialize an empty list to store the prime factors of n
factors = []
# Check if 2 is a factor of n
while n % 2 == 0:
factors.append(2)
n //= 2
# Check for odd prime factors up to sqrt(n)
i = 3
while i <= sqrt(n):
if n % i == 0:
factors.append(i)
n //= i
else:
i += 2
# If n is still greater than 2, it must be prime
if n > 2:
factors.append(n)
# If only one factor is found, return it as p and n//p as q
if len(factors) == 1:
p = factors[0]
q = n // p
return (p, q)
# If more than one factor is found, recursively factor each factor
until all are prime
else:
pq = []
for factor in factors:
if is_prime(factor):
pq.append(factor)
else:
p, q = factor_rsa_number(factor)
pq.append(p)
pq.append(q)
return tuple(pq)
# Read the input file name from the command line arguments
input_file = sys.argv[1]
# Factor each RSA number in the file using the trial division method
for n in rsa_numbers:
p, q = factor_rsa_number(n)
print(f"{n}={q}*{p}")