Skip to main content
The sustitucion module implements a Caesar cipher algorithm that shifts letters in the Spanish alphabet (including Ñ) by a specified position.

Module constants

abecedario

A dictionary mapping Spanish alphabet letters (A-Z, including Ñ) to their numeric positions.
abecedario = {'A': 0, 'B': 1, 'C': 2, 'D': 3, 'E': 4, 'F': 5, 'G': 6, 'H': 7, 
              'I': 8, 'J': 9, 'K': 10, 'L': 11, 'M': 12, 'N': 13, 'Ñ': 14, 
              'O': 15, 'P': 16, 'Q': 17, 'R': 18, 'S': 19, 'T': 20, 'U': 21, 
              'V': 22, 'W': 23, 'X': 24, 'Y': 25, 'Z': 26}
Type: dict[str, int]

abecedario_inverso

A reverse mapping dictionary that converts numeric positions back to letters. Generated automatically from abecedario.
abecedario_inverso = {0: 'A', 1: 'B', 2: 'C', ..., 26: 'Z'}
Type: dict[int, str]
These dictionaries are used internally by the conversion functions but can be accessed directly if needed for custom operations.

convertiraNumero

Converts a message string into a list of numeric values based on the Spanish alphabet.
def convertiraNumero(mensaje)
mensaje
str
required
The message to convert. Letters are automatically converted to uppercase. Non-alphabetic characters are ignored.
return
list[int]
A list of integers representing each letter’s position in the alphabet (0-26). Ñ is position 14.
The Spanish alphabet mapping includes Ñ at position 14, making the total alphabet size 27 characters (A=0 through Z=26).

Example

import sustitucion

mensaje = "HOLA"
numeros = sustitucion.convertiraNumero(mensaje)
print(numeros)  # Output: [7, 15, 11, 0]

cifrar

Encrypts a list of numeric values by shifting each number by a specified position using modular arithmetic.
def cifrar(cifrado, pos)
cifrado
list[int]
required
A list of numeric values to encrypt (typically from convertiraNumero).
pos
int
required
The number of positions to shift each letter forward in the alphabet (the encryption key).
return
list[int]
The encrypted list with each value shifted by pos modulo 27.

Example

import sustitucion

mensaje = "HOLA"
pos = 8

# Convert to numbers
cifrado = sustitucion.convertiraNumero(mensaje)

# Encrypt
cifrado = sustitucion.cifrar(cifrado, pos)
print(cifrado)  # Shifted values

descifrar

Decrypts a list of encrypted numeric values by shifting each number backwards by the specified position.
def descifrar(cifrado, pos)
cifrado
list[int]
required
A list of encrypted numeric values to decrypt.
pos
int
required
The number of positions to shift each letter backwards in the alphabet (must match the encryption key).
return
list[int]
The decrypted list with each value shifted backwards by pos modulo 27.

Example

import sustitucion

mensaje_cifrado = "PWTM"
pos = 8

# Convert to numbers
numeros = sustitucion.convertiraNumero(mensaje_cifrado)

# Decrypt
descifrado = sustitucion.descifrar(numeros, pos)
print(descifrado)  # Original numeric values

convertiraLetra

Converts a list of numeric values back into a string of letters.
def convertiraLetra(cifrado)
cifrado
list[int]
required
A list of integers representing alphabet positions (0-26) to convert back to letters.
return
str
The string representation of the numeric values.

Example

import sustitucion

numeros = [7, 15, 11, 0]
mensaje = sustitucion.convertiraLetra(numeros)
print(mensaje)  # Output: "HOLA"

Complete encryption workflow

Here’s a complete example showing how to encrypt and decrypt a message:
import sustitucion

mensaje = "HOLA MUNDO"
pos = 8

# Step 1: Convert message to numbers
cifrado = sustitucion.convertiraNumero(mensaje)

# Step 2: Encrypt the numbers
cifrado = sustitucion.cifrar(cifrado, pos)

# Step 3: Convert back to letters
mensaje_cifrado = sustitucion.convertiraLetra(cifrado)
print(f"Encrypted: {mensaje_cifrado}")

Alphabet reference

The module uses the following Spanish alphabet mapping:
LetterValueLetterValueLetterValue
A0J9S19
B1K10T20
C2L11U21
D3M12V22
E4N13W23
F5Ñ14X24
G6O15Y25
H7P16Z26
I8Q17

Build docs developers (and LLMs) love