From 8eb41720f151d20f13d7316d30cd8a02c8664e42 Mon Sep 17 00:00:00 2001 From: Holden Rohrer Date: Tue, 12 Nov 2019 10:28:35 -0500 Subject: allowed user input and base64 intermediary for rsa encrypt/decrypt --- final/rsa-decrypt.py | 15 ++++----------- final/rsa-encrypt.py | 19 +++++-------------- 2 files changed, 9 insertions(+), 25 deletions(-) diff --git a/final/rsa-decrypt.py b/final/rsa-decrypt.py index 39a2720..3bc0a06 100644 --- a/final/rsa-decrypt.py +++ b/final/rsa-decrypt.py @@ -1,7 +1,8 @@ -encrypted = [340, 316, 145, 278, 250, 435, 321, 109, 115, 490, 156, 212, 122, 288, 287, 164, 225] # Directly from demo output of encrypted +import rsautils as ru p = int(input('prime 1: ')) q = int(input('prime 2: ')) n = p*q +encrypted = ru.int2list(ru.base642int(input('encrypted message:\n')),n) tot = (p-1)*(q-1) e = int(input('e: ')) @@ -15,15 +16,7 @@ def decrypt_block(blk): return blk**d % n decrypted = [decrypt_block(block) for block in encrypted] - -messagenum = 0 -for blockind in range(len(decrypted)): - block = decrypted[blockind] - messagenum += block * n ** blockind - -message = '' -while messagenum > 0: - message = chr(messagenum%128) + message - messagenum = (messagenum-messagenum%128)//128 +messagenum = ru.list2int(decrypted,n,endian="big") +message = ru.int2string(messagenum) print(message) diff --git a/final/rsa-encrypt.py b/final/rsa-encrypt.py index a15fe9d..aa52509 100644 --- a/final/rsa-encrypt.py +++ b/final/rsa-encrypt.py @@ -1,24 +1,15 @@ -'''p = 29 -q = 17 -n = p*q -tot = (p-1)*(q-1) -e = 3 # Many present strictly for convenience's sake''' +import rsautils as ru + n = int(input('n: ')) e = int(input('e (make sure it\'s relatively prime to p-1 and q-1): ')) message = input('message:\n') messagenum = 0 -for char in message: - messagenum += ord(char) - messagenum *= 128 # Assuming ASCII only characters - -messageblocks = [] -while messagenum > 0: - messageblocks.append(messagenum%n) - messagenum = (messagenum-messagenum%n)//n +messagenum = ru.string2int(message) +messageblocks = ru.int2list(messagenum,n,endian="big") def encrypt_block(blk): return (blk ** e) % n encrypted = [encrypt_block(block) for block in messageblocks] -print(encrypted) +print(ru.int2base64(ru.list2int(encrypted,n))) -- cgit