The command-line interface (CLI) provides a quick way to test all three encryption algorithms at once. This guide covers how to use main.py for encryption operations.
Running the CLI
Activate the environment
First, activate the Pipenv virtual environment:
Run the main script
Execute the CLI application:
Enter your message
When prompted, type your message: Ingrese el mensaje a operar: HOLA MUNDO
The program will automatically encrypt and decrypt your message using all three algorithms.
Understanding the output
The CLI processes your message through all three algorithms sequentially:
Example session
$ python main.py
Ingrese el mensaje a operar: HOLA MUNDO
Mensaje original: HOLA MUNDO
# Sustitución (César)
Mensaje cifrado: OWSITCULW
Mensaje descifrado: HOLAMUNDO
# Transposición
Mensaje cifrado: HUONLDAOM *
Mensaje descifrado: HOLAMUNDO
# RSA
Mensaje cifrado: [210781516, 270112935, 1336237868, 270112935, 1025793116, 270112935, 1508442393, 1508442393, 1025793116]
Mensaje descifrado: HOLA MUNDO
Algorithm processing flow
The CLI follows this execution order defined in main.py:6-25:
Sustitución (César)
Parameters: Process:
Converts message to numbers
Shifts each number by 8 positions
Converts back to letters
Displays encrypted and decrypted results
Code: pos = 8
susti_des = sustitucion_cifrar(mensaje, pos)
sustitucion_descifrar(susti_des, pos)
Transposición
Parameters: Process:
Arranges message in a matrix with 5 columns
Reads by columns to create ciphertext
Reverses the process to decrypt
Displays encrypted and decrypted results
Code: clave = 5
transp_descifrar = transposicion_cifrar(mensaje, clave)
transposicion_descifrar(transp_descifrar, clave)
RSA
Parameters: Process:
Generates RSA key pair (e, d, n)
Encrypts message using public key (e, n)
Decrypts using private key (d, n)
Displays keys, encrypted list, and decrypted message
Code: bits = 32
e, d, n = RSA .gen_keys(bits)
mensaje_cifrado = RSA_cifrar(mensaje, e, n)
RSA_descifrar(mensaje_cifrado, d, n)
Detailed algorithm usage
Sustitución
Transposición
RSA
The Caesar cipher implementation shifts letters in the Spanish alphabet (including Ñ). How it works # Encrypt
def sustitucion_cifrar ( mensaje , pos ):
cifrado = sustitucion.convertiraNumero(mensaje)
cifrado = sustitucion.cifrar(cifrado, pos)
mensajecifrado = sustitucion.convertiraLetra(cifrado)
util.printResultado( "cifrado" , mensajecifrado)
return mensajecifrado
# Decrypt
def sustitucion_descifrar ( mensaje_cifrado , pos ):
mensaje_cifrado = sustitucion.convertiraNumero(mensaje_cifrado)
descifrado = sustitucion.descifrar(mensaje_cifrado, pos)
mensajedescifrado = sustitucion.convertiraLetra(descifrado)
util.printResultado( "descifrado" , mensajedescifrado)
return mensajedescifrado
Key features
Supports Spanish alphabet with Ñ
Default shift: 8 positions
Removes spaces from output
Case-insensitive processing
Input Shift Output HOLA 8 OWSI ABC 8 IJK XYZ 8 FGH
The transposition cipher rearranges characters in a columnar fashion. How it works # Encrypt
def transposicion_cifrar ( mensaje , clave ):
cifrado = transposicion.cifrar(mensaje, clave)
util.printResultado( "cifrado" , cifrado)
return cifrado
# Decrypt
def transposicion_descifrar ( mensaje , clave ):
descifrado = transposicion.descifrar(mensaje, clave)
util.printResultado( "descifrado" , descifrado)
Key features
Default key: 5 columns
Adds * padding when needed
Preserves character positions relatively
Example with key=5 Input: HOLA MUNDO (10 characters)Matrix arrangement: Read by columns: HUONLDAOM*The key determines how many columns the matrix has. A larger key means more columns and a different transposition pattern.
RSA encryption uses public-key cryptography with automatically generated keys. How it works # Generate keys
bits = 32
e, d, n = RSA .gen_keys(bits)
# Encrypt
def RSA_cifrar ( mensaje , e , n ):
mensaje_cifrado = RSA .encrypt(mensaje, e, n)
print ( f "Mensaje cifrado: { mensaje_cifrado } " )
return mensaje_cifrado
# Decrypt
def RSA_descifrar ( mensaje_cifrado , d , n ):
mensaje_descifrado = RSA .decrypt(mensaje_cifrado, d, n)
if mensaje_descifrado is not None :
print ( f "Mensaje descifrado: { mensaje_descifrado } " )
else :
print ( "Error al descifrar el mensaje." )
Key components
e (public exponent) : Used for encryption
d (private exponent) : Used for decryption
n (modulus) : Product of two prime numbers
bits : Key size (default: 32 for demo purposes)
RSA outputs a list of encrypted integers: [ 210781516 , 270112935 , 1336237868 , 270112935 , 1025793116 ]
Each number represents an encrypted character from the original message. 32-bit keys are extremely weak and only suitable for educational purposes. Real-world RSA uses 2048-bit or 4096-bit keys.
Customizing parameters
You can modify the algorithm parameters by editing main.py:
Changing Caesar shift
Changing transposition key
Changing RSA key size
# Change from shift=8 to shift=13 (ROT13)
pos = 13 # Line 11
susti_des = sustitucion_cifrar(mensaje, pos)
sustitucion_descifrar(susti_des, pos)
Using individual algorithms
If you want to use only one algorithm instead of running all three, you can import and use the functions directly:
Caesar only
Transposition only
RSA only
import sustitucion
import util
mensaje = "HOLA MUNDO"
pos = 8
# Encrypt
cifrado = sustitucion.convertiraNumero(mensaje)
cifrado = sustitucion.cifrar(cifrado, pos)
mensajecifrado = sustitucion.convertiraLetra(cifrado)
print ( f "Cifrado: { mensajecifrado } " )
# Decrypt
numeros = sustitucion.convertiraNumero(mensajecifrado)
descifrado = sustitucion.descifrar(numeros, pos)
mensajedescifrado = sustitucion.convertiraLetra(descifrado)
print ( f "Descifrado: { mensajedescifrado } " )
The CLI uses utility functions from util.py to format output consistently:
util.printResultado( "cifrado" , mensajecifrado)
util.printResultado( "descifrado" , mensajedescifrado)
This ensures all algorithm outputs follow the same format for easy comparison.
Error handling
The CLI includes basic error handling:
RSA decryption errors : Returns None and prints error message
Invalid input : Handled by individual algorithm modules
Empty messages : Processed but may produce unexpected results
The CLI is designed for quick testing and demonstration. For production use, implement more robust error handling and input validation.