This article explores classical ciphers historically used. In the past, amid low productivity and primitive computing, these ciphers offered protection.
This article will introduce some commonly used classical ciphers, which have been applied in history, reflecting the wisdom of ancient people. In ancient times, with low productivity and backward computing devices, classical ciphers had a certain protective effect. However, with the improvement of computer computing power nowadays, these classical ciphers have become very vulnerable and no longer practical.
Nowadays, there are many methods for breaking these classical ciphers. When I have the opportunity later on, I will summarize some of the decryption techniques for classical ciphers.
Caesar Cipher
When it comes to classical ciphers, one classic example is the Caesar Cipher. The Caesar Cipher is a simple substitution encryption method based on letter shifting.
Core Idea: The core idea of the Caesar Cipher is to obtain the ciphertext by shifting each letter in the plaintext by a fixed number. This number of shifts is called the offset or key.
Algorithm Steps:
 Determine the offset, i.e., the number of shifts.
 For encryption, shift each letter in the plaintext backward by the offset to obtain the ciphertext.
 For decryption, shift each letter in the ciphertext forward by the offset to obtain the plaintext.
TypeScript Implementation:
This TypeScript implementation provides encryption and decryption functions for the Caesar Cipher. The encryption function accepts plaintext and the offset as parameters and returns ciphertext, while the decryption function accepts ciphertext and the offset as parameters and returns plaintext.
Rail Fence Cipher
The Rail Fence Cipher is a classical substitution encryption method based on rearranging the letters in the plaintext. The core idea of the Rail Fence Cipher is to rearrange the letters in the plaintext to form a matrix with a specific number of rows, and then read the letters in the matrix in a certain order to obtain the ciphertext.
Algorithm Steps:
 Determine the height of the rail fence, i.e., the number of rows in the matrix.
 Write the plaintext into a matrix according to the height of the rail fence.
 Read the letters in the matrix in a specific order to obtain the ciphertext.
 For decryption, knowing the height of the rail fence is necessary to correctly rearrange the letters in the ciphertext to obtain the plaintext.
Idea: The Rail Fence Cipher is a substitution encryption method based on rearranging letters. Encryption and decryption are achieved by reading the letters in the matrix in a specific order.
TypeScript Implementation:
This TypeScript implementation provides encryption and decryption functions for the Rail Fence Cipher. The encryption function accepts plaintext and the height of the rail fence as parameters and returns ciphertext, while the decryption function accepts ciphertext and the height of the rail fence as parameters and returns plaintext.
Route Cipher
The Route Cipher, also known as the Path Transposition Cipher, is a substitution encryption method based on matrix rearrangement. Its core idea is to read the characters in the matrix according to a predetermined path order to encrypt or decrypt messages. The essence of the Route Cipher is to arrange the plaintext into a matrix along a specific path and then read the characters in the matrix according to the predetermined path order to obtain the ciphertext.
Algorithm Steps:
 Arrange the plaintext into a matrix along a specific path.
 Read the characters in the matrix according to the predetermined path order to obtain the ciphertext.
 For decryption, read the characters in the ciphertext according to the same path order to reconstruct the plaintext.
TypeScript Implementation:
This TypeScript implementation provides encryption and decryption functions for the Route Cipher. The encryption function accepts plaintext, the number of rows and columns in the matrix, and the path as parameters, and returns ciphertext. The decryption function accepts ciphertext, the number of rows and columns in the matrix, and the path as parameters, and returns plaintext.
It is worth noting that:
 The Route Cipher is based on path transposition.
 It uses a matrix (usually a square matrix) to represent the plaintext, and then selects characters for encryption by following a predetermined path in the matrix.
 Encryption paths can be straight lines, curves, or any other shape, with each path representing a key.
 During decryption, knowledge of the same path is required to reverse the operation.
