25
25
Note:
26
26
This implementation only considers alphanumerics in the text. If the length of
27
27
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
29
29
length of the text reaches a multiple of the break_key. So the text after
30
30
decrypting might be a little different than the original text.
31
31
@@ -66,7 +66,7 @@ class HillCipher:
66
66
67
67
def __init__ (self , encrypt_key ):
68
68
"""
69
- encrypt_key is an NxN numpy matrix
69
+ encrypt_key is an NxN numpy array
70
70
"""
71
71
self .encrypt_key = self .modulus (encrypt_key ) # mod36 calc's on the encrypt key
72
72
self .check_determinant () # validate the determinant of the encryption key
@@ -75,7 +75,7 @@ def __init__(self, encrypt_key):
75
75
76
76
def replace_letters (self , letter : str ) -> int :
77
77
"""
78
- >>> hill_cipher = HillCipher(numpy.matrix ([[2, 5], [1, 6]]))
78
+ >>> hill_cipher = HillCipher(numpy.array ([[2, 5], [1, 6]]))
79
79
>>> hill_cipher.replace_letters('T')
80
80
19
81
81
>>> hill_cipher.replace_letters('0')
@@ -85,7 +85,7 @@ def replace_letters(self, letter: str) -> int:
85
85
86
86
def replace_digits (self , num : int ) -> str :
87
87
"""
88
- >>> hill_cipher = HillCipher(numpy.matrix ([[2, 5], [1, 6]]))
88
+ >>> hill_cipher = HillCipher(numpy.array ([[2, 5], [1, 6]]))
89
89
>>> hill_cipher.replace_digits(19)
90
90
'T'
91
91
>>> hill_cipher.replace_digits(26)
@@ -95,7 +95,7 @@ def replace_digits(self, num: int) -> str:
95
95
96
96
def check_determinant (self ) -> None :
97
97
"""
98
- >>> hill_cipher = HillCipher(numpy.matrix ([[2, 5], [1, 6]]))
98
+ >>> hill_cipher = HillCipher(numpy.array ([[2, 5], [1, 6]]))
99
99
>>> hill_cipher.check_determinant()
100
100
"""
101
101
det = round (numpy .linalg .det (self .encrypt_key ))
@@ -111,9 +111,11 @@ def check_determinant(self) -> None:
111
111
112
112
def process_text (self , text : str ) -> str :
113
113
"""
114
- >>> hill_cipher = HillCipher(numpy.matrix ([[2, 5], [1, 6]]))
114
+ >>> hill_cipher = HillCipher(numpy.array ([[2, 5], [1, 6]]))
115
115
>>> hill_cipher.process_text('Testing Hill Cipher')
116
116
'TESTINGHILLCIPHERR'
117
+ >>> hill_cipher.process_text('hello')
118
+ 'HELLOO'
117
119
"""
118
120
chars = [char for char in text .upper () if char in self .key_string ]
119
121
@@ -125,17 +127,19 @@ def process_text(self, text: str) -> str:
125
127
126
128
def encrypt (self , text : str ) -> str :
127
129
"""
128
- >>> hill_cipher = HillCipher(numpy.matrix ([[2, 5], [1, 6]]))
130
+ >>> hill_cipher = HillCipher(numpy.array ([[2, 5], [1, 6]]))
129
131
>>> hill_cipher.encrypt('testing hill cipher')
130
132
'WHXYJOLM9C6XT085LL'
133
+ >>> hill_cipher.encrypt('hello')
134
+ '85FF00'
131
135
"""
132
136
text = self .process_text (text .upper ())
133
137
encrypted = ""
134
138
135
139
for i in range (0 , len (text ) - self .break_key + 1 , self .break_key ):
136
140
batch = text [i : i + self .break_key ]
137
141
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
139
143
batch_encrypted = self .modulus (self .encrypt_key .dot (batch_vec )).T .tolist ()[
140
144
0
141
145
]
@@ -148,10 +152,10 @@ def encrypt(self, text: str) -> str:
148
152
149
153
def make_decrypt_key (self ):
150
154
"""
151
- >>> hill_cipher = HillCipher(numpy.matrix ([[2, 5], [1, 6]]))
155
+ >>> hill_cipher = HillCipher(numpy.array ([[2, 5], [1, 6]]))
152
156
>>> hill_cipher.make_decrypt_key()
153
- matrix ([[ 6., 25.],
154
- [ 5., 26.]])
157
+ array ([[ 6., 25.],
158
+ [ 5., 26.]])
155
159
"""
156
160
det = round (numpy .linalg .det (self .encrypt_key ))
157
161
@@ -173,9 +177,11 @@ def make_decrypt_key(self):
173
177
174
178
def decrypt (self , text : str ) -> str :
175
179
"""
176
- >>> hill_cipher = HillCipher(numpy.matrix ([[2, 5], [1, 6]]))
180
+ >>> hill_cipher = HillCipher(numpy.array ([[2, 5], [1, 6]]))
177
181
>>> hill_cipher.decrypt('WHXYJOLM9C6XT085LL')
178
182
'TESTINGHILLCIPHERR'
183
+ >>> hill_cipher.decrypt('85FF00')
184
+ 'HELLOO'
179
185
"""
180
186
self .decrypt_key = self .make_decrypt_key ()
181
187
text = self .process_text (text .upper ())
@@ -184,7 +190,7 @@ def decrypt(self, text: str) -> str:
184
190
for i in range (0 , len (text ) - self .break_key + 1 , self .break_key ):
185
191
batch = text [i : i + self .break_key ]
186
192
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
188
194
batch_decrypted = self .modulus (self .decrypt_key .dot (batch_vec )).T .tolist ()[
189
195
0
190
196
]
@@ -205,7 +211,7 @@ def main():
205
211
row = [int (x ) for x in input ().split ()]
206
212
hill_matrix .append (row )
207
213
208
- hc = HillCipher (numpy .matrix (hill_matrix ))
214
+ hc = HillCipher (numpy .array (hill_matrix ))
209
215
210
216
print ("Would you like to encrypt or decrypt some text? (1 or 2)" )
211
217
option = input ("\n 1. Encrypt\n 2. Decrypt\n " )
0 commit comments