the affine cipher is a type of monoalphabetic substitution cipher, wherein 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 (ax+b)\mod(26), where b is the magnitude of the shift. ==description== in the affine cipher the letters of an alphabet of size m are first mapped to the integers in the range 0 .. m-1. it then uses modular arithmetic 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 :\mbox{e}(x)=(ax+b)\mod{m}, where modulus m is the size of the alphabet and a and b are the key of the cipher. the value a must be chosen such that a and m are coprime. the decryption function is :\mbox{d}(x)=a^{-1}(x-b)\mod{m}, where a^{-1} is the modular multiplicative inverse of a modulo m. i.e., it satisfies the equation :1 = a a^{-1}\mod{m}. the multiplicative inverse of a only exists if a and m are coprime. hence without the restriction on a decryption might not be possible. it can be shown as follows that decryption function is the inverse of the encryption function, : \begin{align} \mbox{d}(\mbox{e}(x)) &= a^{-1}(\mbox{e}(x)-b)\mod{m}\\ &= a^{-1}(((ax+b)\mod{m})-b)\mod{m} \\ &= a^{-1}(ax+b-b)\mod{m} \\ &= a^{-1}ax \mod{m}\\ &= x\mod{m}. \end{align} ==weaknesses== since the affine cipher is still a monoalphabetic substitution cipher, it inherits the weaknesses of that class of ciphers. the affine cipher is a caesar cipher when a=1 since the encrypting function simply reduces to a linear shift. considering the specific case of encrypting messages in english (i.e. m=26), 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 with 26 that are less than 26 (these are the possible values of a). each value of a can have 26 different addition shifts (the b 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. 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. since we know a and m are relatively prime this can be used to rapidly discard many "false" keys in an automated system. the same type of transformati
Classical_ciphers