rsa: black formatting, ignore idea files
Signed-off-by: HeshamTB <hishaminv@gmail.com>
This commit is contained in:
parent
8c7bc52d9a
commit
a58f9b9337
2
.gitignore
vendored
2
.gitignore
vendored
@ -9,3 +9,5 @@ build/
|
|||||||
|
|
||||||
#venv dir
|
#venv dir
|
||||||
venv/
|
venv/
|
||||||
|
|
||||||
|
.idea/
|
||||||
|
132
rsa.py
132
rsa.py
@ -28,6 +28,7 @@ Q=4
|
|||||||
PHI = 5
|
PHI = 5
|
||||||
ID = 6
|
ID = 6
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
print("hesham-rsa version ", VERSION)
|
print("hesham-rsa version ", VERSION)
|
||||||
if sys.argv[1] == "gen" and len(sys.argv) == 4: ##rsa gen <keysize> <keyname>
|
if sys.argv[1] == "gen" and len(sys.argv) == 4: ##rsa gen <keysize> <keyname>
|
||||||
@ -56,7 +57,9 @@ def main():
|
|||||||
print(ex)
|
print(ex)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
if sys.argv[1] == "encrypt" and len(sys.argv) == 5: ##rsa encrypt <message> <key> <signer>
|
if (
|
||||||
|
sys.argv[1] == "encrypt" and len(sys.argv) == 5
|
||||||
|
): ##rsa encrypt <message> <key> <signer>
|
||||||
msg = sys.argv[2]
|
msg = sys.argv[2]
|
||||||
msg_list = msg.split()
|
msg_list = msg.split()
|
||||||
keyName = sys.argv[3]
|
keyName = sys.argv[3]
|
||||||
@ -69,7 +72,9 @@ def main():
|
|||||||
msg_encrypted = msg_encrypted + " " + hex(encrypt(word, key_public))
|
msg_encrypted = msg_encrypted + " " + hex(encrypt(word, key_public))
|
||||||
# msg_encrypted = encrypt(msg, key_public)
|
# msg_encrypted = encrypt(msg, key_public)
|
||||||
print("Encrypted msg: \n", msg_encrypted)
|
print("Encrypted msg: \n", msg_encrypted)
|
||||||
print("Signed: \n", sign(msg_encrypted, signing_key)) ## Adds an encrypted sig at the end of message.
|
print(
|
||||||
|
"Signed: \n", sign(msg_encrypted, signing_key)
|
||||||
|
) ## Adds an encrypted sig at the end of message.
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
elif sys.argv[1] == "encrypt":
|
elif sys.argv[1] == "encrypt":
|
||||||
print("Not enough arguments")
|
print("Not enough arguments")
|
||||||
@ -83,7 +88,9 @@ def main():
|
|||||||
msg_decrypted = ""
|
msg_decrypted = ""
|
||||||
key = readKeyFile(sys.argv[3])
|
key = readKeyFile(sys.argv[3])
|
||||||
for cipher_word in cipher_list:
|
for cipher_word in cipher_list:
|
||||||
msg_decrypted = msg_decrypted + " " + str(decrypt(int(cipher_word, 16),key[D],key[N]))
|
msg_decrypted = (
|
||||||
|
msg_decrypted + " " + str(decrypt(int(cipher_word, 16), key[D], key[N]))
|
||||||
|
)
|
||||||
if sig == None:
|
if sig == None:
|
||||||
print("\033[91mUnknown signature! \u2717" + "\033[0m")
|
print("\033[91mUnknown signature! \u2717" + "\033[0m")
|
||||||
else:
|
else:
|
||||||
@ -92,7 +99,7 @@ def main():
|
|||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
elif sys.argv[1] == "decrypt":
|
elif sys.argv[1] == "decrypt":
|
||||||
print("Not enough arguments")
|
print("Not enough arguments")
|
||||||
print("rsa decrypt \"<cipher>\" <keyid>")
|
print('rsa decrypt "<cipher>" <keyid>')
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
if sys.argv[1] == "list":
|
if sys.argv[1] == "list":
|
||||||
listKeys()
|
listKeys()
|
||||||
@ -115,7 +122,9 @@ def main():
|
|||||||
if sys.argv[1] == "is_prime" and len(sys.argv) == 4: # rsa is_prime <base> <N>
|
if sys.argv[1] == "is_prime" and len(sys.argv) == 4: # rsa is_prime <base> <N>
|
||||||
isPrime_cmd(0)
|
isPrime_cmd(0)
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
if sys.argv[1] == "is_prime_mr" and len(sys.argv) == 4: #rsa is_prime_mr <base> <N>
|
if (
|
||||||
|
sys.argv[1] == "is_prime_mr" and len(sys.argv) == 4
|
||||||
|
): # rsa is_prime_mr <base> <N>
|
||||||
isPrime_cmd(1)
|
isPrime_cmd(1)
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
if sys.argv[1] == "genrand" and len(sys.argv) == 3: # rsa genrand <bits>
|
if sys.argv[1] == "genrand" and len(sys.argv) == 3: # rsa genrand <bits>
|
||||||
@ -124,7 +133,9 @@ def main():
|
|||||||
if sys.argv[1] == "genprime" and len(sys.argv) == 3: # rsa genprime <bits>
|
if sys.argv[1] == "genprime" and len(sys.argv) == 3: # rsa genprime <bits>
|
||||||
print(getPrime(int(sys.argv[2])))
|
print(getPrime(int(sys.argv[2])))
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
if sys.argv[1] == "prime_factors" and len(sys.argv) == 4: #rsa primefactors <base> <N>
|
if (
|
||||||
|
sys.argv[1] == "prime_factors" and len(sys.argv) == 4
|
||||||
|
): # rsa primefactors <base> <N>
|
||||||
prime_factors(sys.argv[3], sys.argv[2])
|
prime_factors(sys.argv[3], sys.argv[2])
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
if sys.argv[1] == "print" and len(sys.argv) == 3: # rsa print <key>
|
if sys.argv[1] == "print" and len(sys.argv) == 3: # rsa print <key>
|
||||||
@ -142,9 +153,9 @@ def main():
|
|||||||
sys.exit(127)
|
sys.exit(127)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def generateKeys(id, bits=64):
|
def generateKeys(id, bits=64):
|
||||||
from multiprocessing.pool import Pool
|
from multiprocessing.pool import Pool
|
||||||
|
|
||||||
# Primes of size 32 bit random
|
# Primes of size 32 bit random
|
||||||
# resulting in a 64-bit key mod
|
# resulting in a 64-bit key mod
|
||||||
pool = Pool()
|
pool = Pool()
|
||||||
@ -169,6 +180,7 @@ def generateKeys(id, bits=64):
|
|||||||
d = pow(e, -1, phi) # d = e^-1 mod phi
|
d = pow(e, -1, phi) # d = e^-1 mod phi
|
||||||
return (n, e, d, p, q, phi, id)
|
return (n, e, d, p, q, phi, id)
|
||||||
|
|
||||||
|
|
||||||
def encrypt(message, publicKey):
|
def encrypt(message, publicKey):
|
||||||
msg_text = message
|
msg_text = message
|
||||||
n = publicKey[N]
|
n = publicKey[N]
|
||||||
@ -181,25 +193,29 @@ def encrypt(message, publicKey):
|
|||||||
msg_encrypted_number_form = pow(msg_number_form, e, n) # c = msg^e mod n
|
msg_encrypted_number_form = pow(msg_number_form, e, n) # c = msg^e mod n
|
||||||
return msg_encrypted_number_form
|
return msg_encrypted_number_form
|
||||||
|
|
||||||
|
|
||||||
def decrypt(cipher, privateKey, n):
|
def decrypt(cipher, privateKey, n):
|
||||||
msg_encrypted_number_form = cipher
|
msg_encrypted_number_form = cipher
|
||||||
d = privateKey
|
d = privateKey
|
||||||
msg_decrypted_number_form = pow(msg_encrypted_number_form, d, n) # msg = c^d mod n
|
msg_decrypted_number_form = pow(msg_encrypted_number_form, d, n) # msg = c^d mod n
|
||||||
msg_decrypted = int(msg_decrypted_number_form)
|
msg_decrypted = int(msg_decrypted_number_form)
|
||||||
try:
|
try:
|
||||||
msg_decrypted = str(msg_decrypted.to_bytes(msg_decrypted.bit_length(), byteOrder).decode()).strip()
|
msg_decrypted = str(
|
||||||
|
msg_decrypted.to_bytes(msg_decrypted.bit_length(), byteOrder).decode()
|
||||||
|
).strip()
|
||||||
except UnicodeDecodeError:
|
except UnicodeDecodeError:
|
||||||
# print("decrypt: Cant decrypt properly")
|
# print("decrypt: Cant decrypt properly")
|
||||||
return ""
|
return ""
|
||||||
return msg_decrypted
|
return msg_decrypted
|
||||||
|
|
||||||
|
|
||||||
def getPrime(bits):
|
def getPrime(bits):
|
||||||
while True:
|
while True:
|
||||||
# Byte order "little" or "big" does not matter here since we want a random number from os.urandom()
|
# Byte order "little" or "big" does not matter here since we want a random number from os.urandom()
|
||||||
x = int.from_bytes(os.urandom(int(bits / 8)), byteOrder)
|
x = int.from_bytes(os.urandom(int(bits / 8)), byteOrder)
|
||||||
print('Trying: ', x, end="\n")
|
print("Trying: ", x, end="\n")
|
||||||
if mr.is_prime(x):
|
if mr.is_prime(x):
|
||||||
print("\nprime: ", x, '\n')
|
print("\nprime: ", x, "\n")
|
||||||
return x
|
return x
|
||||||
# backTrack(x)
|
# backTrack(x)
|
||||||
|
|
||||||
@ -226,10 +242,12 @@ def isPrime(number):
|
|||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def gen_random(bits: int):
|
def gen_random(bits: int):
|
||||||
x = int.from_bytes(os.urandom(int(bits / 8)), byteOrder)
|
x = int.from_bytes(os.urandom(int(bits / 8)), byteOrder)
|
||||||
return x
|
return x
|
||||||
|
|
||||||
|
|
||||||
def sign(encrypted_msg, key):
|
def sign(encrypted_msg, key):
|
||||||
enc_msg = str(encrypted_msg)
|
enc_msg = str(encrypted_msg)
|
||||||
encrypted_msg_list = enc_msg.split()
|
encrypted_msg_list = enc_msg.split()
|
||||||
@ -240,6 +258,7 @@ def sign(encrypted_msg, key):
|
|||||||
signed_msg = str(signed_msg) + " " + str(word)
|
signed_msg = str(signed_msg) + " " + str(word)
|
||||||
return signed_msg.strip()
|
return signed_msg.strip()
|
||||||
|
|
||||||
|
|
||||||
def verify(cipher_list):
|
def verify(cipher_list):
|
||||||
local_keys = os.listdir(keysFolder)
|
local_keys = os.listdir(keysFolder)
|
||||||
cipher_list.reverse() # To get last word using index 0
|
cipher_list.reverse() # To get last word using index 0
|
||||||
@ -252,11 +271,13 @@ def verify(cipher_list):
|
|||||||
sig = str(decrypt(int(encrypted_sig, 16), key[E], key[N]))
|
sig = str(decrypt(int(encrypted_sig, 16), key[E], key[N]))
|
||||||
if "sig:" in sig:
|
if "sig:" in sig:
|
||||||
return sig.replace("sig:", "")
|
return sig.replace("sig:", "")
|
||||||
else: continue
|
else:
|
||||||
else: return None
|
continue
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
def isPrime_cmd(func):
|
def isPrime_cmd(func):
|
||||||
|
|
||||||
number = int_base_n_from_str(sys.argv[3], sys.argv[2])
|
number = int_base_n_from_str(sys.argv[3], sys.argv[2])
|
||||||
|
|
||||||
if func == 0:
|
if func == 0:
|
||||||
@ -265,13 +286,13 @@ def isPrime_cmd(func):
|
|||||||
prime = mr.is_prime(number)
|
prime = mr.is_prime(number)
|
||||||
|
|
||||||
if prime:
|
if prime:
|
||||||
print('Prime')
|
print("Prime")
|
||||||
# print(number)
|
# print(number)
|
||||||
else:
|
else:
|
||||||
print('Not prime')
|
print("Not prime")
|
||||||
|
|
||||||
|
|
||||||
def prime_factors(number, base):
|
def prime_factors(number, base):
|
||||||
|
|
||||||
num = int_base_n_from_str(number, base)
|
num = int_base_n_from_str(number, base)
|
||||||
factors = {1: 1}
|
factors = {1: 1}
|
||||||
k = 0
|
k = 0
|
||||||
@ -283,7 +304,7 @@ def prime_factors(number, base):
|
|||||||
|
|
||||||
for i in range(3, int(math.sqrt(num)) + 1, 2):
|
for i in range(3, int(math.sqrt(num)) + 1, 2):
|
||||||
j = 0
|
j = 0
|
||||||
while (num % i == 0):
|
while num % i == 0:
|
||||||
j += 1
|
j += 1
|
||||||
num = num / i
|
num = num / i
|
||||||
if j != 0:
|
if j != 0:
|
||||||
@ -292,20 +313,32 @@ def prime_factors(number, base):
|
|||||||
factors.update({int(num): 1})
|
factors.update({int(num): 1})
|
||||||
|
|
||||||
print(factors)
|
print(factors)
|
||||||
|
|
||||||
|
|
||||||
def readKeyFile(keyName):
|
def readKeyFile(keyName):
|
||||||
key = tuple()
|
key = tuple()
|
||||||
with open(keysFolder + keyName, "r") as keyFile:
|
with open(keysFolder + keyName, "r") as keyFile:
|
||||||
tempkey = keyFile.readlines()
|
tempkey = keyFile.readlines()
|
||||||
if len(tempkey) == 3: # means it only public part (n, e, id)
|
if len(tempkey) == 3: # means it only public part (n, e, id)
|
||||||
key = (int(tempkey[N].strip(), 16), int(tempkey[E].strip(), 16), 0, 0, 0, 0, tempkey[2])
|
key = (
|
||||||
|
int(tempkey[N].strip(), 16),
|
||||||
|
int(tempkey[E].strip(), 16),
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
tempkey[2],
|
||||||
|
)
|
||||||
else: # Make this a loop from 0 to 5
|
else: # Make this a loop from 0 to 5
|
||||||
key = (int(tempkey[N].strip(), 16),
|
key = (
|
||||||
|
int(tempkey[N].strip(), 16),
|
||||||
int(tempkey[E].strip(), 16),
|
int(tempkey[E].strip(), 16),
|
||||||
int(tempkey[D].strip(), 16),
|
int(tempkey[D].strip(), 16),
|
||||||
int(tempkey[P].strip(), 16),
|
int(tempkey[P].strip(), 16),
|
||||||
int(tempkey[Q].strip(), 16),
|
int(tempkey[Q].strip(), 16),
|
||||||
int(tempkey[PHI].strip(), 16),
|
int(tempkey[PHI].strip(), 16),
|
||||||
str(tempkey[ID].strip()))
|
str(tempkey[ID].strip()),
|
||||||
|
)
|
||||||
return key
|
return key
|
||||||
|
|
||||||
|
|
||||||
@ -320,21 +353,24 @@ def saveKeyFile(key, fileName):
|
|||||||
pass
|
pass
|
||||||
keyFile.write(key[ID] + "\n")
|
keyFile.write(key[ID] + "\n")
|
||||||
|
|
||||||
|
|
||||||
def printKey(key):
|
def printKey(key):
|
||||||
n = key[N]
|
n = key[N]
|
||||||
e = key[E]
|
e = key[E]
|
||||||
d = key[D]
|
d = key[D]
|
||||||
id = key[ID]
|
id = key[ID]
|
||||||
print("----------------------------------------------"+
|
print(
|
||||||
"\nID: {}".format(id) +
|
"----------------------------------------------"
|
||||||
"\n{}-BIT KEY".format(n.bit_length())+
|
+ "\nID: {}".format(id)
|
||||||
"\nPUBLIC PART:"+
|
+ "\n{}-BIT KEY".format(n.bit_length())
|
||||||
"\n{0}/{1}".format(hex(n), hex(e))+
|
+ "\nPUBLIC PART:"
|
||||||
"\nPTIVATE PART:"+
|
+ "\n{0}/{1}".format(hex(n), hex(e))
|
||||||
"\n{0}".format(hex(d))+
|
+ "\nPTIVATE PART:"
|
||||||
"\n----------------------------------------------",
|
+ "\n{0}".format(hex(d))
|
||||||
|
+ "\n----------------------------------------------",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def listKeys():
|
def listKeys():
|
||||||
if not os.path.isdir(keysFolder):
|
if not os.path.isdir(keysFolder):
|
||||||
os.makedirs(keysFolder)
|
os.makedirs(keysFolder)
|
||||||
@ -348,15 +384,18 @@ def listKeys():
|
|||||||
key = readKeyFile(keyName)
|
key = readKeyFile(keyName)
|
||||||
if key[D] == 0:
|
if key[D] == 0:
|
||||||
check = "".strip()
|
check = "".strip()
|
||||||
else: check = '\u2713'
|
else:
|
||||||
|
check = "\u2713"
|
||||||
print("%10s%7s%7s-bit" % (key[ID].strip(), check, key[N].bit_length()))
|
print("%10s%7s%7s-bit" % (key[ID].strip(), check, key[N].bit_length()))
|
||||||
|
|
||||||
|
|
||||||
def exportKey(keyFileName):
|
def exportKey(keyFileName):
|
||||||
key = readKeyFile(keyFileName)
|
key = readKeyFile(keyFileName)
|
||||||
public_key = (key[N], key[E], 0, 0, 0, 0, key[ID])
|
public_key = (key[N], key[E], 0, 0, 0, 0, key[ID])
|
||||||
saveKeyFile(public_key, key[ID] + "-public")
|
saveKeyFile(public_key, key[ID] + "-public")
|
||||||
print("Saved public form of key {} in keys folder".format(key[ID]))
|
print("Saved public form of key {} in keys folder".format(key[ID]))
|
||||||
|
|
||||||
|
|
||||||
def crackKey(keyName):
|
def crackKey(keyName):
|
||||||
print("in crack")
|
print("in crack")
|
||||||
key = readKeyFile(keyName)
|
key = readKeyFile(keyName)
|
||||||
@ -373,8 +412,11 @@ def crackKey(keyName):
|
|||||||
d = pow(e, -1, phi)
|
d = pow(e, -1, phi)
|
||||||
key_cracked = (n, e, d, p, q, phi, str(keyName + "-cracked"))
|
key_cracked = (n, e, d, p, q, phi, str(keyName + "-cracked"))
|
||||||
return key_cracked
|
return key_cracked
|
||||||
else: pass
|
else:
|
||||||
else: pass
|
pass
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
def crackKey2(keyName):
|
def crackKey2(keyName):
|
||||||
print("in crack")
|
print("in crack")
|
||||||
@ -385,7 +427,8 @@ def crackKey2(keyName):
|
|||||||
print("bits: ", bits)
|
print("bits: ", bits)
|
||||||
while True:
|
while True:
|
||||||
number = int.from_bytes(os.urandom(int(bits / 8)), byteOrder)
|
number = int.from_bytes(os.urandom(int(bits / 8)), byteOrder)
|
||||||
if number == 0 or number == 1: continue
|
if number == 0 or number == 1:
|
||||||
|
continue
|
||||||
print("Trying prime: ", number, end="\r")
|
print("Trying prime: ", number, end="\r")
|
||||||
# if number devides n then it p or q
|
# if number devides n then it p or q
|
||||||
if n % number == 0:
|
if n % number == 0:
|
||||||
@ -394,24 +437,28 @@ def crackKey2(keyName):
|
|||||||
print("p: ", p)
|
print("p: ", p)
|
||||||
q = int(n / p)
|
q = int(n / p)
|
||||||
phi = (p - 1) * (q - 1)
|
phi = (p - 1) * (q - 1)
|
||||||
if phi == 0: continue
|
if phi == 0:
|
||||||
|
continue
|
||||||
e = 65537
|
e = 65537
|
||||||
d = pow(e, -1, phi)
|
d = pow(e, -1, phi)
|
||||||
key_cracked = (n, e, d, p, q, phi, str(keyName + "-cracked"))
|
key_cracked = (n, e, d, p, q, phi, str(keyName + "-cracked"))
|
||||||
print(key_cracked)
|
print(key_cracked)
|
||||||
return key_cracked
|
return key_cracked
|
||||||
else: continue
|
else:
|
||||||
|
continue
|
||||||
|
|
||||||
|
|
||||||
def printHelp():
|
def printHelp():
|
||||||
print("commands:")
|
print("commands:")
|
||||||
print("rsa gen <keysize> <keyname>")
|
print("rsa gen <keysize> <keyname>")
|
||||||
print("rsa encrypt <message> <key> <signer>")
|
print("rsa encrypt <message> <key> <signer>")
|
||||||
print("rsa decrypt \"<cipher>\" <key>")
|
print('rsa decrypt "<cipher>" <key>')
|
||||||
print("rsa export <key>")
|
print("rsa export <key>")
|
||||||
print("rsa crack <key>")
|
print("rsa crack <key>")
|
||||||
print("rsa print <key>")
|
print("rsa print <key>")
|
||||||
print("rsa list")
|
print("rsa list")
|
||||||
|
|
||||||
|
|
||||||
def backTrack(x):
|
def backTrack(x):
|
||||||
# Back track and clear terminal with length of x
|
# Back track and clear terminal with length of x
|
||||||
length = len(str(x))
|
length = len(str(x))
|
||||||
@ -419,25 +466,30 @@ def backTrack(x):
|
|||||||
print("\b", end="")
|
print("\b", end="")
|
||||||
length -= 1
|
length -= 1
|
||||||
|
|
||||||
|
|
||||||
def keyExist(keyName):
|
def keyExist(keyName):
|
||||||
exist = os.path.exists(keysFolder + keyName)
|
exist = os.path.exists(keysFolder + keyName)
|
||||||
return exist
|
return exist
|
||||||
|
|
||||||
def int_base_n_from_str(st: str, base):
|
|
||||||
|
|
||||||
|
def int_base_n_from_str(st: str, base):
|
||||||
try:
|
try:
|
||||||
base = int(base)
|
base = int(base)
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
print(f'Value {sys.argv[2]} is not a valid base (2, 8, 10, 16)',
|
print(
|
||||||
file=sys.stderr)
|
f"Value {sys.argv[2]} is not a valid base (2, 8, 10, 16)", file=sys.stderr
|
||||||
|
)
|
||||||
exit(-1)
|
exit(-1)
|
||||||
try:
|
try:
|
||||||
number = int(st, base)
|
number = int(st, base)
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
print(f'Value {sys.argv[3]} is not valid for as a base {base} number',
|
print(
|
||||||
file=sys.stderr)
|
f"Value {sys.argv[3]} is not valid for as a base {base} number",
|
||||||
|
file=sys.stderr,
|
||||||
|
)
|
||||||
exit(-1)
|
exit(-1)
|
||||||
return number
|
return number
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
Loading…
Reference in New Issue
Block a user