From 303437435f4b6ec660a8b289055fa190e1028328 Mon Sep 17 00:00:00 2001 From: Holden Rohrer Date: Mon, 11 Nov 2019 22:53:52 -0500 Subject: simpler rsa --- final/rsa-decrypt.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 final/rsa-decrypt.py (limited to 'final/rsa-decrypt.py') diff --git a/final/rsa-decrypt.py b/final/rsa-decrypt.py new file mode 100644 index 0000000..335ed68 --- /dev/null +++ b/final/rsa-decrypt.py @@ -0,0 +1,29 @@ +encrypted = [405, 35, 133, 371, 435, 279] # Directly from demo output of encrypted +p = 29 +q = 17 +n = p*q +tot = (p-1)*(q-1) +e = 3 + +# Calculate d such that ed == 1 mod tot +d = 1 # Guess +while (e*d % tot != 1): + d += 1 +# Algorithm is currently extremely dumb + +def decrypt_block(blk): + return blk**d % n + +decrypted = [decrypt_block(block) for block in encrypted] + +messagenum = 0 +for block in decrypted: + messagenum += block + messagenum *= n + +message = '' +while messagenum > 0: + message += chr(messagenum%128) + messagenum = (messagenum-messagenum%128)//128 + +print(message) -- cgit From 0ef3d4cde99a8288339338d3ed9285a71c404236 Mon Sep 17 00:00:00 2001 From: Holden Rohrer Date: Mon, 11 Nov 2019 23:03:23 -0500 Subject: fixed endianness bugs --- final/rsa-decrypt.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'final/rsa-decrypt.py') diff --git a/final/rsa-decrypt.py b/final/rsa-decrypt.py index 335ed68..d05b351 100644 --- a/final/rsa-decrypt.py +++ b/final/rsa-decrypt.py @@ -17,13 +17,13 @@ def decrypt_block(blk): decrypted = [decrypt_block(block) for block in encrypted] messagenum = 0 -for block in decrypted: - messagenum += block - messagenum *= n +for blockind in range(len(decrypted)): + block = decrypted[blockind] + messagenum += block * n ** blockind message = '' while messagenum > 0: - message += chr(messagenum%128) + message = chr(messagenum%128) + message messagenum = (messagenum-messagenum%128)//128 print(message) -- cgit From eb364b5badef4afb4cc1ce370a36fd089aa61cf7 Mon Sep 17 00:00:00 2001 From: Holden Rohrer Date: Mon, 11 Nov 2019 23:18:36 -0500 Subject: added user entry (except for the hardcoded encrypted data) --- final/rsa-decrypt.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'final/rsa-decrypt.py') diff --git a/final/rsa-decrypt.py b/final/rsa-decrypt.py index d05b351..39a2720 100644 --- a/final/rsa-decrypt.py +++ b/final/rsa-decrypt.py @@ -1,9 +1,9 @@ -encrypted = [405, 35, 133, 371, 435, 279] # Directly from demo output of encrypted -p = 29 -q = 17 +encrypted = [340, 316, 145, 278, 250, 435, 321, 109, 115, 490, 156, 212, 122, 288, 287, 164, 225] # Directly from demo output of encrypted +p = int(input('prime 1: ')) +q = int(input('prime 2: ')) n = p*q tot = (p-1)*(q-1) -e = 3 +e = int(input('e: ')) # Calculate d such that ed == 1 mod tot d = 1 # Guess -- cgit