Search for a tool
Affine Cipher

Tool to decrypt/encrypt with Affine cipher, an encryption function with additions and multiplication that codes a letter into another with value (ax + b) modulo 26.

Results

Affine Cipher -

Tag(s) : Substitution Cipher

Share
Share
dCode and more

dCode is free and its tools are a valuable help in games, maths, geocaching, puzzles and problems to solve every day!
A suggestion ? a feedback ? a bug ? an idea ? Write to dCode!


Please, check our dCode Discord community for help requests!
NB: for encrypted messages, test our automatic cipher identifier!


Feedback and suggestions are welcome so that dCode offers the best 'Affine Cipher' tool for free! Thank you!

Affine Cipher

Affine Decoder

 



Manual Parameters and Options







Affine Encoder

 








Answers to Questions (FAQ)

What is the Affine cipher? (Definition)

Affine cipher is the name given to a substitution cipher whose key consists of 2 coefficients A and B constituting the parameters of a mathematical linear function $ f=Ax+B $ (called affine).

How to encrypt using the Affine cipher?

Encryption uses a classic alphabet, and two integers, called coefficients or keys A and B, these are the parameters of the affine function Ax+B (which is a straight line/linear equation).

Example: Encrypt DCODE with the keys A=5, B=3 and the English/latin alphabet ABCDEFGHIJKLMNOPQRSTUVWXYZ.

For each letter of the alphabet is associated to the value of its position in the alphabet (starting at 0).

Example: By default, A=0, B=1, …, Z=25, it is possible (but not recommended) to use A=1, …, Y=25, Z=0 using the alphabet ZABCDEFGHIJKLMNOPQRSTUVWXY.

For each letter of value $ x $ of the plain text, is associated a value $ y $, result of the affine function $ y = A \times x + B \mod 26 $ (with $ 26 $ the alphabet size). Each computed value $ y $ corresponds to a letter with the same position in the alphabet, it is the ciphered letter. The Affine ciphertext is the replacement of all the letters by the new ones.

Example: DCODE is crypted SNVSX

Plain letter$ x $$ y $Cipher letter
D3$ 5 \times 3 + 3 = 18 $S
O14$ 5 \times 14 + 3 = 73 = 21 \mod 26 $V

How to decrypt the Affine cipher?

Affine decryption requires to know the two keys A and B (the one from encryption) and the used alphabet.

Example: Decrypt the ciphered message SNVSX with keys A=5 and B=3

For each letter of the alphabet, associate the value of its position in the alphabet.

Example: The alphabet ABCDEFGHIJKLMNOPQRSTUVWXYZ, starting at 0 gives A=0, B=1, …, Z=25.

Each letter of value $ y $ of the message corresponds to a value $ x $, result of the inverse function $ x = A' \times (y-B) \mod 26 $ (with $ 26 $ the alphabet size). If the value obtained is negative, add 26.

The value $ A' $ is an integer such as $ A \times A' = 1 \mod 26 $ (with $ 26 $ the alphabet size). To find $ A' $, calculate its modular inverse.

Example: A coefficient $ A' $ for $ A = 5 $ with an alphabet size of $ 26 $ is $ 21 $ because $ 5 \times 21 = 105 \equiv 1 \mod 26 $.
For S ( $ y = 18 $ ), $ x = A' \times (18-B) = 21 \times (18-3) \equiv 315 \mod 26 = 3 $

For each value $ x $, associate the letter with the same position in the alphabet: the coded letter. The plain text is the replacement of all characters with calculated new letters.

Example: For S ( $ x = 3 $ ) associate the letter at position 3: D, etc. The original plain text is DCODE.

How to recognize an Affine ciphertext?

A message encrypted by Affine has a coincidence index close to the plain text language's one.

Any reference to an affine function (in a straight line), a graph, an abscissa or an ordinate is a clue (the function $ f(x) = ax + b $ can be represented in an orthonormal coordinate system like a classical affine function, it is therefore possible from a graph to find the slope coefficient $ a $ and the y-intercept $ b $).

What are Affine cipher variants?

