Cracking keys function:

- Given n and e, try factoring n and
	generate d if p or q is found.
	works well with 16-bit to 64-bit keys.
	larger keys take vary long which is
	good.

Signed-off-by: HeshamTB <hishaminv@gmail.com>
This commit is contained in:
HeshamTB 2020-04-19 20:24:57 +03:00
parent bfdbca50e7
commit e086a2cdcc
Signed by: Hesham
GPG Key ID: 74876157D199B09E

30
rsa.py
View File

@ -85,9 +85,9 @@ def main():
key_file_name = sys.argv[2]
exportKey(key_file_name)
exit(0)
if sys.argv[1] == "crack":
if sys.argv[1] == "crack": #rsa crack <key>
keyName = sys.argv[2]
cracked_key = crackKey(keyName)
cracked_key = crackKey2(keyName)
printKey(cracked_key)
#No command exit code
exit(127)
@ -283,5 +283,31 @@ def crackKey(keyName):
else: pass
else: pass
def crackKey2(keyName):
print("in crack")
key = readKeyFile(keyName)
n = key[N]
print("n: ", n)
bits = int(n.bit_length())
print("bits: ", bits)
while True:
number = int.from_bytes(os.urandom(int(bits/8)), byteOrder)
if number == 0 or number == 1: continue
print("Trying prime: ", number, end="\r")
# if number devides n then it p or q
if n % number == 0:
print("\nFound a factor")
p = number
print("p: ", p)
q = int(n/p)
phi = (p-1)*(q-1)
if phi == 0: continue
e = 65537
d = pow(e,-1,phi)
key_cracked = (n, e, d, p, q, phi, str(keyName+"-cracked"))
print(key_cracked)
return key_cracked
else: continue
if __name__ == "__main__":
main()