Improved and accurate signature output

Signed-off-by: HeshamTB <hishaminv@gmail.com>
This commit is contained in:
HeshamTB 2020-08-28 17:22:35 +03:00
parent c121314eb9
commit 7ae3e4dc04
Signed by: Hesham
GPG Key ID: 74876157D199B09E

27
rsa.py Executable file → Normal file
View File

@ -75,22 +75,26 @@ def main():
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("Unknown signature!") print("\033[91mUnknown signature! \u2717" + "\033[0m")
else: else:
print("Signed by: ", sig) print("Signed by: \033[92m " + sig + " \u2713\033[0m")
print("Decrypted message: \n", msg_decrypted) print("Decrypted message: \n", msg_decrypted)
else: else:
print("Not enough arguments") print("Not enough arguments")
print("rsa decrypt \"<cipher>\" <key>") print("rsa decrypt \"<cipher>\" <keyid>")
exit(0) exit(0)
if sys.argv[1] == "list": if sys.argv[1] == "list":
listKeys() listKeys()
exit(0) exit(0)
if sys.argv[1] == "export": #rsa export <key> if sys.argv[1] == "export": #rsa export <key>
key_file_name = sys.argv[2] if len(sys.argv) == 3:
exportKey(key_file_name) key_file_name = sys.argv[2]
exit(0) exportKey(key_file_name)
exit(0)
else:
print("Not enough arguments")
print("rsa export <keyid>")
if sys.argv[1] == "crack": #rsa crack <key> if sys.argv[1] == "crack": #rsa crack <key>
keyName = sys.argv[2] keyName = sys.argv[2]
cracked_key = crackKey2(keyName) cracked_key = crackKey2(keyName)
@ -136,10 +140,10 @@ def encrypt(message, publicKey):
msg_text = message msg_text = message
n = publicKey[N] n = publicKey[N]
e = publicKey[E] e = publicKey[E]
print("using n: {0}, e: {1}".format(n, e)) #print("using n: {0}, e: {1}".format(n, e))
msg_number_form = int.from_bytes(msg_text.encode(), byteOrder) msg_number_form = int.from_bytes(msg_text.encode(), byteOrder)
print("Word: %s or %d" % (msg_text, msg_number_form)) #print("Word: %s or %d" % (msg_text, msg_number_form))
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
@ -152,7 +156,8 @@ def decrypt(cipher, privateKey, n):
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("Cant decrypt properly") #print("decrypt: Cant decrypt properly")
return ""
return msg_decrypted return msg_decrypted
def getPrime(bits): def getPrime(bits):
@ -199,7 +204,6 @@ def sign(encrypted_msg, key):
return signed_msg.strip() return signed_msg.strip()
def verify(cipher_list): def verify(cipher_list):
sig = "Unknown"
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
encrypted_sig = cipher_list[0] encrypted_sig = cipher_list[0]
@ -211,7 +215,8 @@ 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: return sig else: continue
else: return None
def readKeyFile(keyName): def readKeyFile(keyName):
key = tuple() key = tuple()