Classical Crypto / C++

Source ยป

Collection of C++ programs to replicate classical ciphers. These programs are old, rough, and often can be improved vastly. Programs are tested using g++ with C++14.

Affine-cipher.cpp

Supports encryption and decryption.

  1. Choose either encryption or decryption (e or d);
  2. Enter input string (to be encrypted or decrypted);
  3. Enter \(a\) and \(b\), such that \(a\) and \(b\) are coprime.

Example

Encrypt or Decrypt? [e/d] = e

Input string: 'Tis but a scratch!

a and b must be coprime
a = 51
b = 17

'Yjz qxy r zparypk!
Encrypt or Decrypt? [e/d] = d

Input string: 'Yjz qxy r zparypk!

a and b must be coprime
a = 51
b = 17

'Tis but a scratch!

Affine-cipher-v2.cpp

Identical in functionality to the first version.

Example

Encrypt or decrypt [e/d] = e

Input string: Oh, look. There's some lovely filth over here.

a and b must be coprime
a = 69
b = 7

Lw, mllv. Swxkx'b bldx mlaxmz onmsw laxk wxkx.
Encrypt or decrypt [e/d] = d

Input string: Lw, mllv. Swxkx'b bldx mlaxmz onmsw laxk wxkx.

a and b must be coprime
a = 69
b = 7

Oh, look. There's some lovely filth over here.

Atbash-cipher.cpp

Encryption and decryption use the same process. Does not check for repeated characters.

  1. Enter modified alphabet used for the key;
  2. Prints normal alphabet and modified alphabet;
  3. Enter input string (to be encrypted or decrypted).

Example

Key:
a = q
b = a
c = z
d = w
e = s
f = x
g = e
h = d
i = c
j = r
k = f
l = v
m = t
n = g
o = b
p = y
q = h
r = n
s = u
t = j
u = m
v = i
w = k
x = o
y = l
z = p
abcdefghijklmnopqrstuvwxyz
qazwsxedcrfvtgbyhnujmikolp
Message: Hello world!
Dsvvb kbnvw!

Atbash-cipher-v2.cpp

Identical in functionality to the first version except it takes the key as a long string.

Example

Key (length 26): qazwsxedcrfvtgbyhnujmikolp

abcdefghijklmnopqrstuvwxyz
qazwsxedcrfvtgbyhnujmikolp

Message: 'Tis but a scratch!
'Jcu amj q uznqjzd!

Backwards-cipher.cpp

  1. Prints a string backwards.

Code includes two alternative methods for printing a string backwards that have been commented out.

Example

Hello world!
!dlrow olleH
'Tis but a scratch!
!hctarcs a tub siT'

Caesar-cipher.cpp

  1. Enter the input string and all possible shifts are printed.

This version does not rotate capital letters correctly!

Example

Hello world!

ROT0 - Hello world!
ROT1 - RfmmpRxpsmeR
ROT2 - RgnnqRyqtnfR
ROT3 - RhoorRzruogR
ROT4 - RippsRasvphR
ROT5 - RjqqtRbtwqiR
ROT6 - RkrruRcuxrjR
ROT7 - RlssvRdvyskR
ROT8 - RmttwRewztlR
ROT9 - RnuuxRfxaumR
ROT10 - RovvyRgybvnR
ROT11 - RpwwzRhzcwoR
ROT12 - RqxxaRiadxpR
ROT13 - RryybRjbeyqR
ROT14 - RszzcRkcfzrR
ROT15 - RtaadRldgasR
ROT16 - RubbeRmehbtR
ROT17 - RvccfRnficuR
ROT18 - RwddgRogjdvR
ROT19 - RxeehRphkewR
ROT20 - RyffiRqilfxR
ROT21 - RzggjRrjmgyR
ROT22 - RahhkRsknhzR
ROT23 - RbiilRtloiaR
ROT24 - RcjjmRumpjbR
ROT25 - RdkknRvnqkcR

Caesar-cipher-v2.cpp

Added correct functionality for special characters (i.e. !) and reduced the length of the code. This version does not rotate capital letters correctly!

Example

Hello world!

