From 18cf5b3059b4c0ce63dc6de7198e23cbdea3dc2a Mon Sep 17 00:00:00 2001 From: Hesham Date: Fri, 10 Jul 2020 07:25:20 +0300 Subject: [PATCH 1/7] Add 'README.md' --- README.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..03e37d9 --- /dev/null +++ b/README.md @@ -0,0 +1,21 @@ + +# hesham-rsa (hrsa) +A simple program written in python to implement RSA public encryption. Used as Course EE-305 project. +# Installation +There is no packaging or installation. Works in current dir (portable) for now. +# Usage + + ./hrsa + or + + python hrsa + +## generating keys +to generate a key pair with and ID + + ./hrsa [keysize] [keyID] +All generated keys are {working dir}/saves/ in keys folder +## encrypting + + ./hrsa encrypt "[message]" [RecieverKeyID] [SenderSignetureKeyID] + From 45584d378ed435805cb78b9b6fba8a8cf2533004 Mon Sep 17 00:00:00 2001 From: Hesham Date: Fri, 10 Jul 2020 07:27:37 +0300 Subject: [PATCH 2/7] Update 'README.md' --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 03e37d9..79e7fe5 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ There is no packaging or installation. Works in current dir (portable) for now. python hrsa ## generating keys -to generate a key pair with and ID +To generate a key pair with an ID ./hrsa [keysize] [keyID] All generated keys are {working dir}/saves/ in keys folder From c121314eb9d791ac6f0f44b418b2550085a7e8e3 Mon Sep 17 00:00:00 2001 From: Hesham Date: Fri, 28 Aug 2020 02:47:01 +0300 Subject: [PATCH 3/7] Update 'README.md' --- README.md | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 79e7fe5..025923e 100644 --- a/README.md +++ b/README.md @@ -1,21 +1,23 @@ -# hesham-rsa (hrsa) -A simple program written in python to implement RSA public encryption. Used as Course EE-305 project. +# hesham-rsa +A simple program written in python to implement RSA public encryption. Used as Course EE-305 project KAU. # Installation There is no packaging or installation. Works in current dir (portable) for now. + + git clone https://apollo-server.ddns.net/gitea/Hesham/hesham-rsa.git + # Usage - ./hrsa + ./rsa.py or - python hrsa + python rsa.py ## generating keys -To generate a key pair with an ID +to generate a key pair with and ID - ./hrsa [keysize] [keyID] + ./rsa.py [keysize] [keyID] All generated keys are {working dir}/saves/ in keys folder ## encrypting - ./hrsa encrypt "[message]" [RecieverKeyID] [SenderSignetureKeyID] - + ./rsa.py encrypt "[message]" [RecieverKeyID] [SenderSignetureKeyID] From 7ae3e4dc04b2cdf531a9ce971bd87213028cb01f Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Fri, 28 Aug 2020 17:22:35 +0300 Subject: [PATCH 4/7] Improved and accurate signature output Signed-off-by: HeshamTB --- rsa.py | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) mode change 100755 => 100644 rsa.py diff --git a/rsa.py b/rsa.py old mode 100755 new mode 100644 index 78ecb14..847db13 --- a/rsa.py +++ b/rsa.py @@ -75,22 +75,26 @@ def main(): for cipher_word in cipher_list: msg_decrypted = msg_decrypted + " " + str(decrypt(int(cipher_word, 16),key[D],key[N])) if sig == None: - print("Unknown signature!") + print("\033[91mUnknown signature! \u2717" + "\033[0m") else: - print("Signed by: ", sig) + print("Signed by: \033[92m " + sig + " \u2713\033[0m") print("Decrypted message: \n", msg_decrypted) else: print("Not enough arguments") - print("rsa decrypt \"\" ") + print("rsa decrypt \"\" ") exit(0) if sys.argv[1] == "list": listKeys() exit(0) if sys.argv[1] == "export": #rsa export - key_file_name = sys.argv[2] - exportKey(key_file_name) - exit(0) + if len(sys.argv) == 3: + key_file_name = sys.argv[2] + exportKey(key_file_name) + 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) @@ -136,10 +140,10 @@ def encrypt(message, publicKey): msg_text = message n = publicKey[N] 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) - 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 return msg_encrypted_number_form @@ -152,7 +156,8 @@ def decrypt(cipher, privateKey, n): try: msg_decrypted = str(msg_decrypted.to_bytes(msg_decrypted.bit_length(), byteOrder).decode()).strip() except UnicodeDecodeError: - print("Cant decrypt properly") + #print("decrypt: Cant decrypt properly") + return "" return msg_decrypted def getPrime(bits): @@ -199,7 +204,6 @@ def sign(encrypted_msg, key): return signed_msg.strip() def verify(cipher_list): - sig = "Unknown" local_keys = os.listdir(keysFolder) cipher_list.reverse() #To get last word using index 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])) if "sig:" in sig: return sig.replace("sig:","") - else: return sig + else: continue + else: return None def readKeyFile(keyName): key = tuple() From c059a7dccdbf7ede4e30e229c8cc8362c191e0f6 Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Fri, 28 Aug 2020 17:52:18 +0300 Subject: [PATCH 5/7] version 1.2.1 Signed-off-by: HeshamTB --- rsa.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rsa.py b/rsa.py index 847db13..6169680 100644 --- a/rsa.py +++ b/rsa.py @@ -15,6 +15,7 @@ import os import sys import MillerRabin as mr +VERSION="1.2.1" keysFolder = "keys/" byteOrder = "little" N=0 @@ -26,7 +27,7 @@ PHI=5 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] From b7597320fd80e41ae51d495e7ed4ccb66dbef8df Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Fri, 28 Aug 2020 18:53:22 +0300 Subject: [PATCH 6/7] Ignore pyinstaller files --- .gitignore | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.gitignore b/.gitignore index 542eed8..a55b6b0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,8 @@ __pycache__/ keys/ test-file + +#pyinstaller files +dist/ +build/ +*.spec From d51a42589c064da6829de01a66c2923a0a92403e Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Fri, 28 Aug 2020 18:53:55 +0300 Subject: [PATCH 7/7] Use sys.exit() instead of exit(): exit() cuases stack trace output from pyinstaller exe. It is also recommended to use sys.exit() in prodcution. Signed-off-by: HeshamTB --- rsa.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/rsa.py b/rsa.py index 6169680..39d75e7 100644 --- a/rsa.py +++ b/rsa.py @@ -40,11 +40,11 @@ def main(): saveKeyFile(key, keyFileName) except IOError: print("could not write file") - exit(1) + sys.exit(1) except Exception as ex: print(ex) - exit(1) - exit(0) + sys.exit(1) + sys.exit(0) if sys.argv[1] == "encrypt": ##rsa encrypt if len(sys.argv) == 5: msg = sys.argv[2] @@ -64,7 +64,7 @@ def main(): print("Not enough arguments") print("rsa encrypt ") #Make help function - exit(0) + sys.exit(0) if sys.argv[1] == "decrypt": ##rsa decrypt "" if len(sys.argv) == 4: cipher = sys.argv[2] @@ -83,16 +83,16 @@ def main(): else: print("Not enough arguments") print("rsa decrypt \"\" ") - exit(0) + sys.exit(0) if sys.argv[1] == "list": listKeys() - exit(0) + 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) - exit(0) + sys.exit(0) else: print("Not enough arguments") print("rsa export ") @@ -107,7 +107,7 @@ def main(): #No command exit code printHelp() - exit(127) + sys.exit(127)