Cryptography
DR. ADNAN HAIDER
Welcome
Course objectives:
Learn how crypto primitives work
Learn how to use them correctly and reason about security
Course Outlines
Introduction
Symmetric Encryption
Asymmetric Encryption
Hashing
Digital Certificate
Book
Cryptography is everywhere
Secure communication:
web traffic: HTTPS
wireless traffic: 802.11i WPA2 (and WEP), GSM, Bluetooth
Encrypting files on disk: EFS, TrueCrypt
Content protection (e.g. DVD, Blu-ray): CSS, AACS
User authentication
… and much much more
Secure communication
no eavesdropping
no tampering
Secure Sockets Layer / TLS
Two main parts
1. Handshake Protocol: Establish shared secret key
using public-key cryptography (2nd part of course)
2. Record Layer: Transmit data using shared secret key
Ensure confidentiality and integrity (1st part of
course)
Protected files on disk
Disk
Alice File 1 Alice
No eavesdropping
No tampering
File 2
Analogous to secure communication:
Alice today sends a message to Alice tomorrow
Crypto Core
Talking Talking
to Bob to Alice
Secret key establishment:
Alice
Bob
attacker???
Secure communication:
k m1
k
m2
confidentiality and integrity
But crypto can do much more
Digital signatures
Anonymous communication
Who did I
just talk to?
Alice
Alice
Bob signature
But crypto can do much more
Digital signatures
Anonymous communication
Anonymous digital cash
Can I spend a “digital coin” without anyone knowing who I am?
How to prevent double spending?
Who
1$ Alice was
Internet that?
(anon. comm.)
Protocols
Elections
Private auctions
Protocols
Elections
Private auctions
trusted
Goal: compute f(x1, x2, x3, x4)
authority
“Thm:” anything that can done with trusted auth. can also
be done without
Secure multi-party computation
Crypto magic
Privately outsourcing computation What did
she search
for?
search
query E[ query ]
Alice
E[ results ]
Zero knowledge (proof of knowledge)
results
???
Alice I know the factors of N !!
N=p∙q N
proof π Bob
Things to remember
Cryptography is:
A tremendous tool
The basis for many security mechanisms
Cryptography is not:
The solution to all security problems
Reliable unless implemented and used properly
Something you should try to invent yourself
• many many examples of broken ad-hoc designs
End of Segment
Classical Encryption
Symmetric Encryption
or conventional / private-key / single-key
sender and recipient share a common key
all classical encryption algorithms are private-key
was only type prior to invention of public-key in 1970’s
and by far most widely used
Some Basic Terminology
plaintext - original message
ciphertext - coded message
cipher - algorithm for transforming plaintext to ciphertext
key - info used in cipher known only to sender/receiver
encipher (encrypt) - converting plaintext to ciphertext
decipher (decrypt) - recovering ciphertext from plaintext
cryptography - study of encryption principles/methods
cryptanalysis (codebreaking) - study of principles/
methods of deciphering ciphertext without knowing key
cryptology - field of both cryptography and cryptanalysis
Symmetric Cipher Model
Requirements
two requirements for secure use of symmetric encryption:
a strong encryption algorithm
a secret key known only to sender / receiver
mathematically have:
Y = EK(X)
X = DK(Y)
assume encryption algorithm is known
implies a secure channel to distribute key
Cryptography
characterize cryptographic system by:
type of encryption operations used
substitution / transposition / product
number of keys used
single-key or private / two-key or public
way in which plaintext is processed
block / stream
Cryptanalysis
objective to recover key not just message
general approaches:
cryptanalytic attack
brute-force attack
Cryptanalytic Attacks
ciphertext only
only know algorithm & ciphertext, is statistical, know or can identify plaintext
known plaintext
know/suspect plaintext & ciphertext
chosen plaintext
select plaintext and obtain ciphertext
chosen ciphertext
select ciphertext and obtain plaintext
chosen text
select plaintext or ciphertext to en/decrypt
More Definitions
unconditional security
no matter how much computer power or time is available, the cipher cannot be
broken since the ciphertext provides insufficient information to uniquely determine
the corresponding plaintext
computational security
given limited computing resources (eg time needed for calculations is greater
than age of universe), the cipher cannot be broken
Brute Force Search
Brute force search means to try every possible key
This is a basic attack, and its speed is proportional to key size
You should know or be able to recognise plaintext
Key Size (bits) Number of Alternative Avg. Time required at 1 Avg. Time required at 106
Keys decryption/µs decryptions/µs
32 232 = 4.3 109 231 µs = 35.8 minutes 2.15 milliseconds
56 256 = 7.2 1016 255 µs = 1142 years 10.01 hours
128 2128 = 3.4 1038 2127 µs = 5.4 1024 years 5.4 1018 years
168 2168 = 3.7 1050 2167 µs = 5.9 1036 years 5.9 1030 years
26 characters 26! = 4 1026 2 1026 µs = 6.4 1012 years 6.4 106 years
(permutation)
Classical Substitution Ciphers
Substitution Ciphers involves:
Replacing letters of plaintext by other letters, numbers, or symbols.
In digital word, replacing plaintext bit patterns with ciphertext bit patterns
Substitution Ciphers
Single Letter Cipher
Caser Cipher
Multi-letter Cipher
Polyfair Cipher
Hill Cipher
Polyalphabetic Cipher
Vigenère Cipher
Vernam Cipher
https://www.youtube.com/watch?v=vFifNTTrSEc
Encrypt by Shifting
Caesar Cipher
Caesar Cipher
The simplest substitution cipher
The exact origin of this technique is unclear; however, It is named after
Julius Caesar, who used it in military communication.
first attested use in military affairs replaces each letter by 3rd letter on
example:
meet me after class
PHHW PH DIWHU FODVV
Caesar Cipher
The transformation can be defined by listing all possibilities, as follows:
plain: 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
cipher: D E F G H I J K L M N O P Q R S T U V W X Y Z A B C
Then, substituting letters of plaintext by its substitution in
the ciphertext mapped underneath
c = E(p) = (p + k) mod (26)
p = D(c) = (c – k) mod (26)
Caesar Cipher | Mathematic
Expression
Mathematically representation is achieved as follows:
Give each letter a unique number from 0 to N-1 where N is the total number of alphabet
letters as follows:
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
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
Then, encryption/decryption functions can be derived as follows
c = E(p) = (p + k) mod (26)
p = D(c) = (c – k) mod (26)
Cryptanalysis of Caesar Cipher
only have 26 possible ciphers
A maps to A,B,..Z
could simply try each in turn; a brute force search
given ciphertext, just try all shifts of letters.
do need to recognize when have plaintext
eg. break ciphertext "GCUA VQ DTGCM"
Brute-Force Cryptanalysis of Caesar
Cipher
Ciphertext
PHHW PH DIWHU WKH WRJD SDUWB
Keys Brute-Force decoding Keys Brute-Force decoding
0 PHHW PH DIWHU WKH WRJD SDUWB 13 btti bt puitg iwt idvp epgin
1 nffu nf bgufs uif uphb qbsuz 14 assh as othsf hvs hcuo dofhm
2 meet me after the toga party 15 zrrg zr nsgre gur gbtn cnegl
3 ldds ld zesdq sgd snfz ozqsx 16 yqqf yq mrfqd ftq fasm bmdfk
4 kccr kc ydrcp rfc rmey nyprw 17 xppe xp lqepc esp ezrl alcej
5 jbbq jb xcqbo qeb qldx mxoqv 18 wood wo kpdob dro dyqk zkbdi
6 iaap ia wbpan pda pkcw lwnpu 19 vnnc vn jocna cqn cxpj yjach
7 hzzo hz vaozm ocz ojbv kvmot 20 ummb um inbmz bpm bwoi xizbg
8 gyyn gy uznyl nby niau julns 21 tlla tl hmaly aol avnh whyaf
9 fxxm fx tymxk max mhzt itkmr 22 skkz sk glzkx znk zumg vgxze
10 ewwl ew sxlwj lzw lgys hsjlq 23 rjjy rj fkyjw ymj ytlf ufwyd
11 dvvk dv rwkvi kyv kfxr grikp 24 qiix qi ejxiv xli xske tevxc
12 cuuj cu qvjuh jxu jewq fqhjo 25 oggv og chvgt vjg vqic rctva
Caesar Cipher | Brute-Force
Brute force attack is possible because:
Cipher algorithms are known
Short number of keys to try: There are only 25 keys to try
The language of plaintext is known and easily recognizable.
Brute-Force Cryptanalysis of Caesar
Cipher
It is very easy to break
Is there any better way of using substitution?
Monoalphabetic Cipher
What if we shuffle the letters arbitrarily (i.e. uses Arbitrary Substitution / Permutation).
Each plaintext letter maps to a different random ciphertext letter, hence key is 26 letters
long
How much does it take to break the encryption (being able to recognize plaintext)?
Plain: abcdefghijklmnopqrstuvwxyz
Cipher: DKVQFIBJWPESCXHTMYAUOLRGZN
Plaintext: ifwewishtoreplaceletters
Ciphertext: WIRFRWAJUHYFTSDVFSFUUFYA
Arbitrary Substitution | Permutation
A permutation of a finite set of elements S is an ordered sequence of all the elements of
S, with each element appearing exactly once.
For example, if S = {a, b, c}, there are six permutations of S: abc, acb, bac, bca, cab, cba
In general, there are n! permutations of a set of n elements.
Monoalphabetic Cipher Security
Arbitrary Substitution have a total of 𝟐𝟔! = 𝟒 𝒙 𝟏𝟎𝟐𝟔 keys
Unlike Caesar cipher, there are so many keys.
Is this approach secure?
Is it difficult to break?
No, but Why? problem is language characteristics
Language Redundancy and
Cryptanalysis
human languages are redundant
eg "pls wrk hrd"
letters are not equally commonly used
in English E is by far the most common letter
followed by T,R,N,I,O,A,S
other letters like Z,J,K,Q,X are fairly rare
have tables of single, double & triple letter
frequencies for various languages
English Letter Frequencies
Arabic Letter Frequencies
Letter Frequencies & Cryptanalysis
key concept - monoalphabetic substitution ciphers do not
change relative letter frequencies
discovered by Arabian scientists in 9th century
calculate letter frequencies for ciphertext
compare counts/plots against known values
if Caesar cipher look for common peaks/troughs
For monoalphabetic, You must identify each letter
tables of common double/triple letters help
Example Cryptanalysis
given ciphertext:
UZQSOVUOHXMOPVGPOZPEVSGZWSZOPFPESXUDBMETSXAIZ
VUEPHZHMDZSHZOWSFPAPPDTSVPQUZWYMXUZUHSX
EPYEPOPDZSZUFPOMBZWPFUPZHMDJUDTMOHMQ
count relative letter frequencies (see text)
guess P & Z are e and t
guess ZW is th and hence ZWP is the
The letters {S, U, O, M, H} are all of relatively high frequency and probably correspond to plain letters from the set {a, h, i,
n, o, r, s}
{A, B, G, Y, I, J) are likely included in the set {b, j, k, q, v, x, z}
proceeding with trial and error finally get:
it was disclosed yesterday that several informal but
direct contacts have been made with political
representatives of the viet cong in moscow
Encrypt Two Letters at Once.
Defeat Single Letter
Frequencies.
Playfair Cipher
Playfair Cipher
not even the large number of keys in a monoalphabetic cipher provides
security
one approach to improving security was to encrypt multiple letters
the Playfair Cipher is an example
invented by Charles Wheatstone in 1854, but named after his friend Baron
Playfair
Playfair Key Matrix
a 5X5 matrix of letters based on a keyword
fill in letters of keyword (sans duplicates)
fill rest of matrix with other letters
eg. using the keyword MONARCHY
M O N A R
C H Y B D
E F G I/J K
L P Q S T
U V W X Z
Encrypting and Decrypting
plaintext is encrypted two letters at a time
1. if a pair is a repeated letter, insert filler like 'X’
2. if both letters fall in the same row, replace each with letter to right
(wrapping back to start from end)
3. if both letters fall in the same column, replace each with the letter below it
(again wrapping to top from bottom)
4. otherwise each letter is replaced by the letter in the same row and in the
column of the other letter of the pair
Security of Playfair Cipher
security much improved over monoalphabetic
since have 26 x 26 = 676 digrams
would need a 676 entry frequency table to analyse
(verses 26 for a monoalphabetic)
and correspondingly more ciphertext
was widely used for many years
eg. by US & British military in WW1
it can be broken, given a few hundred letters
since still has much of plaintext structure
Encrypt by Shifting
Hill Cipher
Multiletter Substitution Cipher
(Hill Cipher)
Substitute m successive plaintext letter with m ciphertext letters (e.g. m=3)
encryption algorithm:
𝐶1 𝐾11 𝐾12 𝐾13 𝑃1
𝐶2 = 𝐾21 𝐾22 𝐾23 𝑃2 mod 2 6
𝐶3 𝐾31 𝐾32 𝐾33 𝑃3
𝐾11 𝐾12 𝐾13
where 𝐾 = 𝐾21 𝐾22 𝐾23 is the key, and det(k)≠0 mod 26
𝐾31 𝐾32 𝐾33
𝑃1 𝐾11 𝐾12 𝐾13 −1 𝐶1
decryption algorithm: 𝑃2 = 𝐾21 𝐾22 𝐾23 𝐶2 mod 2 6
𝑃3 𝐾31 𝐾32 𝐾33 𝐶3
2
Key space=26𝑚
Hill Cipher
In general terms, the Hill system can be expressed as
C = E(K, P) = PK mod 26
𝑷 = 𝐷 𝑲, 𝑪 = 𝑪𝑲−1 𝑚𝑜𝑑 26 = 𝑷𝑲𝑲−1 = 𝑷
We know that 𝐾11 𝐾12 𝐾13 𝐾11 𝐾12 𝐾13 −1
1 0 0
𝑲𝑲−1 = 𝐾21 𝐾22 𝐾23 𝐾21 𝐾22 𝐾23 =𝐼= 0 1 0
𝐾31 𝐾32 𝐾33 𝐾31 𝐾32 𝐾33 0 0 1
So, 𝑲 −1 can be calculated as follows
𝑎1 𝑎2 𝑎3
If 𝑲−1 = 𝑏1 𝑏2 𝑏3 THEN solve the following to obtain 𝑲−1
𝑐1 𝑐2 𝑐3
𝑎1 𝐾11 + 𝑏1 𝐾12 + 𝑐1 𝐾13 𝑎2 𝐾11 + 𝑏2 𝐾12 + 𝑐2 𝐾13 𝑎3 𝐾11 + 𝑏3 𝐾12 + 𝑐3 𝐾13 1 0 0
𝑲𝑲−1 = 𝑎1 𝐾21 + 𝑏1 𝐾22 + 𝑐1 𝐾23 𝑎2 𝐾21 + 𝑏2 𝐾22 + 𝑐2 𝐾23 𝑎3 𝐾21 + 𝑏3 𝐾22 + 𝑐3 𝐾23 = 0 1 0
𝑎1 𝐾31 + 𝑏1 𝐾32 + 𝑐1 𝐾33 𝑎2 𝐾31 + 𝑏2 𝐾32 + 𝑐2 𝐾33 𝑎3 𝐾31 + 𝑏3 𝐾32 + 𝑐3 𝐾33 0 0 1
Hill Cipher
Find the det(K), and iff gcd det 𝐾 , 26 = 1 then proceed on
If matrix is 2 × 2
𝑎 𝑏 1 𝑑 −𝑏
K= , det K ≡ 𝐾 = 𝑎𝑑 − 𝑏𝑐, 𝐾 −1 =
𝑐 𝑑 det 𝐾 −𝑐 𝑎
If matrix is 3 × 3
k 22 k 23 k k 23 k k 22
det 𝐾 = K = k11 − k12 21 + k13 21
k 32 k 33 k 31 k 33 k 31 k 32
𝑘22 𝑘23 𝑘13 𝑘12 𝑘12 𝑘13
𝑘32 𝑘33 𝑘33 𝑘32 𝑘22 𝑘23
k11 k12 k13
1 𝑘23 𝑘21 𝑘11 𝑘13 𝑘13 𝑘11
K = k 21 k 22 k 23 , 𝐾 −1 = =
𝐾 𝑘33 𝑘31 𝑘31 𝑘33 𝑘23 𝑘21
k 31 k 32 k 33
𝑘21 𝑘22 𝑘12 𝑘11 𝑘11 𝑘12
𝑘31 𝑘32 𝑘32 𝑘31 𝑘21 𝑘22
Hill Cipher
Example:
17 17 5 4 9 15
𝐾 = 21 18 21 , 𝐾 −1 = 15 17 6
2 2 19 24 0 17
It is easy to be broken by known plaintext attack by solve the following
equation:
𝐶 = 𝑃𝐾
Case 1: if P-1 exists, then 𝑲 = 𝑷−𝟏 𝑪
Case2: if P-1 not exist, then change P and C until P-1 found
Hill Cipher | Quiz
Find the Key 𝐾2×2 if the encryption of gold is cyfz
We know that 𝑲 = 𝑷−𝟏 𝑪 and 𝑷𝑷−𝟏 = 𝑰, So
𝑔𝑜 = 6 14 and 𝑙𝑑 = 11 3
𝑎 𝑏
𝐾= ,
𝑐 𝑑
6 14 𝐾 = 6𝑎 + 14𝑐 6𝑐 + 14𝑑 = 2 24
11 3 𝐾 = 11𝑎 + 3𝑐 11𝑐 + 3𝑑 = 5 25
11 2 − 14𝑐 + 18𝑐 22 − 136𝑐
= =5
6 6
22 − 30 −8 1
𝑐= = =− = −1 × 17−1 = −1 × 23 = 3
136 136 17
Hill Cipher | Quiz
Find the Key 𝐾2×2 if the encryption of gold is cyfz
We know that 𝑲 = 𝑪𝑷−𝟏 and 𝑷𝑷−𝟏 = 𝑰, So
Enc(gold)=CYFZ, Enc(bdbe)=LVOB
1 3 4 −3
𝑷= , 𝑷−𝟏 =
1 4 −1 1
4 −3 11 21 2 3
𝑲 = 𝑷−𝟏 𝑪 = =
−1 1 14 1 3 6
Polyalphabetic Cipher
A Set of Related Monoalphabetic
Substitution Rules
Polyalphabetic Ciphers
polyalphabetic substitution ciphers
improve security using multiple cipher alphabets
make cryptanalysis harder with more alphabets to
guess and flatter frequency distribution
use a key to select which alphabet is used for each
letter of the message
use each alphabet in turn
repeat from start after end of key is reached
Vigenère Cipher
simplest polyalphabetic substitution cipher
effectively multiple caesar ciphers
key is multiple letters long K = k1 k2 ... kd
ith letter specifies ith alphabet to use
use each alphabet in turn
repeat from start after d letters in message
decryption simply works in reverse
Example of Vigenère Cipher
write the plaintext out
write the keyword repeated above it
use each key letter as a caesar cipher key
encrypt the corresponding plaintext letter
eg using keyword deceptive
key: deceptivedeceptivedeceptive
plaintext: wearediscoveredsaveyourself
ciphertext:ZICVTWQNGRZGVTWAVZHCQYGLMGJ
Aids
simple aids can assist with en/decryption
a Saint-Cyr Slide is a simple manual aid
a slide with repeated alphabet
line up plaintext 'A' with key letter, eg 'C'
then read off any mapping for key letter
can bend round into a cipher disk
or expand into a Vigenère Tableau
Vigenère
Tableau
Security of Vigenère Ciphers
have multiple ciphertext letters for each plaintext letter
hence letter frequencies are obscured
but not totally lost
start with letter frequencies
see if look monoalphabetic or not
if not, then need to determine number of alphabets, since then can
attack each monoalphabetic ciphers separately
Kasiski Method
method developed by Babbage / Kasiski
repetitions in ciphertext give clues to period.
So, look for repeated fragments in the ciphertext and compile
a list of the distances that separate the repetitions. Then, the
keyword length is likely to divide many of these distances.
of course, could also be random fluke
e.g. repeated “VTW” in previous example suggests size of 3 or 9
Then attack each monoalphabetic cipher individually using
same techniques as before
key: deceptivedeceptivedeceptive
plaintext: wearediscoveredsaveyourself
ciphertext:ZICVTWQNGRZGVTWAVZHCQYGLMGJ
Kasiski Method
Autokey Cipher
ideally want a key as long as the message
Vigenère proposed the autokey cipher with keyword
prefixed to message as key
knowing keyword can recover the first few letters
use these in turn on the rest of the message
but still have frequency characteristics to attack
eg. given key deceptive
key: deceptivewearediscoveredsav
plaintext: wearediscoveredsaveyourself
ciphertext:ZICVTWQNGKZEIIGASXSTSLVVWLA
Vernam Cipher
The ultimate defense against such a cryptanalysis is to choose a keyword
that is as long as the plaintext and has no statistical relationship to it.
Such a system was introduced by an AT&T engineer named Gilbert
Vernam in 1918.
One-Time Pad
if a truly random key as long as the message is used, the cipher will be secure
called a One-Time pad
is unbreakable since ciphertext bears no statistical relationship to the plaintext
since for any plaintext & any ciphertext there exists a key mapping one to
other
can only use the key once though
problems in generation & safe distribution of key
Perfect Secrecy
It means that the ciphertext conveys no information about the content of
the plaintext.
A ciphertext maintains perfect secrecy if the attacker’s knowledge of the
contents of the message is the same both before and after the adversary
inspects the ciphertext, attacking it with unlimited resources.
∀𝑚0 , 𝑚1 ∈ ℳ 𝑚0 = 𝑚1 𝑎𝑛𝑑 ∀𝑐 ∈ 𝒞, Pr 𝐸 𝑘, 𝑚0 = 𝑐 = Pr 𝐸 𝑘, 𝑚1 = 𝑐 𝑤ℎ𝑒𝑟𝑒 𝑘 ← 𝒦
One-Time Pad is the only cryptosystem that provides perfect secrecy.
Thm: perfect secrecy ⇒ 𝒦 ≥ ℳ It is hard to use in practice
Transposition Ciphers
now consider classical transposition or permutation ciphers
these hide the message by rearranging the letter order
without altering the actual letters used
can recognise these since have the same frequency distribution as the
original text
Rail Fence cipher
write message letters out diagonally over a
number of rows
then read off cipher row by row
eg. write message out as:
m e m a t r h t g p r y
e t e f e t e o a a t
giving ciphertext
MEMATRHTGPRYETEFETEOAAT
Row Transposition Ciphers
a more complex transposition
write letters of message out in rows over a specified number of columns
then reorder the columns according to some key before reading off the rows
Key: 3 4 2 1 5 6 7
Plaintext: a t t a c k p
o s t p o n e
d u n t i l t
w o a m x y z
Ciphertext: TTNAAPTMTSUOAODWCOIXKNLYPETZ
Product Ciphers
ciphers using substitutions or transpositions are not
secure because of language characteristics
hence consider using several ciphers in succession
to make harder, but:
two substitutions make a more complex substitution
two transpositions make more complex transposition
but a substitution followed by a transposition makes a new
much harder cipher
this is bridge from classical to modern ciphers
Rotor Machines
before modern ciphers, rotor machines were most
common complex ciphers in use
widely used in WW2
German Enigma, Allied Hagelin, Japanese Purple
implemented a very complex, varying substitution
cipher
used a series of cylinders, each giving one substitution,
which rotated and changed after each letter was
encrypted
with 3 cylinders have 263=17576 alphabets
Hagelin Rotor Machine
Steganography
an alternative to encryption
hides existence of message
using only a subset of letters/words in a longer message marked in some way
using invisible ink
hiding in LSB in graphic image or sound file
has drawbacks
high overhead to hide relatively few info bits
Summary
have considered:
classical cipher techniques and terminology
monoalphabetic substitution ciphers
cryptanalysis using letter frequencies
Playfair cipher
polyalphabetic ciphers
transposition ciphers
product ciphers and rotor machines
stenography
Secure Usage of Symmetric Encryption
There are two requirements:
Strong Encryption algorithm
An attacker would not be able to decrypt the ciphertext nor discover the key, even if the
attacker is in possession of a number of ciphertexts together with the plaintext that produced
each ciphertext.
Securely share the key between senders and receivers.