FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
TheAlgorithms-Python/ciphers/transposition_cipher.py at master · windNight/TheAlgorithms-Python · GitHub
windNight
/
TheAlgorithms-Python
Public
forked from
TheAlgorithms/Python
Notifications
You must be signed in to change notification settings
Fork
1
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
Expand file tree
Breadcrumbs
TheAlgorithms-Python
/
ciphers
/
transposition_cipher.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
70 lines (55 loc) · 1.77 KB
Breadcrumbs
TheAlgorithms-Python
/
ciphers
/
transposition_cipher.py
Copy path
File metadata and controls
70 lines (55 loc) · 1.77 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
import
math
"""
In cryptography, the TRANSPOSITION cipher is a method of encryption where the
positions of plaintext are shifted a certain number(determined by the key) that
follows a regular system that results in the permuted text, known as the encrypted
text. The type of transposition cipher demonstrated under is the ROUTE cipher.
"""
def
main
():
message
=
input
(
"Enter message: "
)
key
=
int
(
input
(
"Enter key [2-%s]: "
%
(
len
(
message
)
-
1
)))
mode
=
input
(
"Encryption/Decryption [e/d]: "
)
if
mode
.
lower
().
startswith
(
"e"
):
text
=
encryptMessage
(
key
,
message
)
elif
mode
.
lower
().
startswith
(
"d"
):
text
=
decryptMessage
(
key
,
message
)
# Append pipe symbol (vertical bar) to identify spaces at the end.
print
(
"Output:
\n
%s"
%
(
text
+
"|"
))
def
encryptMessage
(
key
:
int
,
message
:
str
)
->
str
:
"""
>>> encryptMessage(6, 'Harshil Darji')
'Hlia rDsahrij'
"""
cipherText
=
[
""
]
*
key
for
col
in
range
(
key
):
pointer
=
col
while
pointer
<
len
(
message
):
cipherText
[
col
]
+=
message
[
pointer
]
pointer
+=
key
return
""
.
join
(
cipherText
)
def
decryptMessage
(
key
:
int
,
message
:
str
)
->
str
:
"""
>>> decryptMessage(6, 'Hlia rDsahrij')
'Harshil Darji'
"""
numCols
=
math
.
ceil
(
len
(
message
)
/
key
)
numRows
=
key
numShadedBoxes
=
(
numCols
*
numRows
)
-
len
(
message
)
plainText
=
[
""
]
*
numCols
col
=
0
row
=
0
for
symbol
in
message
:
plainText
[
col
]
+=
symbol
col
+=
1
if
(
(
col
==
numCols
)
or
(
col
==
numCols
-
1
)
and
(
row
>=
numRows
-
numShadedBoxes
)
):
col
=
0
row
+=
1
return
""
.
join
(
plainText
)
if
__name__
==
"__main__"
:
import
doctest
doctest
.
testmod
()
main
()
Back
|
FazBrowse Home
|
New Git URL