C++ cryptography coursework: classical ciphers, a three-rotor Enigma simulator, and RSA and ElGamal written from scratch on top of NTL big integers, with the number-theory helpers they need. Every cipher is tested by encrypting and decrypting back.
- Caesar (
cesar) and a reflected Caesar variant (cesar1). - Affine cipher (
afin), key chosen at random and coprime with the alphabet. - Route (spiral grid) transposition (
ruta) and rail fence (vaya), padding withx. control: rail fence, then route, then an affine layer whose(a, b)is derived from a key string.- Three-rotor Enigma with reflector B (
enigma); encrypting twice gives the plaintext back. - RSA (
rsa): 128-bit modulus from random primes, decryption by the Chinese remainder theorem. - ElGamal (
elgamal): 512-bit prime, generatese1 e2 p, encryptsMensaje.txt. - Helpers in
RSA/andGAMAL/: Miller–Rabin, Fermat, Lucas–Lehmer, Pollard rho, Gordon strong primes, sieve of Eratosthenes, Blum-Blum-Shub, xorshift, Mersenne Twister and LFSR generators.
g++(C++17) andmakefor everything except RSA/ElGamal.- RSA and ElGamal also need NTL and GMP:
- Debian/Ubuntu:
sudo apt install g++ make libntl-dev libgmp-dev - Arch:
sudo pacman -S gcc make gmp, plusntlfrom the AUR
- Debian/Ubuntu:
make # every program into build/
make classic # only the programs that do not need NTL
make NTL_PREFIX=$HOME/opt/ntl # NTL installed somewhere else
make cleanReal output of the built programs:
$ echo 4 holamundo | build/ruta # key, message
oxxxdalohmun
holamundoxxx
$ echo 3 holamundo | build/vaya
holamundo
hmoxoaudxxlnx
holamundoxxxx
$ echo 3 holamundo | build/cesar1
zsvcumt-s
holamundo
$ build/afin # decrypt(encrypt(alphabet))
abcdefghijklmnopqrstuvwxyz
$ build/enigma # alphabet twice, encrypted, then decrypted
JDFXUEZVUNPJRDZHPZTORQVYINJDFXUEZVUNPJRDZHPZTORQVYIN
ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ
control encrypts a fixed message with a key (a string and a number). To
decrypt, give the same key, then the ciphertext on its own line (it may
contain spaces), then the length it printed (menu lines elided):
$ printf '1\nWilberCutire 5\n' | build/control
27 500
...
a - b
30 166
49
tdfdihdW WtWfy RdfW diddRiWtttfiRRdidhdhhhfyRyyy
$ printf '2\nWilberCutire 5\ntdfdihdW WtWfy RdfW diddRiWtttfiRRdidhdhhhfyRyyy\n49\n' | build/control
27 500
...
a - b
30 166
holamundoholamundoholamundoholamundoholamundoxxxx
cesar is an interactive menu (key, then 1 encrypt / 2 decrypt / 0 quit).
rsa and elgamal read and write data files (diccionario.txt,
Mensaje.txt, Datos.txt, Guardar.txt, encriptado.txt) in the current
directory, so run them from their folder, or from a copy of it, since they
overwrite the committed files. Keys are new on every run.
cd RSA && ../build/rsa # prints p, q, n, e, d; then 1 = encrypt (n e message), 2 = decrypt, 0 = quit
cd GAMAL && ../build/elgamal # prints e1, e2, p; then 1 = encrypt Mensaje.txt (e1 e2 p), 2 = decryptA round trip as run by make test on GitHub Actions:
ok rsa: 'holamundoHOLAMUNDO1234567890' -> ... -> 'holamundoHOLAMUNDO1234567890AAAAAAAAA'
ok elgamal: 'Wilber Cutire Sivincha' -> ... -> 'Wilber Cutire SivinchaAAAAAAAAAAAAAAAA'
make test # everything (needs NTL)
make test-classic # unit + golden tests, g++ only
make test-bignum # RSA and ElGamal session round trips, needs NTLtests/unit/: round tripsdecrypt(encrypt(m)) == mfor Caesar and its variant (every key), affine (200 random keys, plus the modular inverse), route and rail fence (keys 2–9, lengths 1–60, onlyxpadding allowed), thecontrolpipeline (several key strings and sizes), and Enigma (enc(enc(m)) == mover every rotor offset, no letter maps to itself).tests/golden/: each program run on<case>.inand compared with<case>.out, includingcontroldecrypting its own output and Caesar round-trippingX.tests/bignum_roundtrip.sh: startsrsa/elgamalin a scratch copy of their folder, reads the key they just generated, encrypts, feeds the ciphertext back and checks the plaintext returns, followed only by theAblock padding.
CI also checks that the FDigital/ and Protocolo/ drafts still fail to
compile, and warns if one starts compiling.
Cesar/ Cesar.1/ Caesar and reflected-Caesar variant
AFIN/ affine cipher (uses Control/mate.h)
ruta/ vaya/ route and rail-fence transpositions
Control/ rail fence + route + affine, keyed by a string
EnigmaSimulator/ three-rotor Enigma
RSA/ GAMAL/ RSA and ElGamal on NTL, with number-theory helpers
Protocolo.2/ decrypts a hard-coded rail fence + route ciphertext
FDigital/ Protocolo/ unfinished drafts (digital signature, full protocol)
Alogrithms/ RandLFSR/ Practica/ fragments, not built
Documentos/ our course presentation (slides + RSA notes)
tests/ unit, golden and NTL round-trip tests
FDigital/does not compile: it includesrsa.h, which was never committed to that folder.Protocolo/does not compile either (fdigital.hcompares anintwith astring). Both are left out of the build.Protocolo.2/main.ccis an older draft that includes headers that do not exist; onlymain2.ccis built. Its output does not look like plaintext for the keys tried, and the intended key is not known.Alogrithms/,RandLFSR/andPractica/are fragments (an emptymain, a method with no class, string exercises) and are not built.- RSA and ElGamal pad the last block with zeros, which decrypt as trailing
As. RSA keys are only 128 bits: an exercise, not secure. - Programs read words with
cin >>, socesarandrsamessages cannot contain spaces, andcesarcan emit a newline as ciphertext (its alphabet ends in\n). controlalways encrypts the same hard-coded message; the key only changes how it is encrypted.rsaandelgamaloverwrite their committed data files when run.
Coursework for an abstract algebra / discrete mathematics course at a
university, 2016. Folder names and identifiers are the original Spanish ones;
several folders are successive versions of the same assignment.
Documentos/ holds the presentation we gave for this course:
Criptografía tradicional.pptx
(classical ciphers) and RSA.pdf (the RSA scheme implemented here).
MIT — see LICENSE.