aboutsummaryrefslogtreecommitdiff
path: root/final/rsa-decrypt.py
blob: 39a2720f11787cf0510d70ba6da0d4b2455df265 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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 = int(input('e: '))

# 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 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

print(message)