Skip to main content
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

1

Activate the environment

First, activate the Pipenv virtual environment:
pipenv shell
2

Run the main script

Execute the CLI application:
python main.py
3

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:
1

Sustitución (César)

Parameters:
  • Shift position: 8
Process:
  1. Converts message to numbers
  2. Shifts each number by 8 positions
  3. Converts back to letters
  4. Displays encrypted and decrypted results
Code:
pos = 8
susti_des = sustitucion_cifrar(mensaje, pos)
sustitucion_descifrar(susti_des, pos)
2

Transposición

Parameters:
  • Key (columns): 5
Process:
  1. Arranges message in a matrix with 5 columns
  2. Reads by columns to create ciphertext
  3. Reverses the process to decrypt
  4. Displays encrypted and decrypted results
Code:
clave = 5
transp_descifrar = transposicion_cifrar(mensaje, clave)
transposicion_descifrar(transp_descifrar, clave)
3

RSA

Parameters:
  • Key size: 32 bits
Process:
  1. Generates RSA key pair (e, d, n)
  2. Encrypts message using public key (e, n)
  3. Decrypts using private key (d, n)
  4. 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

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

Example transformations

InputShiftOutput
HOLA8OWSI
ABC8IJK
XYZ8FGH

Customizing parameters

You can modify the algorithm parameters by editing main.py:
# 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:
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}")

Command output formatting

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.

Build docs developers (and LLMs) love