Vigenère Cipher
The Vigenère Cipher is a polyalphabetic substitution cipher and is one of the most famous ciphers in classical cryptography. It encrypts plaintext using a keyword, employing the concept of multiple Caesar ciphers to achieve a more complex substitution encryption.
Core Idea: The core idea of the Vigenère Cipher is to encrypt plaintext using a keyword (key). Each letter in the keyword corresponds to a shift number in the alphabet, forming a set of Caesar ciphers. Then, for each letter in the plaintext, a shift is performed using the corresponding shift number from the keyword to obtain the ciphertext.
Algorithm Steps:
 Choose a keyword as the key.
 Repeat the keyword until it matches the length of the plaintext.
 For encryption, shift each letter in the plaintext by the corresponding letter of the keyword at the same position to obtain the ciphertext.
 For decryption, use the same keyword to shift each letter in the ciphertext by the corresponding letter of the keyword at the same position in reverse to obtain the plaintext.
The Vigenère Cipher is a polyalphabetic substitution cipher that achieves encryption and decryption by using a keyword and the concept of multiple Caesar ciphers. Compared to a single Caesar cipher, the Vigenère Cipher provides a more complex encryption method, making decryption more difficult.
TypeScript Implementation:
In this example, the vigenereCipherEncrypt
function encrypts plaintext, and the vigenereCipherDecrypt
function decrypts ciphertext. These functions use a keyword to encrypt plaintext and decrypt ciphertext, employing the concept of the Vigenère Cipher.
The Vigenère Cipher has been widely used historically and still holds value in modern cryptography. While it can be cracked using modern cryptographic analysis techniques, it still provides a certain level of protection under certain conditions.
Baconian Cipher
The Baconian cipher is an ancient cryptographic technique named after the 16thcentury English philosopher, Francis Bacon. This cipher utilizes the form of letters to encrypt textual information into a series of different symbols.
In the Baconian cipher, typically two different symbols, often represented as "A" and "B" (or "a" and "b"), are used to represent different letters in the plaintext. For example, "A" might represent the letter "A" in the plaintext, while "B" might represent other letters. Through this method, textual information can be transformed into a sequence of "A" and "B" characters, thus achieving encryption.
Below are the algorithm steps for the Baconian cipher:

Firstly, create a Baconian cipher alphabet, which includes the mapping relationship between plaintext letters and their corresponding Baconian cipher symbols. Typically, two symbols, A and B, are used to represent letters.

Convert the plaintext to uppercase and remove any characters other than letters.

Iterate over each letter in the plaintext:
 For each letter, find its corresponding Baconian cipher symbol in the alphabet.
 Concatenate the found Baconian cipher symbols to form the ciphertext.
Here's a TypeScript implementation:
This TypeScript code implements the encryption and decryption functionality of the Baconian cipher. You can pass the plaintext to the BaconCipher.encrypt()
method for encryption or pass the ciphertext to the BaconCipher.decrypt()
method for decryption.
Affine Cipher
The Affine cipher is an ancient substitution cipher that combines the concepts of the Caesar cipher and linear algebra. It encrypts each letter in the plaintext by using two values called the encryption key (a) and the offset key (b).
The encryption formula for the Affine cipher is $E(x) = (ax + b) \mod m$, where:
 $x$ is the numerical representation of the plaintext letter in the alphabet (usually starting from 0, where 'a' is 0, 'b' is 1, and so on).
 $a$ and $b$ are the encryption keys.
 $m$ is the size of the alphabet (usually 26 for English).
To decrypt, the modular multiplicative inverse of the encryption key is used. The encryption key $a$ must be coprime with the alphabet size $m$ to ensure that each letter can be correctly encrypted and decrypted.
Below are the algorithm steps for the Affine cipher:
 Choose appropriate encryption key $a$ and offset key $b$.
 Convert each letter in the plaintext to its numerical representation $x$ in the alphabet.
 Apply the Affine encryption formula to each numerical value $x$ to obtain the corresponding ciphertext value.
 Convert the ciphertext numerical values back to letter representations in the alphabet.
Here's a TypeScript implementation:
This TypeScript code implements the encryption and decryption functionality of the Affine cipher. You can pass the plaintext to the AffineCipher.encrypt()
method for encryption or pass the ciphertext to the AffineCipher.decrypt()
method for decryption.
凡有所学，皆成性格