0% found this document useful (0 votes)
10 views4 pages

Without Comments Rsa: #!/usr/bin/python3

The document contains two Python scripts for factoring RSA numbers. The first script uses a trial division method with a prime-checking function, while the second script is more detailed, implementing a similar approach with additional functionality to handle multiple factors. Both scripts read RSA numbers from an input file and output the factors along with execution time.

Uploaded by

Desalegn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views4 pages

Without Comments Rsa: #!/usr/bin/python3

The document contains two Python scripts for factoring RSA numbers. The first script uses a trial division method with a prime-checking function, while the second script is more detailed, implementing a similar approach with additional functionality to handle multiple factors. Both scripts read RSA numbers from an input file and output the factors along with execution time.

Uploaded by

Desalegn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Without comments

Rsa

#!/usr/bin/python3

import sys
import random
import time

def is_prime(num, k=20):


'''
'''
if num == 2 or num == 3:
return True
if num < 2 or num % 2 == 0:
return False

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())

for i in range(2, num//2):


if num % i == 0 and is_prime(i) and is_prime(num//i):
print(f"{num}={i}*{num//i}")
break

if time.time() - start_time > 5:


print("Time limit exceeded")
exit()

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 check if a number is prime


def is_prime(n):
if n < 2:
return False
for i in range(2, int(sqrt(n)) + 1):
if n % i == 0:
return False
return True

# 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]

# Read the RSA numbers from the input file


with open(input_file, 'r') as f:
rsa_numbers = [int(line.strip()) for line in f.readlines()]

# Start a timer to measure the execution time


start_time = time.time()

# 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}")

# Stop the timer and calculate the execution time


end_time = time.time()
exec_time = end_time - start_time

# Print the execution time in seconds


print("Total execution time: ", exec_time, " seconds")

You might also like