Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
algorithms-in-python/ciphers/hill_cipher.py at Python-3.9rc2 · coderjack/algorithms-in-python · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
coderjack
/
algorithms-in-python
Public
forked from
TheAlgorithms/Python
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Files
Expand file tree
Python-3.9rc2
Breadcrumbs
algorithms-in-python
/
ciphers
/
hill_cipher.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
234 lines (191 loc) · 7.42 KB
Python-3.9rc2
Breadcrumbs
algorithms-in-python
/
ciphers
/
hill_cipher.py
Copy path
Top
File metadata and controls
Code
Blame
234 lines (191 loc) · 7.42 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
"""
Hill Cipher:
The 'HillCipher' class below implements the Hill Cipher algorithm which uses
modern linear algebra techniques to encode and decode text using an encryption
key matrix.
Algorithm:
Let the order of the encryption key be N (as it is a square matrix).
Your text is divided into batches of length N and converted to numerical vectors
by a simple mapping starting with A=0 and so on.
The key is then multiplied with the newly created batch vector to obtain the
encoded vector. After each multiplication modular 36 calculations are performed
on the vectors so as to bring the numbers between 0 and 36 and then mapped with
their corresponding alphanumerics.
While decrypting, the decrypting key is found which is the inverse of the
encrypting key modular 36. The same process is repeated for decrypting to get
the original message back.
Constraints:
The determinant of the encryption key matrix must be relatively prime w.r.t 36.
Note:
This implementation only considers alphanumerics in the text. If the length of
the text to be encrypted is not a multiple of the break key(the length of one
batch of letters), the last character of the text is added to the text until the
length of the text reaches a multiple of the break_key. So the text after
decrypting might be a little different than the original text.
References:
https://apprendre-en-ligne.net/crypto/hill/Hillciph.pdf
https://www.youtube.com/watch?v=kfmNeskzs2o
https://www.youtube.com/watch?v=4RhLNDqcjpA
"""
import
string
import
numpy
def
greatest_common_divisor
(
a
:
int
,
b
:
int
)
->
int
:
"""
>>> greatest_common_divisor(4, 8)
4
>>> greatest_common_divisor(8, 4)
4
>>> greatest_common_divisor(4, 7)
1
>>> greatest_common_divisor(0, 10)
10
"""
return
b
if
a
==
0
else
greatest_common_divisor
(
b
%
a
,
a
)
class
HillCipher
:
key_string
=
string
.
ascii_uppercase
+
string
.
digits
# This cipher takes alphanumerics into account
# i.e. a total of 36 characters
# take x and return x % len(key_string)
modulus
=
numpy
.
vectorize
(
lambda
x
:
x
%
36
)
to_int
=
numpy
.
vectorize
(
lambda
x
:
round
(
x
))
def
__init__
(
self
,
encrypt_key
):
"""
encrypt_key is an NxN numpy array
"""
self
.
encrypt_key
=
self
.
modulus
(
encrypt_key
)
# mod36 calc's on the encrypt key
self
.
check_determinant
()
# validate the determinant of the encryption key
self
.
decrypt_key
=
None
self
.
break_key
=
encrypt_key
.
shape
[
0
]
def
replace_letters
(
self
,
letter
:
str
)
->
int
:
"""
>>> hill_cipher = HillCipher(numpy.array([[2, 5], [1, 6]]))
>>> hill_cipher.replace_letters('T')
19
>>> hill_cipher.replace_letters('0')
26
"""
return
self
.
key_string
.
index
(
letter
)
def
replace_digits
(
self
,
num
:
int
)
->
str
:
"""
>>> hill_cipher = HillCipher(numpy.array([[2, 5], [1, 6]]))
>>> hill_cipher.replace_digits(19)
'T'
>>> hill_cipher.replace_digits(26)
'0'
"""
return
self
.
key_string
[
round
(
num
)]
def
check_determinant
(
self
)
->
None
:
"""
>>> hill_cipher = HillCipher(numpy.array([[2, 5], [1, 6]]))
>>> hill_cipher.check_determinant()
"""
det
=
round
(
numpy
.
linalg
.
det
(
self
.
encrypt_key
))
if
det
<
0
:
det
=
det
%
len
(
self
.
key_string
)
req_l
=
len
(
self
.
key_string
)
if
greatest_common_divisor
(
det
,
len
(
self
.
key_string
))
!=
1
:
raise
ValueError
(
f"determinant modular
{
req_l
}
of encryption key(
{
det
}
) is not co prime "
f"w.r.t
{
req_l
}
.
\n
Try another key."
)
def
process_text
(
self
,
text
:
str
)
->
str
:
"""
>>> hill_cipher = HillCipher(numpy.array([[2, 5], [1, 6]]))
>>> hill_cipher.process_text('Testing Hill Cipher')
'TESTINGHILLCIPHERR'
>>> hill_cipher.process_text('hello')
'HELLOO'
"""
chars
=
[
char
for
char
in
text
.
upper
()
if
char
in
self
.
key_string
]
last
=
chars
[
-
1
]
while
len
(
chars
)
%
self
.
break_key
!=
0
:
chars
.
append
(
last
)
return
""
.
join
(
chars
)
def
encrypt
(
self
,
text
:
str
)
->
str
:
"""
>>> hill_cipher = HillCipher(numpy.array([[2, 5], [1, 6]]))
>>> hill_cipher.encrypt('testing hill cipher')
'WHXYJOLM9C6XT085LL'
>>> hill_cipher.encrypt('hello')
'85FF00'
"""
text
=
self
.
process_text
(
text
.
upper
())
encrypted
=
""
for
i
in
range
(
0
,
len
(
text
)
-
self
.
break_key
+
1
,
self
.
break_key
):
batch
=
text
[
i
:
i
+
self
.
break_key
]
batch_vec
=
[
self
.
replace_letters
(
char
)
for
char
in
batch
]
batch_vec
=
numpy
.
array
([
batch_vec
]).
T
batch_encrypted
=
self
.
modulus
(
self
.
encrypt_key
.
dot
(
batch_vec
)).
T
.
tolist
()[
0
]
encrypted_batch
=
""
.
join
(
self
.
replace_digits
(
num
)
for
num
in
batch_encrypted
)
encrypted
+=
encrypted_batch
return
encrypted
def
make_decrypt_key
(
self
):
"""
>>> hill_cipher = HillCipher(numpy.array([[2, 5], [1, 6]]))
>>> hill_cipher.make_decrypt_key()
array([[ 6., 25.],
[ 5., 26.]])
"""
det
=
round
(
numpy
.
linalg
.
det
(
self
.
encrypt_key
))
if
det
<
0
:
det
=
det
%
len
(
self
.
key_string
)
det_inv
=
None
for
i
in
range
(
len
(
self
.
key_string
)):
if
(
det
*
i
)
%
len
(
self
.
key_string
)
==
1
:
det_inv
=
i
break
inv_key
=
(
det_inv
*
numpy
.
linalg
.
det
(
self
.
encrypt_key
)
*
numpy
.
linalg
.
inv
(
self
.
encrypt_key
)
)
return
self
.
to_int
(
self
.
modulus
(
inv_key
))
def
decrypt
(
self
,
text
:
str
)
->
str
:
"""
>>> hill_cipher = HillCipher(numpy.array([[2, 5], [1, 6]]))
>>> hill_cipher.decrypt('WHXYJOLM9C6XT085LL')
'TESTINGHILLCIPHERR'
>>> hill_cipher.decrypt('85FF00')
'HELLOO'
"""
self
.
decrypt_key
=
self
.
make_decrypt_key
()
text
=
self
.
process_text
(
text
.
upper
())
decrypted
=
""
for
i
in
range
(
0
,
len
(
text
)
-
self
.
break_key
+
1
,
self
.
break_key
):
batch
=
text
[
i
:
i
+
self
.
break_key
]
batch_vec
=
[
self
.
replace_letters
(
char
)
for
char
in
batch
]
batch_vec
=
numpy
.
array
([
batch_vec
]).
T
batch_decrypted
=
self
.
modulus
(
self
.
decrypt_key
.
dot
(
batch_vec
)).
T
.
tolist
()[
0
]
decrypted_batch
=
""
.
join
(
self
.
replace_digits
(
num
)
for
num
in
batch_decrypted
)
decrypted
+=
decrypted_batch
return
decrypted
def
main
():
N
=
int
(
input
(
"Enter the order of the encryption key: "
))
hill_matrix
=
[]
print
(
"Enter each row of the encryption key with space separated integers"
)
for
i
in
range
(
N
):
row
=
[
int
(
x
)
for
x
in
input
().
split
()]
hill_matrix
.
append
(
row
)
hc
=
HillCipher
(
numpy
.
array
(
hill_matrix
))
print
(
"Would you like to encrypt or decrypt some text? (1 or 2)"
)
option
=
input
(
"
\n
1. Encrypt
\n
2. Decrypt
\n
"
)
if
option
==
"1"
:
text_e
=
input
(
"What text would you like to encrypt?: "
)
print
(
"Your encrypted text is:"
)
print
(
hc
.
encrypt
(
text_e
))
elif
option
==
"2"
:
text_d
=
input
(
"What text would you like to decrypt?: "
)
print
(
"Your decrypted text is:"
)
print
(
hc
.
decrypt
(
text_d
))
if
__name__
==
"__main__"
:
import
doctest
doctest
.
testmod
()
main
()
You can’t perform that action at this time.