transposition cipher Algorithm

The Transposition Cipher algorithm is a cryptographic technique used for encoding messages by rearranging the characters of the plaintext in a specific way. This encryption method works by systematically changing the position of the letters in the given input, making it difficult for unauthorized users to decipher the original message. Unlike substitution ciphers, which replace letters with other letters, transposition ciphers maintain the original letters but alter their positions within the message. There are multiple ways to implement the transposition cipher algorithm, with one common approach being the columnar transposition. In this method, the plaintext message is written out in rows of fixed length, forming a grid. The characters are then read column by column, and the resulting ciphertext is formed by concatenating the columns together. The order of the columns can be determined by a keyword or a predetermined pattern. Another popular method is the rail fence cipher, where the plaintext is written in a zigzag pattern across multiple levels, and the ciphertext is formed by reading the characters along the levels sequentially. Despite its simplicity, the transposition cipher can be combined with other cryptographic techniques to create more secure encryption schemes.
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, message):
    """
    >>> 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, message):
    """
    >>> 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()

LANGUAGE:

DARK MODE: