## Affine-Shift Cipher

### December 15, 2009

The affine-shift cipher is simple but cryptographically weak. It works by mapping the twenty-six letters of the alphabet onto the integers 0 through 25, then applying the function (*ax* + *b*) mod 26 to each character in turn, finally mapping back from integers to letters. The key is formed by the numbers *a* and *b*. Decryption is the inverse operation, a^{-1}(*x* − *b*) mod 26, where a^{-1} is the modular inverse of *a* modulo *m*; this means that *a* and *m* must be coprime. For instance, the plain-text PROGRAMMINGPRAXIS is enciphered with *a*=5 and *b*=8 as FPAMPIQQWVMFPITWU, and deciphered with 21 as the modular inverse.

Your task is to write functions to encipher and decipher messages using the affine-shift cipher. When you are finished, you are welcome to read or run a suggested solution, or to post your solution or discuss the exercise in the comments below.

Pages: 1 2

A solution in Python

[...] Praxis – Affine-Shift Cipher By Remco Niemeijer In today’s Programming Praxis we have to implement the Affine-Shift Cipher. Let’s get going, shall [...]

My Haskell solution (see http://bonsaicode.wordpress.com/2009/12/15/programming-praxis-affine-shift-cipher/ for a version with comments):

;; My CL is rusty, so design may be over suboptimal

;; But, the hell, it works :)

(defun str->ints (str)

(loop for c across str

collect (- (char-code (char-upcase c)) 65)))

(defun ints->str (ints)

(concatenate ‘string

(loop for i in ints

collect (code-char (+ i 65)))))

(defun inv (n m)

(loop for i from 0 to m

when (= 1 (mod (* n i) m))

do (return i)))

(defun cypher (str a b)

(let* ((li (str->ints str))

(li2 (mapcar

(lambda (x) (mod (+ (* a x) b) 26))

li)))

(ints->str li2)))

(defun decypher (str a b)

(let* ((li (str->ints str))

(li2 (mapcar

(lambda (x) (mod (* (inv a 26) (- x b)) 26))

li)))

(ints->str li2)))

(defvar a 12)

(defvar b 23)

(decypher (cypher “programmingpraxis” a b) a b)

@ Remko: I’m afraid your notion of “inverse” is wrong.

21 happens to be 5’s inverse mod 26 as well as 26-5, but we’re looking for a^-1 so that a*a^-1=1 [26]…

Duh, with a = 12, my code is never going to work, since it’s got no inverse… Rather use, say, 11…

Thanks to Axioplase for spotting the error in my algorithm. Here’s the correct version: