From 7facaf1207f7888cee4979b38bd6b7b096dad28b Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Fri, 17 Apr 2020 17:31:34 +0300 Subject: [PATCH] Split words in message into a list of strings and encrypt each. This should allow unlimited length of any message. --- rsa.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/rsa.py b/rsa.py index 7f82fae..e1a0c51 100755 --- a/rsa.py +++ b/rsa.py @@ -30,18 +30,25 @@ def main(): if len(sys.argv) == 4: if sys.argv[1] == "encrypt": ##rsa encrypt msg = sys.argv[2] + msg_list = msg.split() keyName = sys.argv[3] key = readKeyFile(keyName) key_public = (key[0], key[1]) - msg_encrypted = encrypt(msg, key_public) - print("Encrypted msg: ", msg_encrypted) + msg_encrypted = "" + for word in msg_list: + msg_encrypted = msg_encrypted + " " + str(encrypt(word, key_public)) + #msg_encrypted = encrypt(msg, key_public) + print("Encrypted msg: \n", msg_encrypted) if sys.argv[1] == "decrypt": ##rsa decrypt - cipher = int(sys.argv[2]) + cipher = sys.argv[2] + cipher_list = cipher.split() + msg_decrypted = "" key = readKeyFile(sys.argv[3]) + for cipher_word in cipher_list: + msg_decrypted = msg_decrypted + " " + str(decrypt(int(cipher_word),key[2],key[0])) #with open(fileName, "r") as cipherFile: # cipher = int(cipherFile.readline()) ##one line may make problems later with padding - msg = decrypt(cipher, key[2],key[0]) - print("Decrypted message: ", msg) + print("Decrypted message: \n", msg_decrypted) @@ -79,7 +86,7 @@ def encrypt(message, publicKey): print("using n: {0}, e: {1}".format(n, e)) msg_number_form = int.from_bytes(msg_text.encode(), byteOrder) - print("Message: %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