ROT0 - Hello world!
ROT1 - Hfmmp xpsme!
ROT2 - Hgnnq yqtnf!
ROT3 - Hhoor zruog!
ROT4 - Hipps asvph!
ROT5 - Hjqqt btwqi!
ROT6 - Hkrru cuxrj!
ROT7 - Hlssv dvysk!
ROT8 - Hmttw ewztl!
ROT9 - Hnuux fxaum!
ROT10 - Hovvy gybvn!
ROT11 - Hpwwz hzcwo!
ROT12 - Hqxxa iadxp!
ROT13 - Hryyb jbeyq!
ROT14 - Hszzc kcfzr!
ROT15 - Htaad ldgas!
ROT16 - Hubbe mehbt!
ROT17 - Hvccf nficu!
ROT18 - Hwddg ogjdv!
ROT19 - Hxeeh phkew!
ROT20 - Hyffi qilfx!
ROT21 - Hzggj rjmgy!
ROT22 - Hahhk sknhz!
ROT23 - Hbiil tloia!
ROT24 - Hcjjm umpjb!
ROT25 - Hdkkn vnqkc!

Caesar-cipher-v3.cpp

Added the ability to rotate capital letters. Added the ability to rotate numbers, but that has been commented out.

Example

Hello world!

ROT0 - Hello world!
ROT1 - Ifmmp xpsme!
ROT2 - Jgnnq yqtnf!
ROT3 - Khoor zruog!
ROT4 - Lipps asvph!
ROT5 - Mjqqt btwqi!
ROT6 - Nkrru cuxrj!
ROT7 - Olssv dvysk!
ROT8 - Pmttw ewztl!
ROT9 - Qnuux fxaum!
ROT10 - Rovvy gybvn!
ROT11 - Spwwz hzcwo!
ROT12 - Tqxxa iadxp!
ROT13 - Uryyb jbeyq!
ROT14 - Vszzc kcfzr!
ROT15 - Wtaad ldgas!
ROT16 - Xubbe mehbt!
ROT17 - Yvccf nficu!
ROT18 - Zwddg ogjdv!
ROT19 - Axeeh phkew!
ROT20 - Byffi qilfx!
ROT21 - Czggj rjmgy!
ROT22 - Dahhk sknhz!
ROT23 - Ebiil tloia!
ROT24 - Fcjjm umpjb!
ROT25 - Gdkkn vnqkc!

Null-cipher.cpp

  1. Enter the input string;
  2. Enter the pattern separated by spaces;
  3. Prints nth letter of each word in the input string based on the pattern.

Example

Separate words with spaces.
Input string: I'm terrible, don't seriously attempt to try raising poverty please!
Separate positive integers with spaces: 1 1 4 1 1 1 2 2 1 7
It'satrap!
Separate words with spaces.
Input string: Susan says Gail lies. Matt lets Susan feel jovial. Elated, angry?
Separate positive integers with spaces: 1 2 3 1 2 3 1 2 3 1 2
SailatSevEn

Null-cipher-v2.cpp

Identical in functionality to the first version.

Example

Separate words with spaces.
Input string: Can one decipher elegantly?
Separate positive integers with spaces: 1 1 1 1
Code
Separate words with spaces.
Input string: Race quickly, now!
Separate positive integers with spaces: 1 2 1
Run

Polybius-square.cpp

Supports encryption and decryption.

  1. Choose either encryption or decryption (e or d);
  2. Encryption
    • Enter input string;
  3. Decryption
    • Enter coordinates (separated by spaces).

Example

Encrypt or decrypt [e/d] = e
Plaintext: 'Tis but a scratch!
44 24 43 12 45 44 11 43 13 42 11 44 13 23
Encrypt or decrypt [e/d] = d
Coordinates (separate with spaces): 44 24 43 12 45 44 11 43 13 42 11 44 13 23
TISBUTASCRATCH

Polybius-square-v2.cpp

Nearly identical to the first version but instead uses math to encrypt and decrypt.

Because numbers and characters are not mapped one-to-one, if an incorrect coordinate is entered, a character is still returned. For example, if the coordinate 110 is input, s is output.

Example

Encrypt or decrypt [e/d] = e
Plaintext: She's a witch!
43 23 15 43 11 52 24 44 13 23
Encrypt or decrypt [e/d] = d
Coordinates (separate with spaces): 43 23 15 43 11 52 24 44 13 23
SHESAWITCH

Tokenize-with-padding.cpp

Tokenizes a string (splits it into n-length blocks) with padding.

Example

Plaintext: 'Tis but a scratch!
Block size: 6
Padding character: X

'Tisbu tascra tch!XX
Plaintext: Hello world!
Block size: 3
Padding character: ^

Hel low orl d!^