Affine cipher
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.[src] 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.[src] As such, it has the weaknesses of all substitution ciphers.[src] Each letter is enciphered with the function , where is the magnitude of the shift.[src]
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.[src] 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.[src] As such, it has the weaknesses of all substitution ciphers.[src] Each letter is enciphered with the function , where is the magnitude of the shift.[src] ==Description== Here, the letters of an alphabet of size are first mapped to the integers in the range .[src] It then uses modular arithmetic to transform the integer that each plaintext letter corresponds to into another integer that correspond to a ciphertext letter.[src] The encryption function for a single letter is E(x)=(ax+b)\bmod{m} where modulus is the size of the alphabet and and are the keys of the cipher.[src] The value must be chosen such that and are coprime.[src] The decryption function is D(x)=a^{-1}(x-b)\bmod{m} where is the modular multiplicative inverse of modulo .[src] I.e., it satisfies the equation 1 = a a^{-1}\bmod{m} The multiplicative inverse of only exists if and are coprime.[src] Hence without the restriction on , decryption might not be possible.[src] It can be shown as follows that decryption function is the inverse of the encryption function, \begin{align} D(E(x)) &= a^{-1}(E(x)-b)\bmod{m}\\ &= a^{-1}(((ax+b)\bmod{m})-b)\bmod{m} \\ &= a^{-1}(ax+b-b)\bmod{m} \\ &= a^{-1}ax \bmod{m}\\ &= x\bmod{m} \end{align} ==Weaknesses== Since the affine cipher is still a monoalphabetic substitution cipher, it inherits the weaknesses of that class of ciphers.[src] The Caesar cipher is an Affine cipher with since the encrypting function simply reduces to a linear shift.[src] The Atbash cipher uses .[src] Considering the specific case of encrypting messages in English (i.e.[src] ), there are a total of 286 non-trivial affine ciphers, not counting the 26 trivial Caesar ciphers.[src] This number comes from the fact there are 12 numbers that are coprime with 26 that are less than 26 (these are the possible values of ).[src] Each value of can have 26 different addition shifts (the value); therefore, there are 12 × 26 or 312 possible keys.[src] The 12 coprime numbers are: 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, and 25.[src] This lack of variety renders the system as highly insecure when considered in light of Kerckhoffs' Principle.[src] The cipher's primary weakness comes from the fact that if the cryptanalyst can discover (by means of frequency analysis, brute force, guessing or otherwise) the plaintext of two ciphertext characters then the key can be obtained by solving a simultaneous equation.[src] Since we know and are relatively prime this can be used to rapidly discard many "false" keys in an automated system.[src] The same type of transformation used in affine ciphers is used in linear congruential generators, a type of pseudorandom number generator.[src] This generator is not a cryptographically secure pseudorandom number generator for the same reason that the affine cipher is not secure.[src] ==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.[src] ===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.[src] In this example, the one-to-one map would be the following: ===Programming examples=== The following Python 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 = {v: k for k, v in D.items()} size = len(string.ascii_lowercase) ret = "" print(size) for c in s: N = E[c] val = a * N + b val = val % size print(f"{c}({N}) -> {D[val]}({val})") ret += D[val] return ret affine(7, 3, "foobar") ==References==[src]
31 sentences · 31 cited inline · 0 references stored
References
No reference rows published for this article.
Cite this article
MLA
"Affine cipher." encyclopegia.org, 2025, https://encyclopegia.org/wiki/affine-cipher. Accessed 29 November 2026.
Chicago
"Affine cipher." encyclopegia.org. Accessed November 29, 2026. https://encyclopegia.org/wiki/affine-cipher.
APA
encyclopegia.org. (2025). Affine cipher. Retrieved November 29, 2026, from https://encyclopegia.org/wiki/affine-cipher
Accessed November 29, 2026 · CC BY-SA 4.0 · citation page · JSON