From b4e61d0a77205ac82fc684d2c0cd6ef6ae6851d3 Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Sun, 6 Sep 2020 07:26:49 +0300 Subject: [PATCH 1/2] Updated interface --- rsa.py | 157 +++++++++++++++++++++++++++++---------------------------- 1 file changed, 81 insertions(+), 76 deletions(-) mode change 100644 => 100755 rsa.py diff --git a/rsa.py b/rsa.py old mode 100644 new mode 100755 index 39d75e7..657cca5 --- a/rsa.py +++ b/rsa.py @@ -28,82 +28,87 @@ ID=6 def main(): print("hesham-rsa version ", VERSION) - if len(sys.argv) > 1: - if sys.argv[1] == "gen": ##rsa gen - keyFileName = sys.argv[3] - key = generateKeys(keyFileName, int(sys.argv[2])) - print("e: ", key[E]) - print("n: ", key[N]) - print("d: ", key[D]) - printKey(key) - try: - saveKeyFile(key, keyFileName) - except IOError: - print("could not write file") - sys.exit(1) - except Exception as ex: - print(ex) - sys.exit(1) - sys.exit(0) - if sys.argv[1] == "encrypt": ##rsa encrypt - if len(sys.argv) == 5: - msg = sys.argv[2] - msg_list = msg.split() - keyName = sys.argv[3] - signing_key_name = sys.argv[4] - key = readKeyFile(keyName) - signing_key = readKeyFile(signing_key_name) - key_public = (key[N], key[E]) - msg_encrypted = "" - for word in msg_list: - msg_encrypted = msg_encrypted + " " + hex(encrypt(word, key_public)) - #msg_encrypted = encrypt(msg, key_public) - print("Encrypted msg: \n", msg_encrypted) - print("Signed: \n", sign(msg_encrypted, signing_key)) ## Adds an encrypted sig at the end of message. - else: - print("Not enough arguments") - print("rsa encrypt ") - #Make help function - sys.exit(0) - if sys.argv[1] == "decrypt": ##rsa decrypt "" - if len(sys.argv) == 4: - cipher = sys.argv[2] - cipher_list = cipher.split() - sig = verify(cipher_list) - del cipher_list[-1] - msg_decrypted = "" - key = readKeyFile(sys.argv[3]) - for cipher_word in cipher_list: - msg_decrypted = msg_decrypted + " " + str(decrypt(int(cipher_word, 16),key[D],key[N])) - if sig == None: - print("\033[91mUnknown signature! \u2717" + "\033[0m") - else: - print("Signed by: \033[92m " + sig + " \u2713\033[0m") - print("Decrypted message: \n", msg_decrypted) - else: - print("Not enough arguments") - print("rsa decrypt \"\" ") - sys.exit(0) - - if sys.argv[1] == "list": - listKeys() - sys.exit(0) - if sys.argv[1] == "export": #rsa export - if len(sys.argv) == 3: - key_file_name = sys.argv[2] - exportKey(key_file_name) - sys.exit(0) - else: - print("Not enough arguments") - print("rsa export ") - if sys.argv[1] == "crack": #rsa crack - keyName = sys.argv[2] - cracked_key = crackKey2(keyName) - printKey(cracked_key) - if sys.argv[1] == "print": #rsa print - printKey(readKeyFile(sys.argv[2])) - if sys.argv[1] == "help": - printHelp() + if sys.argv[1] == "gen": ##rsa gen + keyFileName = sys.argv[3] + key = generateKeys(keyFileName, int(sys.argv[2])) + print("e: ", key[E]) + print("n: ", key[N]) + print("d: ", key[D]) + printKey(key) + try: + saveKeyFile(key, keyFileName) + except IOError: + print("could not write file") + sys.exit(1) + except Exception as ex: + print(ex) + sys.exit(1) + sys.exit(0) + if sys.argv[1] == "encrypt" and len(sys.argv) == 5: ##rsa encrypt + msg = sys.argv[2] + msg_list = msg.split() + keyName = sys.argv[3] + signing_key_name = sys.argv[4] + key = readKeyFile(keyName) + signing_key = readKeyFile(signing_key_name) + key_public = (key[N], key[E]) + msg_encrypted = "" + for word in msg_list: + msg_encrypted = msg_encrypted + " " + hex(encrypt(word, key_public)) + #msg_encrypted = encrypt(msg, key_public) + print("Encrypted msg: \n", msg_encrypted) + print("Signed: \n", sign(msg_encrypted, signing_key)) ## Adds an encrypted sig at the end of message. + sys.exit(0) + elif sys.argv[1] == "encrypt": + print("Not enough arguments") + print("rsa encrypt ") + sys.exit(1) + if sys.argv[1] == "decrypt" and len(sys.argv) == 4: ##rsa decrypt "" + cipher = sys.argv[2] + cipher_list = cipher.split() + sig = verify(cipher_list) + del cipher_list[-1] + msg_decrypted = "" + key = readKeyFile(sys.argv[3]) + for cipher_word in cipher_list: + msg_decrypted = msg_decrypted + " " + str(decrypt(int(cipher_word, 16),key[D],key[N])) + if sig == None: + print("\033[91mUnknown signature! \u2717" + "\033[0m") + else: + print("Signed by: \033[92m " + sig + " \u2713\033[0m") + print("Decrypted message: \n", msg_decrypted) + sys.exit(0) + elif sys.argv[1] == "decrypt": + print("Not enough arguments") + print("rsa decrypt \"\" ") + sys.exit(1) + if sys.argv[1] == "list": + listKeys() + sys.exit(0) + if sys.argv[1] == "export" and len(sys.argv) == 3: #rsa export + key_file_name = sys.argv[2] + exportKey(key_file_name) + sys.exit(0) + elif sys.argv[1] == "export": + printHelp() + sys.exit(1) + if sys.argv[1] == "crack" and len(sys.argv) == 3: #rsa crack + keyName = sys.argv[2] + cracked_key = crackKey2(keyName) + printKey(cracked_key) + sys.exit(0) + elif sys.argv[1] == "crack": + printHelp() + sys.exit(1) + if sys.argv[1] == "print" and len(sys.argv) == 3: #rsa print + printKey(readKeyFile(sys.argv[2])) + sys.exit(0) + elif sys.argv[1] == "print": + printHelp() + sys.exit(1) + if sys.argv[1] == "help": + printHelp() + sys.exit(0) #No command exit code printHelp() From 261ce0fdb400dbec688f57b1246cc8eeb39bdd24 Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Sun, 6 Sep 2020 07:27:47 +0300 Subject: [PATCH 2/2] version 1.2.2 --- rsa.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rsa.py b/rsa.py index 657cca5..ac46289 100755 --- a/rsa.py +++ b/rsa.py @@ -15,7 +15,7 @@ import os import sys import MillerRabin as mr -VERSION="1.2.1" +VERSION="1.2.2" keysFolder = "keys/" byteOrder = "little" N=0