diff options
Diffstat (limited to 'columnar.py')
| -rw-r--r-- | columnar.py | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/columnar.py b/columnar.py new file mode 100644 index 0000000..4b23255 --- /dev/null +++ b/columnar.py @@ -0,0 +1,45 @@ +import numpy as np +message = input("Enter a message: ") +key = input("Key: ") +counter = 0 +length = len(message)//len(key) +width = len(key) +cipher = np.empty([length, width]) + +for i in range(length): + for j in range(width): + cipher[i][j] = ord(message[counter]) + counter+=1 + +for i in range(length): + for j in range(width): + print(chr(round(cipher[i][j])), end="\t") + print() + +def column_order(key): + sorted_key = sorted(enumerate(key), key=lambda x: x[1]) # Sort by letter + order = {index: rank + 1 for rank, (index, _) in enumerate(sorted_key)} + return [order[i] for i in range(len(key))] + +position = column_order(key) +print(position) + +def minimum(arr): + min = arr[0] + for i in range(len(arr)): + if(arr[i]<min): + min=arr[i] + return min +ciphertext = "" +while position: + mini = minimum(position) + output = mini + for i in range(len(position)): + if position[i] == mini: + output=i + break + for i in range(length): + ciphertext += chr(round(cipher[i][output])) + position.pop(output) + +print(ciphertext) |
