-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbruh.py
38 lines (30 loc) · 1.14 KB
/
bruh.py
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
30
31
32
33
34
35
36
37
38
import numpy as np
# Function to convert string to numbers
def str_to_num(s):
return [ord(c) - ord('A') for c in s]
# Function to convert numbers to string
def num_to_str(nums):
return ''.join(chr(n + ord('A')) for n in nums)
# Function to calculate modular inverse
def mod_inverse(n, m=26):
for i in range(1, m):
if (n * i) % m == 1:
return i
return None
# Function to calculate inverse of matrix
def matrix_inverse(matrix, mod=26):
det = int(np.round(np.linalg.det(matrix)))
det_inv = mod_inverse(det, mod)
if det_inv is None:
raise ValueError('Matrix is not invertible')
inv = np.linalg.inv(matrix) * det * det_inv
return np.round(inv).astype(int) % mod
# Convert key and ciphertext to numbers
key_matrix = np.array(str_to_num('NOOB')).reshape(2, 2)
ciphertext_blocks = np.array(str_to_num('UPGTMQGTVMEGMR')).reshape(-1, 2)
# Calculate inverse of key matrix
key_matrix_inv = matrix_inverse(key_matrix)
# Decrypt the ciphertext
plaintext_blocks = (np.dot(ciphertext_blocks, key_matrix_inv) % 26).astype(int)
plaintext = num_to_str(plaintext_blocks.flatten())
print('Decrypted message:', plaintext)