Skip to content

Commit 36c01f9

Browse files
authored
Change matrix to array
Add more tests
1 parent 696a3d0 commit 36c01f9

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

ciphers/hill_cipher.py

+20-14
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
Note:
2626
This implementation only considers alphanumerics in the text. If the length of
2727
the text to be encrypted is not a multiple of the break key(the length of one
28-
batch of letters),the last character of the text is added to the text until the
28+
batch of letters), the last character of the text is added to the text until the
2929
length of the text reaches a multiple of the break_key. So the text after
3030
decrypting might be a little different than the original text.
3131
@@ -66,7 +66,7 @@ class HillCipher:
6666

6767
def __init__(self, encrypt_key):
6868
"""
69-
encrypt_key is an NxN numpy matrix
69+
encrypt_key is an NxN numpy array
7070
"""
7171
self.encrypt_key = self.modulus(encrypt_key) # mod36 calc's on the encrypt key
7272
self.check_determinant() # validate the determinant of the encryption key
@@ -75,7 +75,7 @@ def __init__(self, encrypt_key):
7575

7676
def replace_letters(self, letter: str) -> int:
7777
"""
78-
>>> hill_cipher = HillCipher(numpy.matrix([[2, 5], [1, 6]]))
78+
>>> hill_cipher = HillCipher(numpy.array([[2, 5], [1, 6]]))
7979
>>> hill_cipher.replace_letters('T')
8080
19
8181
>>> hill_cipher.replace_letters('0')
@@ -85,7 +85,7 @@ def replace_letters(self, letter: str) -> int:
8585

8686
def replace_digits(self, num: int) -> str:
8787
"""
88-
>>> hill_cipher = HillCipher(numpy.matrix([[2, 5], [1, 6]]))
88+
>>> hill_cipher = HillCipher(numpy.array([[2, 5], [1, 6]]))
8989
>>> hill_cipher.replace_digits(19)
9090
'T'
9191
>>> hill_cipher.replace_digits(26)
@@ -95,7 +95,7 @@ def replace_digits(self, num: int) -> str:
9595

9696
def check_determinant(self) -> None:
9797
"""
98-
>>> hill_cipher = HillCipher(numpy.matrix([[2, 5], [1, 6]]))
98+
>>> hill_cipher = HillCipher(numpy.array([[2, 5], [1, 6]]))
9999
>>> hill_cipher.check_determinant()
100100
"""
101101
det = round(numpy.linalg.det(self.encrypt_key))
@@ -111,9 +111,11 @@ def check_determinant(self) -> None:
111111

112112
def process_text(self, text: str) -> str:
113113
"""
114-
>>> hill_cipher = HillCipher(numpy.matrix([[2, 5], [1, 6]]))
114+
>>> hill_cipher = HillCipher(numpy.array([[2, 5], [1, 6]]))
115115
>>> hill_cipher.process_text('Testing Hill Cipher')
116116
'TESTINGHILLCIPHERR'
117+
>>> hill_cipher.process_text('hello')
118+
'HELLOO'
117119
"""
118120
chars = [char for char in text.upper() if char in self.key_string]
119121

@@ -125,17 +127,19 @@ def process_text(self, text: str) -> str:
125127

126128
def encrypt(self, text: str) -> str:
127129
"""
128-
>>> hill_cipher = HillCipher(numpy.matrix([[2, 5], [1, 6]]))
130+
>>> hill_cipher = HillCipher(numpy.array([[2, 5], [1, 6]]))
129131
>>> hill_cipher.encrypt('testing hill cipher')
130132
'WHXYJOLM9C6XT085LL'
133+
>>> hill_cipher.encrypt('hello')
134+
'85FF00'
131135
"""
132136
text = self.process_text(text.upper())
133137
encrypted = ""
134138

135139
for i in range(0, len(text) - self.break_key + 1, self.break_key):
136140
batch = text[i : i + self.break_key]
137141
batch_vec = [self.replace_letters(char) for char in batch]
138-
batch_vec = numpy.matrix([batch_vec]).T
142+
batch_vec = numpy.array([batch_vec]).T
139143
batch_encrypted = self.modulus(self.encrypt_key.dot(batch_vec)).T.tolist()[
140144
0
141145
]
@@ -148,10 +152,10 @@ def encrypt(self, text: str) -> str:
148152

149153
def make_decrypt_key(self):
150154
"""
151-
>>> hill_cipher = HillCipher(numpy.matrix([[2, 5], [1, 6]]))
155+
>>> hill_cipher = HillCipher(numpy.array([[2, 5], [1, 6]]))
152156
>>> hill_cipher.make_decrypt_key()
153-
matrix([[ 6., 25.],
154-
[ 5., 26.]])
157+
array([[ 6., 25.],
158+
[ 5., 26.]])
155159
"""
156160
det = round(numpy.linalg.det(self.encrypt_key))
157161

@@ -173,9 +177,11 @@ def make_decrypt_key(self):
173177

174178
def decrypt(self, text: str) -> str:
175179
"""
176-
>>> hill_cipher = HillCipher(numpy.matrix([[2, 5], [1, 6]]))
180+
>>> hill_cipher = HillCipher(numpy.array([[2, 5], [1, 6]]))
177181
>>> hill_cipher.decrypt('WHXYJOLM9C6XT085LL')
178182
'TESTINGHILLCIPHERR'
183+
>>> hill_cipher.decrypt('85FF00')
184+
'HELLOO'
179185
"""
180186
self.decrypt_key = self.make_decrypt_key()
181187
text = self.process_text(text.upper())
@@ -184,7 +190,7 @@ def decrypt(self, text: str) -> str:
184190
for i in range(0, len(text) - self.break_key + 1, self.break_key):
185191
batch = text[i : i + self.break_key]
186192
batch_vec = [self.replace_letters(char) for char in batch]
187-
batch_vec = numpy.matrix([batch_vec]).T
193+
batch_vec = numpy.array([batch_vec]).T
188194
batch_decrypted = self.modulus(self.decrypt_key.dot(batch_vec)).T.tolist()[
189195
0
190196
]
@@ -205,7 +211,7 @@ def main():
205211
row = [int(x) for x in input().split()]
206212
hill_matrix.append(row)
207213

208-
hc = HillCipher(numpy.matrix(hill_matrix))
214+
hc = HillCipher(numpy.array(hill_matrix))
209215

210216
print("Would you like to encrypt or decrypt some text? (1 or 2)")
211217
option = input("\n1. Encrypt\n2. Decrypt\n")

0 commit comments

Comments
 (0)