The affine ciphers in fact group together several ciphers which are special cases:

— The multiplicative cipher is a special case of the Affine cipher where B is 0.

— The Caesar cipher is a special case of the Affine cipher where A is 1 and B is the shift/offest.

The affine cipher is itself a special case of the Hill cipher, which uses an invertible matrix, rather than a straight-line equation, to generate the substitution alphabet.

How to decipher Affine without coefficients A and B?

To crack Affine, it is possible to bruteforce/test all values for A and B coefficients. Use the Brute-force attack button.

If the alphabet is 26 characters long, then A coefficient has only 12 possible values, and B has 26 values, so there are only 312 test to try.

How to compute the decryption function?

For an affine encryption with the function $ y = A x + B $, then the reciprocal/inverse decryption function is expressed $ y' = A' x + B $

How to compute A' value?

Calculate the modular inverse of A, modulo the length of the alphabet (see below for pre-calculated values).

How to compute B' value?

B' has the same value as B, for this reason, this variable should not be called B' but B.

What are the A' values?

The value of A' depends on A but also on the alphabet's length, if it is a classic one, it is 26 characters long. The values of A' are then:

A = 1A' = 1
A = 3A' = 9
A = 5A' = 21
A = 7A' = 15
A = 9A' = 3
A = 11A' = 19
A = 15A' = 7
A = 17A' = 23
A = 19A' = 11
A = 21A' = 5
A = 23A' = 17
A = 25A' = 25

Why is there a constraint on the value of A?

The Bezout's theorem indicates that A' only exists if A and 26 (alphabet length) are coprime. This limits A values to 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23 and 25 (if the alphabet is 26 characters long)

Is it possible to use a key A not coprime with 26?

Yes, but an automatic decryption process becomes impossible, a single ciphered letter will have multiple plain letters possible.

Does a negative value for A exist?

Yes, but there exists a positive corresponding value, a value of A = -1 is equal to a value of A = 25 (because 25 = -1 mod 26).

Is there a limitation on B value?

No, B can take any value.

All the values of B modulo 26 (length of the alphabet) are equivalent. So if B is negative, there is an equivalent positive value of B.

Example: 'B = -1' is equivalent to 'B = 25' (modulo 26)

Why is this encryption so called affine?

In mathematics, an affine function is defined by addition and multiplication of the variable (often $ x $) and written $ f(x) = ax + b $. The affine cipher is similar to the $ f $ function as it uses the values $ a $ and $ b $ as a coefficient and the variable $ x $ is the letter to be encrypted.

When was Affine invented?

Date and author are unknown, the affine cipher.

Source code

dCode retains ownership of the "Affine Cipher" source code. Except explicit open source licence (indicated Creative Commons / free), the "Affine Cipher" algorithm, the applet or snippet (converter, solver, encryption / decryption, encoding / decoding, ciphering / deciphering, breaker, translator), or the "Affine Cipher" functions (calculate, convert, solve, decrypt / encrypt, decipher / cipher, decode / encode, translate) written in any informatic language (Python, Java, PHP, C#, Javascript, Matlab, etc.) and all data download, script, or API access for "Affine Cipher" are not public, same for offline use on PC, mobile, tablet, iPhone or Android app!
Reminder : dCode is free to use.

Cite dCode

The copy-paste of the page "Affine Cipher" or any of its results, is allowed (even for commercial purposes) as long as you credit dCode!
Exporting results as a .csv or .txt file is free by clicking on the export icon
Cite as source (bibliography):
Affine Cipher on dCode.fr [online website], retrieved on 2024-12-04, https://www.dcode.fr/affine-cipher

Need Help ?

Please, check our dCode Discord community for help requests!
NB: for encrypted messages, test our automatic cipher identifier!

Questions / Comments

Feedback and suggestions are welcome so that dCode offers the best 'Affine Cipher' tool for free! Thank you!


https://www.dcode.fr/affine-cipher
© 2024 dCode — The ultimate 'toolkit' to solve every games / riddles / geocaching / CTF.
 
Feedback