-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoubleTranspositionCipher.py
156 lines (118 loc) · 6.46 KB
/
doubleTranspositionCipher.py
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
# making Double Transposition cipher encryption and decryption using python
import sys
import math
import os
design = """
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ ___ _ _ _____ _ _ _ ___ _ _ +
+ / \___ _ _| |__ | | ___ /__ \_ __ __ _ _ __ ___ _ __ ___ ___(_) |_(_) ___ _ __ / __(_)_ __ | |__ ___ _ __ +
+ / /\ / _ \| | | | '_ \| |/ _ \ / /\/ '__/ _` | '_ \/ __| '_ \ / _ \/ __| | __| |/ _ \| '_ \ / / | | '_ \| '_ \ / _ \ '__| +
+ / /_// (_) | |_| | |_) | | __/ / / | | | (_| | | | \__ \ |_) | (_) \__ \ | |_| | (_) | | | | / /___| | |_) | | | | __/ | +
+ /___,' \___/ \__,_|_.__/|_|\___| \/ |_| \__,_|_| |_|___/ .__/ \___/|___/_|\__|_|\___/|_| |_| \____/|_| .__/|_| |_|\___|_| +
+ |_| |_| +
+ -By 7omahawk +
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
"""
# this function is for encryption steps
def encryptionLoop(key, userInput):
userInput = userInput.replace(" ","") # excluding space from the sentence
sizeOfKey = len(str(key)) # column size
sizeOfInput = len(userInput)
numberOfRow = math.ceil(sizeOfInput / sizeOfKey) # row size
totalLengthOfInput = sizeOfKey * numberOfRow
# for padding
if totalLengthOfInput != sizeOfInput:
padding = totalLengthOfInput - sizeOfInput
for i in range(padding):
userInput = userInput + "z"
matrix = [["" for space in range(sizeOfKey)] for space in range(numberOfRow)] # making matrix
# row by row write-------------------###[step 1]
# inserting every single value of input into matrix with padding
for i in range(numberOfRow):
for j in range(sizeOfKey):
index = i * sizeOfKey + j # when i = 0,1 and j = 0,1 then index =[[0][0],[0][1]],[[1][0],[1][1]]
if index < len(userInput):
matrix[i][j] = userInput[index]
# shuffle the matrix with the key-------------------###[step 2]
strKey = str(key) # making integer key to string for store every individual index
shuffledMatrix = [["" for space in range(sizeOfKey)] for space in range(numberOfRow)] # making new matrix for store the shuffled value
for i in range(numberOfRow):
for j in range(sizeOfKey):
shuffledMatrix[i][j] = matrix[i][int(strKey[j])-1]
# count by column-------------------###[step 3]
cipher = ""
for i in range(sizeOfKey): # have to reapet as column size
for j in range(numberOfRow): # have to reapet as row size
value = shuffledMatrix[j][i] # when i = 0,1 and j = 0,1 then value = [[0][0],[1][0]], [[0][1],[1][1]]
cipher = cipher + value
return cipher
# main encryption function
def encryption(key, userInput):
cipher = encryptionLoop(key, userInput) # single encryption cipher
userInput = cipher
cipher = encryptionLoop(key, userInput) # double encryption cipher
print(f"The encrypted message is: {cipher}")
# this function is for decryption steps
def decryptionLoop(key, userInput):
sizeOfKey = len(str(key)) # column size
sizeOfInput = len(userInput)
numberOfRow = math.ceil(sizeOfInput / sizeOfKey) # row size
matrix = [["" for space in range(sizeOfKey)] for space in range(numberOfRow)] # making matrix
# column by column write-------------------###[step 1]
for i in range(sizeOfKey):
for j in range(numberOfRow):
index = i * numberOfRow + j # when i = 0,1 and j = 0,1 then index =[[0][0],[0][1]],[[1][0],[1][1]]
if index < len(userInput):
matrix[j][i] = userInput[index] # when i = 0,1 and j = 0,1 then value of matrix= [[0][0],[1][0]], [[0][1],[1][1]]
# shuffle by key-------------------###[step 2]
strKey = str(key) # making integer key to string for store every individual index
shuffledMatrix = [["" for space in range(sizeOfKey)] for space in range(numberOfRow)] # making new matrix for store the shuffled value
for i in range(numberOfRow):
for j in range(sizeOfKey):
shuffledMatrix[i][int(strKey[j])-1] = matrix[i][j]
# write the plaintext-------------------###[step 3]
plaintext = ""
for i in range(numberOfRow): # have to reapet as row size
for j in range(sizeOfKey): # have to reapet as column size
value = shuffledMatrix[i][j] # when i = 0,1 and j = 0,1 then value = [[0][0],[0][1]], [[1][0],[1][1]]
plaintext = plaintext + value
return plaintext
# main decryption function
def decryption(key, userInput):
plaintext = decryptionLoop(key, userInput) # single decryption plaintext
userInput = plaintext
plaintext = decryptionLoop(key, userInput) # double decryption plaintext
finalText = ""
for i in range(len(plaintext)):
if i < inputSize: # input size is a global variable
finalText = finalText + plaintext[i]
print(f"The decrypted message is: {finalText}")
while(True):
print(design)
print("Enter your choice(Number): ")
print("1. Encryption: ")
print("2. Decryption: ")
print("3. Exit: ")
number = int(input("Enter the number: "))
def choice(number):
if number == 1:
userInput = input("Enter your text to encrypt (A-Z): ")
key = int(input("Enter the key (ex. 24315): "))
userInput = userInput.lower()
encryption(key, userInput)
userInput = userInput.replace(" ","") # excluding space from the sentence
sizeOfInput = len(userInput)
global inputSize # this is the global variable
inputSize = sizeOfInput
elif number == 2:
userInput = input("Enter your text to decrypt (A-Z): ")
key = int(input("Enter the key (ex. 24315): "))
userInput = userInput.lower()
decryption(key, userInput)
elif number == 3:
sys.exit()
else:
print("Input should be a number from 1 to 3")
choice(number)
input() # press enter to clear screen
os.system('clear') # clear screen