With key=5, the message “HOLA MUNDO” is arranged in a 5-column matrix:
Step 1: Remove spaces → HOLAMUNDO (9 chars)Step 2: Add padding → HOLAMUNDO* (10 chars, divisible by 5)Step 3: Create matrix (5 columns):┌───┬───┬───┬───┬───┐│ H │ O │ L │ A │ │├───┼───┼───┼───┼───┤│ M │ U │ N │ D │ O │└───┴───┴───┴───┴───┘Step 4: Read by columns:Column 1: H, M → HMColumn 2: O, U → OUColumn 3: L, N → LNColumn 4: A, D → ADColumn 5: (space), O → OResult: Rearranged as HUONLDAOM*
import RSA# Generate 32-bit keyse, d, n = RSA.gen_keys(32)print(f"Public exponent (e): {e}")# Typically 65537 (a common choice)print(f"Private exponent (d): {d}")# Large number calculated from e and phi(n)print(f"Modulus (n): {n}")# Product of two prime numbers# Key properties:# - e and n are public (used for encryption)# - d is private (used for decryption)# - n is used for both encryption and decryption
Test the same message with all three algorithms to understand their characteristics:
message = "ATAQUE AL AMANECER"# Caesar: Easy to break with frequency analysis# Output: IKICM×KISTI×J×F×L# Weakness: Only 27 possible keys# Transposition: Harder to break, preserves letter frequency# Output: AAUR*QM*EA*LE*CT*AA*EN*DE*# Weakness: Pattern analysis can reveal structure# RSA: Strongest, but overkill for short messages# Output: [large list of numbers]# Strength: Computationally secure with proper key size
mensaje = "HOLA"# Caesar: OWSI# Fast, simple# Transposition: HLOA (with key=2)# Minimal rearrangement# RSA: [4 numbers]# Works well, each char becomes one number
mensaje = "ESTE ES UN MENSAJE SECRETO IMPORTANTE"# Caesar: Clear pattern shift# Transposition: Good scrambling with key=5# RSA: Long list but manageable
mensaje = "A" * 100 # 100 characters# Caesar: Fast processing# Transposition: Matrix becomes large# RSA: Very long number list# Recommendation: Use RSA for short messages,# transposition for medium, Caesar for quick tests
For stronger encryption, you can apply multiple algorithms:
import sustitucionimport transposicionimport RSAmensaje = "MENSAJE SECRETO"# Step 1: Caesar cipherpos = 8cifrado1 = sustitucion.convertiraNumero(mensaje)cifrado1 = sustitucion.cifrar(cifrado1, pos)cifrado1 = sustitucion.convertiraLetra(cifrado1)print(f"After Caesar: {cifrado1}")# Step 2: Transpositionclave = 5cifrado2 = transposicion.cifrar(cifrado1, clave)print(f"After Transposition: {cifrado2}")# Step 3: RSAbits = 32e, d, n = RSA.gen_keys(bits)cifrado_final = RSA.encrypt(cifrado2, e, n)print(f"Final encrypted: {cifrado_final}")# To decrypt, reverse the order:# RSA → Transposition → Caesar
Combining multiple ciphers can increase security, but proper implementation requires careful key management and understanding of each algorithm’s weaknesses.