-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecrypter.py
More file actions
26 lines (22 loc) · 898 Bytes
/
decrypter.py
File metadata and controls
26 lines (22 loc) · 898 Bytes
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
# Made for school task
# Inverse of d must be calculated by hand
def decrypt(c_blocks, e, p, q, d):
n = p * q
a_list = list(str(bin(d)))
a_list = [int(a) for a in a_list[2:]]
m_blocks = []
print(f'Decrypting {" ".join([str(c) for c in c_blocks])} using key ({p}*{q}, {e}), where {d} is the inverse of {e} mod {p-1}*{q-1}')
for c in c_blocks:
print(f'Block being decrypted: {c}')
x = 1
power = c % n
for i, a in enumerate(a_list[::-1]):
if a:
x = (x * power) % n
power = (power * power) % n
print(f'a_{i} = {str(a)} => x = {x} and power = {power}')
print(f'Decrypted block is {format(x, "04d")}\n')
m_blocks.append(format(x, '04d'))
print(f'Decrypted numbers is {" ".join(m_blocks)}')
if __name__ == '__main__':
decrypt([3185, 2038, 2460, 2550], 17, 53, 61, 2753)