Encryption & Decryption
Tanzillah Wahid
TERMINOLOGY
• Plaintext: Message or data which are in their normal, readable (not
encrypted) form.
• Encryption: Encoding the contents of the message in such a way that
hides its contents from outsiders.
• Ciphertext: The encrypted message.
• Decryption: The process of retrieving the plaintext back from the
ciphertext.
• Key: Encryption and decryption usually make use of a key, and the
coding method is such that decryption can be performed only by
knowing the proper key.
TERMINOLOGY
• Cryptography is the art or science of keeping messages secret. It deals
with all aspects of secure messaging, authentication, digital
signatures, electronic money, and other applications.
• Cryptosystems: A cryptographic system (cryptosystem) consists of a
pair of data transformations, namely encryption and decryption.
• Cryptanalysis: The art of breaking ciphers, i.e. retrieving the plaintext
without knowing the proper key.
• Cryptographers: People who do cryptography.
• Cryptanalysts: practitioners of cryptanalysis .
Conventional Cryptosystem Principles
• An cryptosystem has the following five ingredients:
– Plaintext
– Encryption algorithm
– Secret Key
– Ciphertext
– Decryption algorithm
• Security depends on the secrecy of the key, not the secrecy of the
algorithm.
Classifications
• Classification of cryptosystems
– Symmetric cryptosystems
– Asymmetric cryptosystems
Symmetric Cryptosystem
• The same key is used for both encryption and decryption purposes
Ciphertext C = Ek(M)
Encryption Decryption
Plaintext P Plaintext P
process E Process D
Key(K)
Asymmetric Cryptosystem
Different keys are used for both encryption and decryption purposes.
Ciphertext C = Ek(M)
Encryption Decryption
Plaintext P Plaintext P (M)
process E Process D
(M)
Encryption Key(K) Decryption Key (K)
Traditional Symmetric- Key Ciphers
• Traditional symmetric-key ciphers can be classified into two
broad categories:
1. Substitution cipher:
A substitution cipher replaces one symbol with another. For example, we can replace letter A with letter
D, and letter T with letter Z. If the symbols are digits, we can replace 3 with 7, 2 with 6.
Substitution ciphers can be categorized as either monoalphabetic ciphers or polyalphabetic ciphers.
2. Transposition cipher:
A transposition cipher does not substitute one symbol for another, instead it changes the location of the
symbols.
A symbol in the first position of the plaintext may appear in the ninth position of the ciphertext. A symbol
in the eighth position of the plaintext may appear in the first position of the ciphertext. For example, the
plaintext characters “hello” may be encrypted as “elhol”.
There are three types of transposition cipher:
❑ Keyless Transposition Ciphers
❑ Keyed Transposition Ciphers
❑ Keyed Columnar Transposition Ciphers or Columnar Transposition Ciphers
Traditional Symmetric- Key Ciphers
• Monoalphabetic Ciphers
In monoalphabetic substitution, the relationship between a symbol in the plaintext to a symbol
in the ciphertext is always one-to-one.
That is, a character or symbol in the plaintext is always changed to the same character or symbol
in the ciphertext regardless of its position in the text.
For example, if letter A in the plaintext is changed to letter D, every letter A is changed to letter
D.
Additive cipher, Caesar cipher, multiplicative cipher, affine cipher etc. are some examples of
monoalphabetic ciphers.
• Example
• Example
The following shows a plaintext and its corresponding ciphertext. The cipher is
probably monoalphabetic because both l’s (els) are encrypted as O’s.
Plaintext: hello Ciphertext: KHOOR
Traditional Symmetric- Key Ciphers
• Polyalphabetic Ciphers
In polyalphabetic substitution, each occurrence of a character may have a different substitute.
The relationship between a character in the plaintext to a character in the ciphertext is
one-to-many.
For example, if letter “a” could be enciphered as “D” in the beginning of the text, but as “N” at the
middle.
Polyalphabetic ciphers have the advantage of hiding the letter frequency of the undelying language.
Eve cannot use the single-letter frequency statistics to break the ciphertext.
Autokey cipher, playfair cipher, vigenere cipher, Hill cipher etc. are some examples of
polyalphabetic ciphers.
Example
• The following shows a plaintext and its corresponding ciphertext. The cipher is polyalphabetic because
each l (el) is encrypted by a different character. The first l (el) is encrypted as N; the second as Z.
• Plaintext: hello Ciphertext: ABNZF
•E
Transposition Cipher
• Keyless Transposition Ciphers
• Simple transposition ciphers and were used in the past.
• There are two methods for permutation of characters:
In the first method, the text is written into a table column by column and then transmitted
row by row.
In the second method, the text is written into a table row by row and then transmitted
column by column.
Keyless Transposition Ciphers
• 1st Method: Written column by column and transmitted row by row
• A good example of a keyless cipher using the first method is the rail fence cipher.
• In this cipher, the plaintext is arranged in two lines as a zigzag pattern (which means column by
column).
• The ciphertext is created reading the pattern row by row. For example, to send the message
“Meet me at the park” to Bob, Alice writes-
• By sending the first row followed by the second row, Alice then creates the ciphertext
“MEMATEAKETETHPR”.
• Bob receives the ciphertext and divides in half (in this example, the second half has one less
character). The first half forms the first row; the second half forms the second row. She reads the
result in zigzag.
• Because there is no key and the number of rows is fixed (2 here), the cryptanalysis of the
ciphertext would be very easy for Eve.
Keyless Transposition Ciphers
• 2nd Method: Written row by row and transmitted column by column
• Alice and Bob can agree on the number of columns and use the second method. Alice
writes the same plaintext, row by row, in a table of four columns.
• By transmitting the characters column by column, Alice then creates the ciphertext
“MMTAEEHREAEKTTP”.
• Bob receives the ciphertext and follows the reverse process. He writes the received
message column by column and reads it row by row as the plaintext.
• Eve can easily decipher the message if she knows the number of columns.
Keyless Transposition Ciphers
• The cipher in the previous example is actually a transposition cipher. The following shows the
permutation of each character in the plaintext into the ciphertext based on the positions.
• Plaintext : m e e t m e a t t h e p a r K
Source Position : 01 02 03 04 05 06 07 08 09 10 11 12 13 14 1
5
Destination Position
: 01 05 09 13 02 06 10 14 03 07 11 15 04 08 1
2
Ciphertext : M M T A E E H R E A E K T T P
The first character in the plaintext has not changed its position. The second character in the
plaintext has moved to the fifth position in the ciphertext; the third character has moved to the
ninth position; and so on.
Although the characters are permuted, there is a pattern in the permutation: (01, 05, 09,
13), (02, 06, 10, 14), (03, 07, 11, 15), and (04, 08, 12). In each section, the difference
between the two adjacent numbers is 4.
Keyed Transposition Ciphers
• The keyless ciphers permute the characters by using writing plaintext in one way (e.g. row by
row) and reading it in another way (e.g. column by column). The permutation is done on the
whole plaintext to create the whole ciphertext.
• In keyed transposition cipher, the plaintext is divided into groups of predetermined size, called
blocks, and then use a key to permute the characters in each block separately.
EXAMPLE
• Alice needs to send the message “Enemy attacks tonight” to Bob.
• They have agreed to divide the text into groups of five characters and then permute the
characters in each group.
• To make the last group the same size, a bogus character z is added at the end.
• The key used for encryption and decryption is a permutation key, which shows how the character
are permuted. For the above message, we assume that Alice and Bob used the following key:
• The third character in the plaintext block becomes the first character in the ciphertext block.
Keyed Transposition Ciphers
• Similarly, the first character in the plaintext block becomes the second character in the
ciphertext block , and so on.
• The permutation yields:
• Alice sends the ciphertext “EEMYNTAACTTKONSHITZG” to Bob. Bob divides the ciphertext into
5-character groups and finds the plaintext using the key in reverse order.
• Reversed order:
Columnar Transposition Ciphers
• This type of transposition cipher combines the keyless and keyed transposition ciphers to achieve
better scrambling.
• Encryption or decryption is done in three steps:
The text is written into a table row by row.
The permutation is done by reordering the columns.
• The new table is read column by column.
• Here, the 1st and 3rd steps provide a keyless global reordering and the 2nd step provides a
blockwise keyed reordering.
EXAMPLE
• Encrypt the message “enemy attacks tonight” using Columnar transposition cipher.
Columnar Transposition Ciphers
• Solution: The encryption and decryption is illustrated in the figure below.
Figure: Combining two approaches
Columnar Transposition Ciphers
• The 1st table in the figure is created by Alice writing the plaintext row
by row.
• The columns are permuted using the key mentioned.
• The ciphertext is created by reading the 2nd table column by column.
• Bob does the same three steps in the reverse order.
Columnar Transposition Ciphers
• The 1st table in the figure is created by Alice writing the plaintext row
by row.
• The columns are permuted using the key mentioned.
• The ciphertext is created by reading the 2nd table column by column.
• Bob does the same three steps in the reverse order.
Additive Cipher
The simplest monoalphabetic cipher is the additive cipher. This cipher is sometimes called a shift
cipher and sometimes a Caesar cipher, but the term additive cipher better reveals its
mathematical nature.
Assume that the plaintext consists of lowercase letters (a to z), and that the ciphertext consists of
uppercase letters (A to Z).
To be able to apply mathematical operations on the plaintext and ciphertext, we assign numerical
values to each letter as shown in the figure below.
Figure: Plaintext and ciphertext in Z26
Each character (uppercase or lowercase) is assigned an integer in Z26. The secret key between
Alice and Bob is also an integer in Z26.
For simplicity, lowercase characters are used as plaintext and uppercase characters are used as
ciphertext.
Additive Cipher
When the cipher is additive, the plaintext, ciphertext, and key are integers in Z26.
The encryption algorithm adds the key to the plaintext character; the decryption algorithm
subtracts the key from the ciphertext character. That is, encryption and decryption are inverse
of each other.
Figure below shows the process of additive cipher.
Figure: Additive cipher
Additive Cipher
When the cipher is additive, the plaintext, ciphertext, and key are integers in Z26.
The encryption algorithm adds the key to the plaintext character; the decryption
algorithm subtracts the key from the ciphertext character. That is, encryption and
decryption are inverse of each other.
Figure below shows the process of additive cipher.
EE FFFE
FFFFFFxaFFFFFFFFF
FFFFFFFFFFFFFFFFFFFFFmple:
Additive Cipher
• Example:
Use the additive cipher with key = 15 to encrypt the message “hello”.
• Solution
We apply the encryption algorithm to the plaintext, character by character:
The result is ‘WTAAD’.
Note that two instances of the same plaintext character (l) are encrypted as the
same character (A). Hence additive cipher is monoalphabetic
Additive Cipher
• Example:
Use the additive cipher with key = 15 to decrypt the message “WTAAD”.
Solution:
We apply the decryption algorithm to the plaintext character by character:
The result is ‘hello’.
Note that the operation is in modulo 26, which means that a negative result
needs to be mapped to Z26. (for example, -15 becomes 11).
Shift and Ceaser Cipher
• Shift Cipher:
Historically, additive ciphers are called shift ciphers. Because, the encryption
algorithm can be interpreted as “shift key character down” and the decryption
algorithm can be interpreted as “shift key character up” .
For example, if the key=15, the encryption algorithm shifts 15 character down. The
decryption algorithm shifts 15 character up.
• Ceaser Cipher:
Additive ciphers are also called Caesar cipher. Because, Julius Caesar used this cipher
to communicate with his officers.
Caesar used a key of 3 for his communications. That is, the cipher involves replacing
each letter of the plaintext with the letter standing three places further down the
alphabet. For example,
• Plaintext :Meet me after the lunch
• Ciphertext:PHHW PH DIWHU WKH OXQFK
Multiplicative Cipher
• In a multiplicative cipher-
The encryption algorithm specifies multiplication of the plaintext by the key.
The decryption algorithm specifies division of the ciphertext by the key. In other words,
decryption algorithm means multiplication of the ciphertext by the multiplicative inverse
of the key.
The plaintext and ciphertext are integers in Z26, but the key is an integer in Z26*.
Encryption and decryption are inverse of each other.
Figure below shows the process of multiplicative cipher.
Figure: Multiplicative Cipher
Multiplicative Cipher
• Example: What is the key domain for any multiplicative cipher?
• Solution:The key needs to be in Z26*. This set has only 12 members: 1, 3, 5, 7, 9,
11, 15, 17, 19, 21, 23, 25.
• We can find the key domain for any multiplicative cipher using Euler’s
Phi-function, sometimes called the Euler’s Totient function.
• Example: Encrypt the message “hello” with a key of 7 using multiplicative cipher.
• Solution:We apply the following encryption algorithm to the plaintext character
by character: C=(Pxk)mod 26
• The ciphertext is “XCZZU”.
Multiplicative Cipher
• Example: Decrypt the message “XCZZU” with a key of 7 using multiplicative
cipher.
• Solution:We apply the following decryption algorithm to the ciphertext character
by character: P=(C x k-1) mod 26, where k-1 is the multiplicative inverse of k. Here
the multiplicative inverse of 7 is 15 in z26.
• Ciphertext: X 23 Decryption: (23x15) mod 26 Plaintext: 05 h
• Ciphertext: C 02 Decryption: (02x15) mod 26 Plaintext: 04 e
• Ciphertext: Z 25 Decryption: (25x15) mod 26 Plaintext: 11 l
• Ciphertext: Z 25 Decryption: (25x15) mod 26 Plaintext: 11 l
• Ciphertext: U 20 Decryption: (20x15) mod 26 Plaintext: 14 o
• The result is ‘hello’.
Affine Cipher
It is the combination of additive and multiplicative ciphers with a pair of keys.
The first key is used with the multiplicative cipher which comes from Z 26*. This
set has only 12 members: 1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25.
The second key is used with the additive cipher which comes from Z 26. This set
has only 26 members: 0, 1, 2, 3, 4, 5, ……, 25.
Therefore, the size of the key domain for any Affine cipher is
• 26 × 12 = 312.
Affine Cipher
Figure below shows that Affine cipher is actually two ciphers, applied one after
another.
In Affine cipher, the encryption and decryption algorithms are based on the following
two formulas:
Affine Cipher
• Example:Use the affine cipher to decrypt the message “ZEBBW” with the key pair (7, 2)
in modulus 26.
• Solution:We apply the following decryption algorithm to the ciphertext character by
character: C=((P - k2) x k-1) mod 26, where k-1 is the multiplicative inverse of k1 and –k2 is
the additive inverse of k2. Here additive inverse of 2 is 24 and multiplicative inverse of 7
is 15.
• The plaintext is “hello”.
Traditional Symmetric- Key Ciphers
• Polyalphabetic Cipher:
In polyalphabetic substitution, each occurrence of a character may have a different
substitute.
The relationship between a character in the plaintext to a character in the ciphertext is
one-to-many.
For example, letter “a” could be enciphered as “D” in the beginning of the text, but as
“N” at the middle.
Polyalphabetic ciphers have the advantage of hiding the letter frequency of the
undelying language. Eve cannot use the single-letter frequency statistics to break the
ciphertext.
Autokey cipher, playfair cipher, vigenere cipher, Hill cipher etc. are some examples of
polyalphabetic ciphers.
Example:
The following shows a plaintext and its corresponding ciphertext. The cipher is
polyalphabetic because each l (el) is encrypted by a different character. The first l (el) is
encrypted as N; the second as Z.
• Plaintext: hello Ciphertext: ABNZF
Autokey Cipher
In autokey cipher, the key is a stream of subkeys, in which each subkey is used to
encrypt the corresponding plaintext character.
The first subkey is a predetermined value secretly agreed upon by Alice and Bob.
The second subkey is the value of the first plaintext character (between 0 to 25).
The third subkey is the value of the second plaintext character. And so on.
Encryption and decryption is done using the following formulas.
The name of this cipher as ‘autokey’ implies that the subkeys are automatically
created from the plaintext cipher characters during the encryption process.
Autokey Cipher
• Example of Encryption: Assume that Alice and Bob agreed to use an autokey cipher with
initial key value k1 = 12. Now Alice wants to send Bob the message “Attack is today”.
• Solution:Enciphering is done character by character.
1. Replace each plaintext character by its integer value (e.g a with 00, b with 01 etc.)
2. Write the 1st stsubkey (k1=12) underneath the 1st plaintext
nd
character, 2 nd
subkey (k2=00,
which is the 1 plaintext character) underneath the 2 plaintext character. And so on.
3. Now encrypt each plaintext character using the formula:
• Ci=(Pi+ki) mod 26
• For example, for 3rd plaintext character t, its corresponding ciphertext is-
• C3=(P3+k3) mod 26
• t=(19+19) mod 26
• t=12=M
Autokey Cipher
• Example of Encryption (Continue):
• The ciphertext is “MTMTCMSALHRDY”.
• We see that autokey cipher is a polyalphabetc cipher because the three
occurrence of “a” in the plaintext are encrypted differently. The 1st ‘a’ is
encrypted as M, the 2nd as T, and the 3rd as D.
Autokey Cipher
• Example of Decryption:
• Ciphertext : M T M T C M S A L H R D Y
C’s Values : 12 19 12 19 02 12 18 00 11 07 17 03 24
Key’s Stream: 12 00 19 19 00 02 10 08 18 19 14 03 00
P’s Values : 00 19 19 00 02 10 08 18 19 14 03 00 24
Plaintext : a t t a c k i s t o d a y
• The plaintext is “attack is today”.
Playfair Cipher
The best-known poly-alphabetic cipher is Playfair cipher.
This cipher is invented by Charles Wheatstone in 1854, but named after his
friend Baron Playfair. It was used by the British Army during World War I.
The secret key in this cipher is made of 25 alphabet letters arranged in a 5x5
matrix (letters I and J are considered the same when encrypting).
Different arrangements of the letters in the matrix can create many different
secret keys. One of the possible arrangement is shown in the figure here.
Before encryption, the plaintext characters are grouped as two-character pairs.
If the two letters in a pair are the same, a bogus letter is inserted to separate
them.
Playfair Cipher
• After inserting the bogus letters (if any), if the number of characters in the plaintext is odd, then
one extra bogus character is added at the end to make the number of characters even.
Figure: Secret key in Playfair Cipher
Playfair Cipher
• Encryption Rule for Playfair Cipher:
• The playfair cipher uses three rules for encryption:
1. If the two letters in a pair are located in the same row of the secret key matrix,
the corresponding encrypted character for each letter is the next letter to the
right in the same row (with wrapping to the beginning of the row if the
plaintext letter is the last character in the row).
2. If the two letters in a pair are located in the same column of the secret key
matrix, the corresponding encrypted character for each letter is the letter
beneath it in the same column (with wrapping to the beginning of the column if
the plaintext letter is the last character in the column).
3. If the two letters in a pair are not located in the same row or column of the
secret key matrix, the corresponding encrypted character for each letter is a
letter that is in its own row but in the same column as the other letter.
Playfair Cipher
• Example of Encryption: Encrypt the plaintext “hello” using the secret key matrix shown
in the figure below.
Solution: We group the plaintext as two-character pairs: “he ll o”
Here, in the second pair, the two letters are the same. So, we insert x as a bogus letter
between the two l’s. Now we have:
• “he lx lo”.
Now encrypt the message using the encryption rules for playfair cipher.
• We see that playfair cipher is actually a polyalphabetc cipher because the two
occurrence of “l” in the plaintext are encrypted differently, such as “Q” and “B”.
Playfair Cipher
Decryption Rule for Playfair Cipher:
• The playfair cipher uses three rules for decryption:
1. If the two ciphertext letters in a pair are located in the same row of the
secret key matrix, the corresponding decrypted character for each letter
is the previous letter to the left in the same row (with wrapping to the
end of the row if the ciphertext letter is the first character in the row).
2. If the two ciphertext letters in a pair are located in the same column of
the secret key matrix, the corresponding decrypted character for each
letter is the letter above it in the same column (with wrapping to the end
of the column if the ciphertext letter is the first character in the column).
3. If the two ciphertext letters in a pair are not located in the same row or
column of the secret key matrix, the corresponding decrypted character
for each letter is a letter that is in its own row but in the same column as
the other letter
Playfair Cipher
• Example of Decryption: Decrypt the message “ECQZBX” using the secret key matrix shown in
the figure below.
• Solution:
We group the ciphertext as two-character pairs: “EC QZ BX”
Now decrypt the message using the decryption rules for playfair cipher.
• Figure: Secret Key
Vigenere Cipher
This cipher was designed by French mathematician Blaise de Vigenere.
In this cipher, the secret key stream is created by repeating the initial secret
key stream as many times as needed.
The initial secret key stream of length m (where 1<=m<=26) is previously
agreed upon by Alice and Bob.
The cipher can be described as follows:
P=P1P2P3… C=C1C2C3… K=[(k1,k2,…,km),
(k1,k2,…,km),…]
Encryption: Ci=(Pi+ki) mod 26
Decryption: Pi=(Ci-ki) mod 26
• Here, (k1, k2, k3,….., km) is the initial secret key stream
Vigenere Cipher
• Example of Encryption: Encrypt the message “She is listening” using Vigenere
cipher with the 6-character keyword “PASCAL”.
• Solution:
1. The initial key stream is “PASCAL” (15, 0, 18, 2, 0, 11). The key stream is the
repetition of this initial key stream (as many times as needed).
2. Now encrypt each plaintext character using the formula Ci=(Pi+ki) mod 26
The ciphertext is “HHWKSWXSLGNTCG”.
Vigenere Cipher
• Example of Decryption:Decrypt the message “HHWKSWXSLGNTCG” using
Vigenere cipher with the 6-character keyword “PASCAL”.
• Solution:
1. The initial key stream is “PASCAL” (15, 0, 18, 2, 0, 11). The key stream is the
repetition of this initial key stream (as many times as needed).
2. Now decrypt each plaintext character using the formula Pi=(Ci-ki) mod 26
• The plaintext is “she is listening”.
One-Time Pad
An additive cipher can be easily broken because the same key is used to encrypt each
character of the plaintext.
In one-time pad, each character in the plaintext is encrypted with a key randomly
chosen from a key domain (00, 01, 02, 03, …..,25). For example, the first character of
the plaintext is encrypted using the key 04, the second character is encrypted using
the key 02, the third character is encrypted using the key 21; and so on.
This type of cipher is invented by Vernam. The key has the same length as the
plaintext and is chosen completely in random.
The same key is used to encrypt and decrypt for each individual character of
the message and then discarded. Each new message requires a new key of the
same length as the new message.
❖ Ciphertext-only attack is impossible for one-time pad.
❖ If the sender changes the key sequence randomly each time she sends a
message, then other types of attacks are also impossible.
❖ One-time pad produces random output that bears no statistical relationship
to the plaintext. So, there is simply no way to break the code.
One-Time Pad
Though perfect secrecy can be achieved through one-time pad,
it has some difficulties:
❖ It is almost impossible to implement commercially, because
large quantities of random key generation is very difficult.
❖ For every message to be sent, a key of equal length is
needed by both sender and receiver. How can Alice tell Bob
the new key each time she has a message to send? Thus, a
mammoth key distribution problem exists.
Hill Cipher
This cipher was invented by Lester S. Hill.
In this cipher, the plaintext is divided into equal-size blocks.
The blocks are encrypted one at a time in such a way that each character in the
block contributes to the encryption of other characters in the block.
In a Hill cipher, the key is a square matrix of size m x m in which m is the size of
the block (i.e. number of characters in each block).
The key matrix K in the Hill cipher needs to have a multiplicative inverse.
Figure: Key in the Hill Cipher
Hill Cipher
• Example: Encrypt the message “code is ready” using Hill cipher.
Solution:
There are 11 characters in the plaintext message.
For dividing it into equal-size blocks (here, 3 blocks with 4 characters per block),
add an extra bogus character “z” to the last block, and then remove the spaces.
Thus, the plaintext characters can be represented by a 3 × 4 matrix P.
Hence, the key will be a 4 x 4 square matrix K.
The ciphertext is obtained from ciphertext matrix C as “OHKNIHGKLISS”.
Decryption is done using the inverse of the key matrix.
Hill Cipher
THANK YOU