aboutsummaryrefslogtreecommitdiff
path: root/final
diff options
context:
space:
mode:
authorHolden Rohrer <holden.rohrer@gmail.com>2019-11-11 22:53:52 -0500
committerHolden Rohrer <holden.rohrer@gmail.com>2019-11-11 22:53:52 -0500
commit303437435f4b6ec660a8b289055fa190e1028328 (patch)
treeb8beff898fca07682250b283a16396098e0c174f /final
parent1c34256f21fb04190e91f648fb7f170be61c47eb (diff)
simpler rsa
Diffstat (limited to 'final')
-rw-r--r--final/rsa-decrypt.py29
-rw-r--r--final/rsa-encrypt.py22
2 files changed, 51 insertions, 0 deletions
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)
diff --git a/final/rsa-encrypt.py b/final/rsa-encrypt.py
new file mode 100644
index 0000000..05cf562
--- /dev/null
+++ b/final/rsa-encrypt.py
@@ -0,0 +1,22 @@
+p = 29
+q = 17
+n = p*q
+tot = (p-1)*(q-1)
+e = 3 # Many present strictly for convenience's sake
+
+message = 'abcdef'
+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
+
+def encrypt_block(blk):
+ return (blk ** e) % n
+
+encrypted = [encrypt_block(block) for block in messageblocks]
+print(encrypted)