Skip to main content
The transposicion module implements a columnar transposition cipher that encrypts messages by writing them in rows and reading them in columns.

cifrar

Encrypts a message using columnar transposition by arranging characters into a grid and reading column-by-column.
def cifrar(mensaje, clave)
mensaje
str
required
The message to encrypt. Spaces are automatically removed before encryption.
clave
int
required
The number of columns to use in the transposition grid (the encryption key).
return
str
The encrypted message. If the message length is not divisible by the key, asterisks (*) are added as padding.
The function automatically pads the message with asterisks (*) if its length is not a multiple of the key value. This ensures a complete rectangular grid.

How it works

The encryption process:
  1. Removes all spaces from the message
  2. Calculates the number of rows needed based on the key (columns)
  3. Pads the message with asterisks if needed to complete the grid
  4. Arranges the message in a grid row-by-row
  5. Reads the grid column-by-column to create the ciphertext

Example

import transposicion

mensaje = "HOLA MUNDO"
clave = 5

cifrado = transposicion.cifrar(mensaje, clave)
print(f"Encrypted: {cifrado}")
# Output: "HMNOUDAL*O*"

# The grid visualization:
# H O L A M
# U N D O *
# Reading columns: H,U then O,N then L,D then A,O then M,*

Real-world usage

import transposicion

mensaje = "ATTACK AT DAWN"
clave = 4

resultado = transposicion.cifrar(mensaje, clave)
print(resultado)
# The spaces are removed and message is encrypted

descifrar

Decrypts a message that was encrypted using columnar transposition.
def descifrar(mensaje_cifrado, clave)
mensaje_cifrado
str
required
The encrypted message to decrypt. Spaces are automatically removed.
clave
int
required
The number of columns used during encryption (must match the encryption key).
return
str
The decrypted message with padding asterisks removed from the end.
The key used for decryption must match the key used for encryption, otherwise the output will be gibberish.

How it works

The decryption process:
  1. Removes all spaces from the encrypted message
  2. Calculates the number of rows in the original grid
  3. Reads the encrypted message column-by-column
  4. Reconstructs the original message row-by-row
  5. Removes trailing asterisk padding characters

Example

import transposicion

mensaje_cifrado = "HMNOUDAL*O*"
clave = 5

descifrado = transposicion.descifrar(mensaje_cifrado, clave)
print(f"Decrypted: {descifrado}")
# Output: "HOLAMUNDO"

Real-world usage

import transposicion

# Decrypt a message
mensaje_cifrado = "TACDTKWANA*"
clave = 4

original = transposicion.descifrar(mensaje_cifrado, clave)
print(original)
# Returns the original message without padding

Complete encryption workflow

Here’s a complete example demonstrating encryption and decryption:
import transposicion

# Original message
mensaje = "MEET ME AT THE PARK"
print(f"Original: {mensaje}")

# Encrypt
clave = 6
cifrado = transposicion.cifrar(mensaje, clave)
print(f"Encrypted: {cifrado}")

# Decrypt
descifrado = transposicion.descifrar(cifrado, clave)
print(f"Decrypted: {descifrado}")

Key selection guidelines

Choosing the right key:
  • Smaller keys (2-3): Less secure but faster
  • Medium keys (4-8): Good balance of security and usability
  • Larger keys (9+): More secure but requires longer messages for effectiveness
  • The key should be smaller than the message length for meaningful encryption

Example with different keys

import transposicion

mensaje = "TRANSPOSITION CIPHER"

# Different key sizes
for clave in [3, 5, 7]:
    cifrado = transposicion.cifrar(mensaje, clave)
    print(f"Key {clave}: {cifrado}")

Build docs developers (and LLMs) love