The affine cipher is a type of
monoalphabetic substitution cipher, where each letter in an alphabet is mapped to its numeric equivalent, encrypted using a simple mathematical function, and converted back to a letter. The formula used means that each letter encrypts to one other letter, and back again, meaning the cipher is essentially a standard substitution cipher with a rule governing which letter goes to which. As such, it has the weaknesses of all substitution ciphers. Each letter is enciphered with the function , where is the magnitude of the shift.
Description
Here, the letters of an alphabet of size are first mapped to the integers in the range . It then uses
modular arithmetic
In mathematics, modular arithmetic is a system of arithmetic operations for integers, other than the usual ones from elementary arithmetic, where numbers "wrap around" when reaching a certain value, called the modulus. The modern approach to mo ...
to transform the integer that each plaintext letter corresponds to into another integer that correspond to a ciphertext letter.
The encryption function for a single letter is
:
where modulus is the size of the alphabet and and are the keys of the cipher. The value must be chosen such that and are
coprime
In number theory, two integers and are coprime, relatively prime or mutually prime if the only positive integer that is a divisor of both of them is 1. Consequently, any prime number that divides does not divide , and vice versa. This is equiv ...
. The decryption function is
:
where is the
modular multiplicative inverse
In mathematics, particularly in the area of arithmetic, a modular multiplicative inverse of an integer is an integer such that the product is congruent to 1 with respect to the modulus .. In the standard notation of modular arithmetic this cong ...
of
modulo
In computing and mathematics, the modulo operation returns the remainder or signed remainder of a division, after one number is divided by another, the latter being called the '' modulus'' of the operation.
Given two positive numbers and , mo ...
. I.e., it satisfies the equation
:
The multiplicative inverse of only exists if and are coprime. Hence without the restriction on , decryption might not be possible.
It can be shown as follows that decryption function is the inverse of the encryption function,
:
Weaknesses
Since the affine cipher is still a monoalphabetic substitution cipher, it inherits the weaknesses of that class of ciphers. The
Caesar cipher
In cryptography, a Caesar cipher, also known as Caesar's cipher, the shift cipher, Caesar's code, or Caesar shift, is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in t ...
is an Affine cipher with since the encrypting function simply reduces to a linear shift. The
Atbash cipher uses .
Considering the specific case of encrypting messages in English (i.e. ), there are a total of 286 non-trivial affine ciphers, not counting the 26 trivial Caesar ciphers. This number comes from the fact there are 12 numbers that are
coprime
In number theory, two integers and are coprime, relatively prime or mutually prime if the only positive integer that is a divisor of both of them is 1. Consequently, any prime number that divides does not divide , and vice versa. This is equiv ...
with 26 that are less than 26 (these are the possible values of ). Each value of can have 26 different addition shifts (the value); therefore, there are 12 × 26 or 312 possible keys. This lack of variety renders the system as highly insecure when considered in light of
Kerckhoffs' Principle
Kerckhoffs's principle (also called Kerckhoffs's desideratum, assumption, axiom, doctrine or law) of cryptography was stated by the Dutch cryptographer Auguste Kerckhoffs in the 19th century. The principle holds that a cryptosystem should be sec ...
.
The cipher's primary weakness comes from the fact that if the cryptanalyst can discover (by means of
frequency analysis
In cryptanalysis, frequency analysis (also known as counting letters) is the study of the frequency of letters or groups of letters in a ciphertext. The method is used as an aid to breaking classical ciphers.
Frequency analysis is based on th ...
,
brute force, guessing or otherwise) the plaintext of two ciphertext characters then the key can be obtained by solving a
simultaneous equation
In mathematics, a set of simultaneous equations, also known as a system of equations or an equation system, is a finite set of equations for which common solutions are sought. An equation system is usually classified in the same manner as single e ...
. Since we know and are relatively prime this can be used to rapidly discard many "false" keys in an automated system.
The same type of transformation used in affine ciphers is used in
linear congruential generator
A linear congruential generator (LCG) is an algorithm that yields a sequence of pseudo-randomized numbers calculated with a discontinuous piecewise linear equation. The method represents one of the oldest and best-known pseudorandom number gener ...
s, a type of
pseudorandom number generator
A pseudorandom number generator (PRNG), also known as a deterministic random bit generator (DRBG), is an algorithm for generating a sequence of numbers whose properties approximate the properties of sequences of random number generation, random n ...
. This generator is not a
cryptographically secure pseudorandom number generator
A cryptographically secure pseudorandom number generator (CSPRNG) or cryptographic pseudorandom number generator (CPRNG) is a pseudorandom number generator (PRNG) with properties that make it suitable for use in cryptography. It is also referred t ...
for the same reason that the affine cipher is not secure.
Example
In this example showing encryption and decryption, the alphabet is going to be the letters A through Z, and will have the corresponding values found in the following table.
Encryption
In this encrypting example,
the plaintext to be encrypted is "AFFINE CIPHER" using the table mentioned above for the numeric values of each letter, taking to be 5, to be 8, and to be 26 since there are 26 characters in the alphabet being used. Only the value of has a restriction since it has to be coprime with 26. The possible values that could be are 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, and 25. The value for can be arbitrary as long as does not equal 1 since this is the shift of the cipher. Thus, the encryption function for this example will be . The first step in encrypting the message is to write the numeric values of each letter.
Now, take each value of , and solve the first part of the equation, . After finding the value of for each character, take the remainder when dividing the result of by 26. The following table shows the first four steps of the encrypting process.
The final step in encrypting the message is to look up each numeric value in the table for the corresponding letters. In this example, the encrypted text would be IHHWVCSWFRCP. The table below shows the completed table for encrypting a message in the Affine cipher.
Decryption
In this decryption example, the ciphertext that will be decrypted is the ciphertext from the encryption example. The corresponding decryption function is , where is calculated to be 21, and is 8. To begin, write the numeric equivalents to each letter in the ciphertext, as shown in the table below.
Now, the next step is to compute , and then take the remainder when that result is divided by 26. The following table shows the results of both computations.
The final step in decrypting the ciphertext is to use the table to convert numeric values back into letters. The plaintext in this decryption is AFFINECIPHER. Below is the table with the final step completed.
Entire alphabet encoded
To make encrypting and decrypting quicker, the entire alphabet can be encrypted to create a one-to-one map between the letters of the cleartext and the ciphertext. In this example, the one-to-one map would be the following:
Programming examples
The following
Python
Python may refer to:
Snakes
* Pythonidae, a family of nonvenomous snakes found in Africa, Asia, and Australia
** ''Python'' (genus), a genus of Pythonidae found in Africa and Asia
* Python (mythology), a mythical serpent
Computing
* Python (prog ...
code encrypts text with the affine cipher:
import string
def affine(a: int, b: int, s: str) -> str:
"""Prints a transposition table for an affine cipher."""
D = dict(enumerate(string.ascii_lowercase, start=0))
E =
size = len(string.ascii_lowercase)
ret = ""
print(size)
for c in s:
N = E val = a * N + b
val = val % size
print(f"() -> ()")
ret += D al return ret
affine(7, 3, "foobar")
See also
*
Affine function
In Euclidean geometry, an affine transformation or affinity (from the Latin, ''wikt:affine, affinis'', "connected with") is a geometric transformation that preserves line (geometry), lines and parallel (geometry), parallelism, but not necessarily ...
s
*
Atbash code
*
Caesar cipher
In cryptography, a Caesar cipher, also known as Caesar's cipher, the shift cipher, Caesar's code, or Caesar shift, is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in t ...
*
ROT13
ROT13 is a simple letter substitution cipher that replaces a letter with the 13th letter after it in the Latin alphabet.
ROT13 is a special case of the Caesar cipher which was developed in ancient Rome, used by Julius Caesar in the 1st centur ...
*
Topics in cryptography
The following outline is provided as an overview of and topical guide to cryptography:
Cryptography (or cryptology) – practice and study of hiding information. Modern cryptography intersects the disciplines of mathematics, computer scie ...
References
{{DEFAULTSORT:Affine Cipher
Articles with example Python (programming language) code
Classical ciphers