38
38
39
39
import string
40
40
41
- import numpy
41
+ import numpy as np
42
42
43
43
from maths .greatest_common_divisor import greatest_common_divisor
44
44
@@ -49,11 +49,11 @@ class HillCipher:
49
49
# i.e. a total of 36 characters
50
50
51
51
# take x and return x % len(key_string)
52
- modulus = numpy .vectorize (lambda x : x % 36 )
52
+ modulus = np .vectorize (lambda x : x % 36 )
53
53
54
- to_int = numpy .vectorize (round )
54
+ to_int = np .vectorize (round )
55
55
56
- def __init__ (self , encrypt_key : numpy .ndarray ) -> None :
56
+ def __init__ (self , encrypt_key : np .ndarray ) -> None :
57
57
"""
58
58
encrypt_key is an NxN numpy array
59
59
"""
@@ -63,7 +63,7 @@ def __init__(self, encrypt_key: numpy.ndarray) -> None:
63
63
64
64
def replace_letters (self , letter : str ) -> int :
65
65
"""
66
- >>> hill_cipher = HillCipher(numpy .array([[2, 5], [1, 6]]))
66
+ >>> hill_cipher = HillCipher(np .array([[2, 5], [1, 6]]))
67
67
>>> hill_cipher.replace_letters('T')
68
68
19
69
69
>>> hill_cipher.replace_letters('0')
@@ -73,7 +73,7 @@ def replace_letters(self, letter: str) -> int:
73
73
74
74
def replace_digits (self , num : int ) -> str :
75
75
"""
76
- >>> hill_cipher = HillCipher(numpy .array([[2, 5], [1, 6]]))
76
+ >>> hill_cipher = HillCipher(np .array([[2, 5], [1, 6]]))
77
77
>>> hill_cipher.replace_digits(19)
78
78
'T'
79
79
>>> hill_cipher.replace_digits(26)
@@ -83,10 +83,10 @@ def replace_digits(self, num: int) -> str:
83
83
84
84
def check_determinant (self ) -> None :
85
85
"""
86
- >>> hill_cipher = HillCipher(numpy .array([[2, 5], [1, 6]]))
86
+ >>> hill_cipher = HillCipher(np .array([[2, 5], [1, 6]]))
87
87
>>> hill_cipher.check_determinant()
88
88
"""
89
- det = round (numpy .linalg .det (self .encrypt_key ))
89
+ det = round (np .linalg .det (self .encrypt_key ))
90
90
91
91
if det < 0 :
92
92
det = det % len (self .key_string )
@@ -101,7 +101,7 @@ def check_determinant(self) -> None:
101
101
102
102
def process_text (self , text : str ) -> str :
103
103
"""
104
- >>> hill_cipher = HillCipher(numpy .array([[2, 5], [1, 6]]))
104
+ >>> hill_cipher = HillCipher(np .array([[2, 5], [1, 6]]))
105
105
>>> hill_cipher.process_text('Testing Hill Cipher')
106
106
'TESTINGHILLCIPHERR'
107
107
>>> hill_cipher.process_text('hello')
@@ -117,7 +117,7 @@ def process_text(self, text: str) -> str:
117
117
118
118
def encrypt (self , text : str ) -> str :
119
119
"""
120
- >>> hill_cipher = HillCipher(numpy .array([[2, 5], [1, 6]]))
120
+ >>> hill_cipher = HillCipher(np .array([[2, 5], [1, 6]]))
121
121
>>> hill_cipher.encrypt('testing hill cipher')
122
122
'WHXYJOLM9C6XT085LL'
123
123
>>> hill_cipher.encrypt('hello')
@@ -129,7 +129,7 @@ def encrypt(self, text: str) -> str:
129
129
for i in range (0 , len (text ) - self .break_key + 1 , self .break_key ):
130
130
batch = text [i : i + self .break_key ]
131
131
vec = [self .replace_letters (char ) for char in batch ]
132
- batch_vec = numpy .array ([vec ]).T
132
+ batch_vec = np .array ([vec ]).T
133
133
batch_encrypted = self .modulus (self .encrypt_key .dot (batch_vec )).T .tolist ()[
134
134
0
135
135
]
@@ -140,14 +140,14 @@ def encrypt(self, text: str) -> str:
140
140
141
141
return encrypted
142
142
143
- def make_decrypt_key (self ) -> numpy .ndarray :
143
+ def make_decrypt_key (self ) -> np .ndarray :
144
144
"""
145
- >>> hill_cipher = HillCipher(numpy .array([[2, 5], [1, 6]]))
145
+ >>> hill_cipher = HillCipher(np .array([[2, 5], [1, 6]]))
146
146
>>> hill_cipher.make_decrypt_key()
147
147
array([[ 6, 25],
148
148
[ 5, 26]])
149
149
"""
150
- det = round (numpy .linalg .det (self .encrypt_key ))
150
+ det = round (np .linalg .det (self .encrypt_key ))
151
151
152
152
if det < 0 :
153
153
det = det % len (self .key_string )
@@ -158,16 +158,14 @@ def make_decrypt_key(self) -> numpy.ndarray:
158
158
break
159
159
160
160
inv_key = (
161
- det_inv
162
- * numpy .linalg .det (self .encrypt_key )
163
- * numpy .linalg .inv (self .encrypt_key )
161
+ det_inv * np .linalg .det (self .encrypt_key ) * np .linalg .inv (self .encrypt_key )
164
162
)
165
163
166
164
return self .to_int (self .modulus (inv_key ))
167
165
168
166
def decrypt (self , text : str ) -> str :
169
167
"""
170
- >>> hill_cipher = HillCipher(numpy .array([[2, 5], [1, 6]]))
168
+ >>> hill_cipher = HillCipher(np .array([[2, 5], [1, 6]]))
171
169
>>> hill_cipher.decrypt('WHXYJOLM9C6XT085LL')
172
170
'TESTINGHILLCIPHERR'
173
171
>>> hill_cipher.decrypt('85FF00')
@@ -180,7 +178,7 @@ def decrypt(self, text: str) -> str:
180
178
for i in range (0 , len (text ) - self .break_key + 1 , self .break_key ):
181
179
batch = text [i : i + self .break_key ]
182
180
vec = [self .replace_letters (char ) for char in batch ]
183
- batch_vec = numpy .array ([vec ]).T
181
+ batch_vec = np .array ([vec ]).T
184
182
batch_decrypted = self .modulus (decrypt_key .dot (batch_vec )).T .tolist ()[0 ]
185
183
decrypted_batch = "" .join (
186
184
self .replace_digits (num ) for num in batch_decrypted
@@ -199,7 +197,7 @@ def main() -> None:
199
197
row = [int (x ) for x in input ().split ()]
200
198
hill_matrix .append (row )
201
199
202
- hc = HillCipher (numpy .array (hill_matrix ))
200
+ hc = HillCipher (np .array (hill_matrix ))
203
201
204
202
print ("Would you like to encrypt or decrypt some text? (1 or 2)" )
205
203
option = input ("\n 1. Encrypt\n 2. Decrypt\n " )
0 commit comments