Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@
* [Shuffled Shift Cipher](ciphers/shuffled_shift_cipher.py)
* [Simple Keyword Cypher](ciphers/simple_keyword_cypher.py)
* [Simple Substitution Cipher](ciphers/simple_substitution_cipher.py)
* [Spiral Transposition](ciphers/spiral_transposition.py)
* [Transposition Cipher](ciphers/transposition_cipher.py)
* [Transposition Cipher Encrypt Decrypt File](ciphers/transposition_cipher_encrypt_decrypt_file.py)
* [Trifid Cipher](ciphers/trifid_cipher.py)
Expand Down
95 changes: 95 additions & 0 deletions ciphers/spiral_transposition.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# spiral_transposition.py

from __future__ import annotations
import math


def encrypt(plaintext: str) -> str:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file ciphers/spiral_transposition.py, please provide doctest for the function encrypt

"""
Encrypts text by writing it in a square matrix
and reading characters in spiral order.
"""
text = "".join(ch for ch in plaintext.upper() if ch.isalpha())
n = math.ceil(math.sqrt(len(text)))
matrix = [["X"] * n for _ in range(n)]

idx = 0
for r in range(n):
for c in range(n):
if idx < len(text):
matrix[r][c] = text[idx]
idx += 1

result = []
top, left, bottom, right = 0, 0, n - 1, n - 1
while top <= bottom and left <= right:
for c in range(left, right + 1):
result.append(matrix[top][c])
top += 1
for r in range(top, bottom + 1):
result.append(matrix[r][right])
right -= 1
if top <= bottom:
for c in range(right, left - 1, -1):
result.append(matrix[bottom][c])
bottom -= 1
if left <= right:
for r in range(bottom, top - 1, -1):
result.append(matrix[r][left])
left += 1
return "".join(result)


def decrypt(cipher_text: str) -> str:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file ciphers/spiral_transposition.py, please provide doctest for the function decrypt

"""
Attempts to reconstruct the original message by reversing the spiral order.
"""
L = len(cipher_text)
n = math.ceil(math.sqrt(L))
matrix = [[None] * n for _ in range(n)]

top, left, bottom, right = 0, 0, n - 1, n - 1
idx = 0
while top <= bottom and left <= right:
for c in range(left, right + 1):
matrix[top][c] = cipher_text[idx]
idx += 1
top += 1
for r in range(top, bottom + 1):
matrix[r][right] = cipher_text[idx]
idx += 1
right -= 1
if top <= bottom:
for c in range(right, left - 1, -1):
matrix[bottom][c] = cipher_text[idx]
idx += 1
bottom -= 1
if left <= right:
for r in range(bottom, top - 1, -1):
matrix[r][left] = cipher_text[idx]
idx += 1
left += 1

result = []
for row in matrix:
for ch in row:
if ch:
result.append(ch)
return "".join(result)


if __name__ == "__main__":
while True:
print("\n" + "-" * 10 + "\nSpiral Transposition Cipher\n" + "-" * 10)
print("1. Encrypt\n2. Decrypt\n3. Quit")
choice = input("Choice: ").strip()
if choice == "1":
pt = input("Plaintext: ")
print("Ciphertext:", encrypt(pt))
elif choice == "2":
ct = input("Ciphertext: ")
print("Recovered:", decrypt(ct))
elif choice == "3":
break
else:
print("Invalid option.")