Created
March 28, 2022 08:40
-
-
Save acheong08/89c3e5de6277efb43e68f3535f9b9769 to your computer and use it in GitHub Desktop.
Threaded RSA cracker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Importing modules | |
from time import perf_counter | |
from hashlib import md5 | |
from random import randint | |
from multiprocessing import Queue, Process, Pool | |
import sys | |
# Global variables | |
q = Queue() | |
# Check if prime is factor of RSA number | |
def rsaFcheck(RSAnum, prime): | |
if RSAnum % prime == 0: | |
factors = (prime, (RSAnum / prime)) | |
return True | |
return False | |
# Check RSA factors main | |
def crack(RSAnum, down, up): | |
for i in range(down+1, up): | |
cand = (2*i) + 1 | |
if rsaFcheck(RSAnum, cand): | |
factor1, factor2 = (cand, int(RSAnum / cand)) | |
print(str("The prime factors are: " + str(factor1) + ", " + str(factor2))) | |
q.put((factor1, factor2)) | |
break | |
if not q.empty(): | |
break | |
# Main threading | |
def main(): | |
if len(sys.argv) != 3: | |
sys.exit("Usage: python3 RSAcrack.py <RSA number> <Thread number>") | |
RSAnum = int(sys.argv[1]) | |
threadNum = int(sys.argv[2]) | |
# Generate range numbers | |
minimum = int(10 ** int((len(str(RSAnum)) - 2) / 2)) | |
maximum = int((10 ** (int(len(str(RSAnum)) + 1) / 2)) -1) | |
inc = maximum / threadNum | |
# multiprocessing | |
threads = [] | |
pool = Pool() | |
for i in range(1,int(threadNum+1)): | |
down = minimum + int(inc*(i-1)) | |
up = int(inc*i) | |
thread = pool.apply_async(func = crack, args = (RSAnum, down, up)) | |
threads.append(thread) | |
pool.close() | |
pool.join() | |
if __name__ == '__main__': | |
tic = perf_counter() | |
main() | |
toc = perf_counter() | |
print("Execution time: " + str(toc - tic)) | |
sys.exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment