You are on page 1of 2

University of Ljubljana, Faculty of Electrical Engineering

Master’s study programme in Electrical Enginnering (level 2)


Biomedical Engineering - Biomedical Informatics (64209) - Laboratory practice
Web page: https://e.fe.uni-lj.si

Lab Work Defence - Assignment 4.2

Assignment
One of the encryption algorithms that is based on the substitution of symbols is known as
the “affine cipher”, which first encrypts the consecutive number of each symbol according to a
selected simple mathematical function (e.g. a first order polynomial) and then converts it back
to a symbol. Encryption is therefore performed by:

y = E(x) = (ax + b) mod N,

where a and b are the parameters of the first order polynomial, N is the length of the cryp-
toalphabet (all symbols that are available in the cryptographic scheme), x is the consecutive
number of the plaintext symbol in the cryptoalphabet (0 ≤ x <N ), y = E(x) is the consecutive
number of the encrypted symbol in the cryptoalphabet (0 ≤ y <N ), and mod represents the
modulo operation as the remainder after integer division. Decryption is performed by:

x = D(y) = m(y − b) mod N,

where m is the least integer (m ≥ 0) that satisfies the equality (am) mod N = 1.
The cryptoalphabet has to be equal for encryption and decryption, as for example the following
that is composed of the uppercase letters of the English alphabet, the white space, and the full
stop and comma punctuation marks:

Alphabet A B C D E F G H I J K L M N O P Q R S T U V W X Y Z . ,
Symbol no. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

For example, plaintext ’ALPHABET’ is according to the given cryptoalphabet (N = 29), and pa-
rameters a = 7 and b = 11 of the affine cipher encrypted into ciphertext ’LBACLSK,’:

Plaintext A L P H A B E T
Symbol no. 0 11 15 7 0 1 4 19 ← x
Symbol no. 11 1 0 2 11 18 10 28 ← y = (7x + 11) mod 29
Ciphertext L B A C L S K ,

1. Write a function for the affine encryption:

function oText = affineEncryption(iText, iA, iB, iAlphabet),

where the input argument iText represents the plaintext, iA = a and iB = b are the para-
meters of the affine cipher, and iAlphabet is the cryptoalphabet. The output argument
oText represents the ciphertext.
Apply the function to plaintext ’BIOMEDICAL INFORMATICS’ by using a = 5 and b = 8, and
the above listed cryptoalphabet.
2. Write a function for the affine decryption:

function oText = affineDecryption(iText, iA, iB, iAlphabet),

where the input argument iText represents the ciphertext, iA = a and iB = b are respecti-
vely the parameters of the affine cipher, and iAlphabet is the cryptoalphabet. The output
argument oText represents the plaintext.
Apply the function to the ciphertext obtained in assignment 1 by using the same affine
cipher parameters and the same cryptoalphabet.

3. Because the selected symbol is always encrypted into the same symbol and because the
affine cipher is defined by two parameters (i.e. a and b), the affine cipher can be “broken”
by knowing the encryption of only two symbols. For the given example, we know that
’L’ = E(’A’) or 11 = E(0), and that ’B’ = E(’L’) or 1 = E(11). Write a function for finding
the parameters of the affine cipher:

function [oA, oB] = breakAffineCipher(iText1, iText2, iAlphabet),

where the input argument iText1 represents the plaintext, iText2 is the ciphertext, and
iAlphabet is the cryptoalphabet. The output parameters oA = a and oB = b represent the
parameters of the affine cipher.
Parameter a is defined as the least integer (a ≥ 0) that satisfies the equality
a(x1 − x2 ) − (y1 − y2 ) mod N = 0, while parameter  b is defined as the least in-
teger (b ≥ 0) that satisfies the equality ax1 + b − y1 mod N = 0. Variables x1 and x2
represent the consecutive numbers of two arbitrary plaintext symbols in the cryptoalpha-
bet, while variables y1 = E(x1 ) and y2 = E(x2 ) are the consecutive encrypted numbers of
the same two symbols in the cryptoalphabet.
Apply the function to plaintext ’ALPHABET’ and ciphertext ’LBACLSK,’ from the given
encryption example, and for the given cryptoalphabet.

4. The affine encryption and decryption can be made faster in the case the length of the
plaintext or ciphertext is larger than N . In this case we can encrypt once the whole
cryptoalphabet (therefore N symbols), and then perform encryption or decryption by
comparing the symbols in the original and encrypted alphabet. Write a function for such
affine encryption and decryption:

function oText = fastAffineCipher(iText, iA, iB, iAlphabet, iMode),

where the input argument iText represents the plaintext or the ciphertext, iA = a and
iB = b are the parameters of the affine cipher, iAlphabet is the cryptoalphabet, and iMode
is the cipher mode (’encrypt’ for encryption, ’decrypt’ for decryption). The output ar-
gument oText represents the ciphertext or the plaintext.
Apply the function to the ciphertext that is stored in file cipherText.txt on the online clas-
sroom (https://e.fe.uni-lj.si) by using the same affine cipher parameters and the same
cryptoalphabet as in assignment 1.

You might also like