Encrypting with an Affine Cipher

In an affine cipher, the letters of the original message are first identified with integer values (A=0, B=1, C=2, D=3, ... Z=25). These values are then used as inputs to a function of the following form:
$$f(x) = (ax + b) \,\%\, 26$$

The $\%$ symbol above refers to the "mod" operator and returns the integer remainder of a given division. For example,

$$25 \, \% \, 7 = 4 \quad \quad \textrm{ as } \quad\quad 25 = 3 \cdot 7 + \fbox{4} \leftarrow \textrm{ remainder }$$

Suppose we wished to encrypt the plaintext message "HELLO" with an affine cipher using the function $f(x) = 3x + 7$.

  1. "HELLO" would be first turned into the sequence of numbers 7, 4, 11, 11, 14.
  2. These numbers would be fed into $f(x)$ one at a time to produce the sequence 2, 19, 14, 14, 23.
  3. Finally, we interpret the resultant numbers again as letters to produce the ciphertext "CTOOX".

It should be noted that the only values of $a$ that will produce uniquely decryptable ciphertext are those that share no common factors with $26$. For example, if $f(x) = 2x+1$, then 'B' gets encrypted as a 'D', but an 'O' also gets encrypted as a 'D'. So, upon seeing a 'D' in the ciphertext, one can't decide which letter, 'B' or 'O' was originally intended.

Write a class named AffineEncryptChar which takes three arguments, the values of $a$, $b$ mentioned above and a single letter to encrypt. The output should be the encrypted version of the given letter using the given values for $a$ and $b$.

Sample runs:

$ java AffineEncryptChar 7 5 x
k
$ java AffineEncryptChar 17 2 p
x