-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Lucas Swartsenburg
committed
Dec 11, 2023
1 parent
c420f70
commit e330cdf
Showing
3 changed files
with
93 additions
and
98 deletions.
There are no files selected for viewing
140 changes: 68 additions & 72 deletions
140
src/cipher_algorithms/ciphers/columnar_transposition/algo.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,106 +1,102 @@ | ||
# Columnar Transposition | ||
import math | ||
|
||
|
||
# encrypt func | ||
def encryptMessage(msg, key): | ||
cipher = "" | ||
cipher = "" | ||
|
||
# track key indices | ||
k_indx = 0 | ||
|
||
msg_len = float(len(msg)) | ||
msg_lst = list(msg) | ||
key_lst = sorted(list(key)) | ||
|
||
# track key indices | ||
k_indx = 0 | ||
# calculate column of the matrix | ||
col = len(key) | ||
|
||
msg_len = float(len(msg)) | ||
msg_lst = list(msg) | ||
key_lst = sorted(list(key)) | ||
# calculate maximum row of the matrix | ||
row = int(math.ceil(msg_len / col)) | ||
|
||
# calculate column of the matrix | ||
col = len(key) | ||
|
||
# calculate maximum row of the matrix | ||
row = int(math.ceil(msg_len / col)) | ||
# adding padding char | ||
fill_null = int((row * col) - msg_len) | ||
msg_lst.extend("_" * fill_null) | ||
|
||
# adding padding char | ||
fill_null = int((row * col) - msg_len) | ||
msg_lst.extend('_' * fill_null) | ||
# create Matrix and insert message and padding characters row-wise | ||
matrix = [msg_lst[i : i + col] for i in range(0, len(msg_lst), col)] | ||
|
||
# create Matrix and insert message and padding characters row-wise | ||
matrix = [msg_lst[i: i + col] | ||
for i in range(0, len(msg_lst), col)] | ||
# read matrix column-wise using key | ||
for _ in range(col): | ||
curr_idx = key.index(key_lst[k_indx]) | ||
cipher += "".join([row[curr_idx] for row in matrix]) | ||
k_indx += 1 | ||
|
||
# read matrix column-wise using key | ||
for _ in range(col): | ||
curr_idx = key.index(key_lst[k_indx]) | ||
cipher += ''.join([row[curr_idx] | ||
for row in matrix]) | ||
k_indx += 1 | ||
return cipher | ||
|
||
return cipher | ||
|
||
# decrypt func | ||
def decryptMessage(cipher, key): | ||
msg = "" | ||
msg = "" | ||
|
||
# track key indices | ||
k_indx = 0 | ||
# track key indices | ||
k_indx = 0 | ||
|
||
# track msg indices | ||
msg_indx = 0 | ||
msg_len = float(len(cipher)) | ||
msg_lst = list(cipher) | ||
# track msg indices | ||
msg_indx = 0 | ||
msg_len = float(len(cipher)) | ||
msg_lst = list(cipher) | ||
|
||
# calculate column of the matrix | ||
col = len(key) | ||
|
||
# calculate maximum row of the matrix | ||
row = int(math.ceil(msg_len / col)) | ||
# calculate column of the matrix | ||
col = len(key) | ||
|
||
#converting key into a list so that we can sort alphabetically | ||
key_lst = sorted(list(key)) | ||
# calculate maximum row of the matrix | ||
row = int(math.ceil(msg_len / col)) | ||
|
||
# create an empty matrix to | ||
# store deciphered message | ||
dec_cipher = [] | ||
for _ in range(row): | ||
dec_cipher += [[None] * col] | ||
# converting key into a list so that we can sort alphabetically | ||
key_lst = sorted(list(key)) | ||
|
||
# Arrange the matrix column wise | ||
for _ in range(col): | ||
curr_idx = key.index(key_lst[k_indx]) | ||
# create an empty matrix to | ||
# store deciphered message | ||
dec_cipher = [] | ||
for _ in range(row): | ||
dec_cipher += [[None] * col] | ||
|
||
for j in range(row): | ||
dec_cipher[j][curr_idx] = msg_lst[msg_indx] | ||
msg_indx += 1 | ||
k_indx += 1 | ||
# Arrange the matrix column wise | ||
for _ in range(col): | ||
curr_idx = key.index(key_lst[k_indx]) | ||
|
||
# convert decrypted msg matrix into a string | ||
try: | ||
msg = ''.join(sum(dec_cipher, [])) | ||
except TypeError: | ||
raise TypeError("This program cannot", | ||
"handle repeating words.") | ||
for j in range(row): | ||
dec_cipher[j][curr_idx] = msg_lst[msg_indx] | ||
msg_indx += 1 | ||
k_indx += 1 | ||
|
||
null_count = msg.count('_') | ||
# convert decrypted msg matrix into a string | ||
try: | ||
msg = "".join(sum(dec_cipher, [])) | ||
except TypeError: | ||
raise TypeError("This program cannot", "handle repeating words.") | ||
|
||
if null_count > 0: | ||
return msg[: -null_count] | ||
null_count = msg.count("_") | ||
|
||
return msg | ||
if null_count > 0: | ||
return msg[:-null_count] | ||
|
||
return msg | ||
|
||
######Example###### | ||
""" | ||
msg = "the quick brown fox jumps over the lazy dog" | ||
print(msg) | ||
cipher = encryptMessage(msg) | ||
print("Encrypted Message: {}". | ||
format(cipher)) | ||
|
||
print("Decryped Message: {}". | ||
format(decryptMessage(cipher))) | ||
---outputs the following--- | ||
""" | ||
---outputs the following--- | ||
the quick brown fox jumps over the lazy dog | ||
Encrypted Message: hu wou rezoeibnxmo ygtqkofjsehad cr pvtl _ | ||
Decryped Message: the quick brown fox jumps over the lazy dog | ||
""" | ||
if __name__ == "__main__": | ||
msg = "the quick brown fox jumps over the lazy dog" | ||
|
||
print(msg) | ||
cipher = encryptMessage(msg) | ||
print("Encrypted Message: {}".format(cipher)) | ||
|
||
print("Decryped Message: {}".format(decryptMessage(cipher))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters