1.2 Encryption
1.2 Encryption
ntist WAYNE
Computer Science
ary
A N I N T E R D I S C I P L I N A RY A P P R O A C H
or
d
gs,
of
omputer
C cience 1.2 Encryption
hem
h.
S
nce
d
wick
ysis
orics;
ver
An Interdisciplinary Approach
ROBERT SEDGEWICK
ANADA
K E V I N WAY N E
Sending a secret message
Bob
Hey, Bob. Here's a secret message. Hey, Bob. Here's a secret message.
encrypted message gX76W3v7K is "in the clear" (anyone can read it, including Eve) gX76W3v7K ??
Critical point: Without the key, Eve cannot understand the message.
S E N D M O N E Y
encrypt
g X 7 6 W 3 v 7 K
decrypt
S E N D M O N E Y
Bottom line. If we can send and receive bits, we can send and receive anything.
well, not cars or cats (yet)
4
Encoding text as a sequence of bits
bits symbols
Base64 encoding of character strings
Base64 6 64
• A simple method for representing text.
• 64 different symbols allowed: A-Z, a-z, 0-9, +, /. ASCII 8 256
(extended version)
• 6 bits to represent each symbol.
Unicode 16 65,536
• ASCII and Unicode methods used on your computer are similar.
Example:
S E N D M O N E Y
S E N D M O N E Y 010010 0 0 0 1 0 0 0 0 11 0 1 0 0 0 0 11 0 0 11 0 0 0 0 111 0 0 0 11 0 1 0 0 0 1 0 0 0 11 0 0 0
5
Encoding versus Encryption
So in the slides that follow, we are going to encode text in binary using
Base64
• and then encrypt or decrypt it using a one-time pad
6
One-Time Pads
y T 2 5 a 5 i / S
7
Encryption with a one-time pad
Preparation
• Create a "random" sequence of bits (a one-time pad).
• Send one-time pad to intended recipient through a secure channel.
Encryption
• Encode text as a sequence of N bits.
• Use the first N bits of the pad. important point: need to have as many bits
in the pad as there are in the message.
• Compute a new sequence of N bits from the message and the pad.
• Decode result to get a sequence of characters.
a
Result: A ciphertext (encrypted message). simple
machine
message S E N D M O N E Y 010010000100001101000011001100001110001101000100011000
ciphertext g X 7 6 W 3 v 7 K 100000010111111011111010010110110111101111111011001010
8
A (very) simple machine for encryption
Definition: The bitwise exclusive or of two bits is 1 if they differ, 0 if they are the same.
S E N D M O N E Y
S E N D M O N E Y message 010010000100001101000011001100001110001101000100011000
y T 2 5 a 5 i / S
XOR
g X 7 6 W 3 v 7 K ciphertext 100000010111111011111010010110110111101111111011001010
g X 7 6 W 3 v 7 K
9
Pop quiz on bitwise XOR encryption
10
Pop quiz on bitwise XOR encryption
E A S Y
encode message
000100 000000 010010 011000
0 1 2 3
encode pad
110100 110101 110110 110111
w 1 k v decode
11
Decryption with a one-time pad
A. Alice's device uses a "bitwise exclusive or" machine to encrypt the message.
Q. What kind of machine does Bob's device use to decrypt the message?
g X 7 6 W 3 v 7 K
g X 7 6 W 3 v 7 K ciphertext 100000010111111011111010010110110111101111111011001010
y T 2 5 a 5 i / S
XOR
S E N D M O N E Y
13
Why does it work?
S E N D M O N E Y
message S E N D M O N E Y 010010000100001101000011001100001110001101000100011000
XOR
one-time pad y T 2 5 a 5 i / S 110010010011110110111001011010111001100010111111010010
ciphertext g X 7 6 W 3 v 7 K 100000010111111011111010010110110111101111111011001010
g X 7 6 W 3 v 7 K XOR
one-time pad y T 2 5 a 5 i / S 110010010011110110111001011010111001100010111111010010
message S E N D M O N E Y 010010000100001101000011001100001110001101000100011000
S E N D M O N E Y
m k m^k (m ^ k ) ^ k
Approach 1: Truth tables Approach 2: Boolean algebra (k ^ k) = 0
0 0 0 0
m^0=m
0 1 1 0
(m ^ k) ^ k = m ^ (k ^ k)
1 0 1 1 =m^0
1 1 0 1 ✓ =m ✓ 14
Decryption with the wrong pad
My informant tells
Eve cannot read a message without knowing the pad. me that Alice and
Bob's one-time
pad might be
qwDgbDuav
Eve
g X 7 6 W 3 v 7 K
ciphertext g X 7 6 W 3 v 7 K 100000010111111011111010010110110111101111111011001010
XOR
wrong pad q w D g b D u a v 101010110000000011100000011011000011101110011010101111
q w D g b D u a v
gibberish K n 4 a N 0 B h l 001010100111111000011010001101110100000001100001100101
K n 4 a N 0 B h l
Eve
Problem
pad value message?
• 54 bits, so there are 254 possible pad values.
AAAAAAAAA gX76W3v7K
• Suppose Eve could check a million values per second. AAAAAAAAB gX76W3v7L
• It would still take 570+ years to check all possibilities. AAAAAAAAC gX76W3v7I
...
qwDgbDuav Kn4aN0Bhl
Much worse problem ...
Pros:
• Very simple encryption method.
• Decrypt with the same method.
a one-time pad
• Provably unbreakable if bits are truly random.
• Widely used in practice.
17
Random bits are not so easy to find
are not so easy to come by
You might look on the internet. The randomness comes from atmospheric noise
Image sources
https://openclipart.org/detail/25617/astrid-graeber-adult-by-anonymous-25617
https://openclipart.org/detail/169320/girl-head-by-jza
https://openclipart.org/detail/191873/manga-girl---true-svg--by-j4p4n-191873 http://commons.wikimedia.org/
wiki/File:Enigma-Machine.jpg http://pixabay.com/en/binary-one-null-ball-administrator-63530/ http://
commons.wikimedia.org/wiki/File:Jimmy_Carter_Library_and_Museum_99.JPG
A pseudo-random number generator
is a deterministic machine that produces a long sequence of pseudo random bits.
Examples
Enigma.
Linear feedback shift register (next).
Blum-Blum-Shub generator.
...
[ an early application of computing ]
[ research still ongoing ]
Deterministic: Given the current state of the machine, we know the next bit.
• For this lecture: "Has enough properties of a random sequence that Eve can't tell the difference".
22
Which of these sequences appear to be random?
000000000000000000000000000000000000000000000000000000 ✗
010010000100001101000011001100001110001101000100011000 ✗ SENDMONEY
Terminology
• Bit: 0 or 1.
• Cell: storage element that holds one bit.
• Register: sequence of cells.
• Seed: initial sequence of bits.
• Feedback: Compute XOR of two bits and put result at right.
• Shift register: when clock ticks, bits propagate one position to left.
An [11, 9] LFSR 0 1 1 0 1 0 0 0 0 1 0 1
11 10 9 8 7 6 5 4 3 2 1
More terminology
• Tap: Bit positions used for XOR (one must be leftmost). Numbered from right, starting at 1.
• [N, k] LFSR: N-bit register with taps at N and k. Not all values of k give desired effect (stay tuned).
24
Linear feedback shift register simulation
0 1 1 0 1 0 0 0 0 1 0 1 0 1 1 0 1 0 0 0 0 1 0 0
1 1 0 1 0 0 0 0 1 0 1 1 1 1 0 1 0 0 0 0 1 0 1 1
1 0 1 0 0 0 0 1 0 1 1 0 1 0 1 0 0 0 0 1 0 1 1 2
a pseudo-random
^ bit sequence !
0 1 0 0 0 0 1 0 1 1 0 0 0 1 0 0 0 0 1 0 1 1 0 3
1 0 0 0 0 1 0 1 1 0 0 1 1 0 0 0 0 1 0 1 1 0 0 4
0 0 0 0 1 0 1 1 0 0 1 0 0 0 0 0 1 0 1 1 0 0 1 5
25
A random bit sequence?
Looks random to me. No long repeats.
997 0s, 1003 1s.
Q. Is this a random sequence?
256 00s, 254 01s, 256 10s, 257 11s.
...
27
Answer 28
^
0 0 0 0 1 0
0 0 0 1 0 0
0 0 1 0 0 0
0 1 0 0 0 1
1 0 0 0 1 1
0 0 0 11 0
0 0 11 0 0
0 11 0 0 1
11 0 0 1 0
1 0 0 1 0 1
0 0 1 0 1 0
28
Encryption/decryption with an LFSR
“Use the next seed in the book to
decode this secret video (1 GB)”
Preparation
• Alice creates a book of "random" (short) seeds.
• Alice sends the book to Bob through a secure channel. “ OK (consults book)
01101000010 ”
Alice
Encryption/decryption
• Alice sends Bob a description of which seed to use.
• They use the specified seed to initialize an LFSR and produce N bits.
Bob
[and proceed in the same way as for one-time pads]
message S E N D M O N E Y 010010000100001101000011001100001110001101000100011000
XOR
seed 01101000010 LFSR 110010010011110110111001011010111001100010111111010010
ciphertext g X 7 6 W 3 v 7 K 100000010111111011111010010110110111101111111011001010
g X 7 6 W 3 v 7 K XOR
seed 01101000010 LFSR 110010010011110110111001011010111001100010111111010010
message S E N D M O N E Y 010010000100001101000011001100001110001101000100011000
29
Eve's opportunity with LFSR encryption
Bad news (for Eve): It is easy for Alice and Bob to use a much longer LFSR.
30
Key properties of LFSRs
Property 1. ^
• Don’t use all 0s as a seed!
0 0 0 0 0 0 0 0 0 0 0 0
• It will create a fill of all 0s!
31
Key properties of LFSRs
Property 1. ^
• Don’t use all 0s as a seed! Ex. [4,3] LFSR 0 0 1 0 0 0
• It will create a fill of all 0s! 0 1 0 0 1 1
1 0 0 1 1 2
1 0 0 0 1 13
0 0 0 1 0 14
0 0 1 0 15
32
Key properties of LFSRs
Property 1. ^
• Don’t use all 0s as a seed! Ex. [4,2] LFSR 0 0 1 0 1 0
33
Key properties of LFSRs
Property 1.
• Don’t use all 0s as a seed!
• Fill of all 0s will not otherwise occur.
11, 9
Property 2. Bitstream must eventually cycle.
• 2N − 1 nonzero fills in an N-bit register.
63, 62
• Future output completely determined by current fill.
34
Eve's problem with LFSR encryption
gX76W3v7K ???
Eve
Bad news (for Eve): There are still way too many possibilities.
• Ex: 63-bit register implies 263 − 1 possibilities.
NOT ENOUGH COMPUTERS
• If Eve could check 1 million seeds per second,
it would take her 2923 centuries to try them all!
Bad news (for Alice and Bob): LFSR output is not random.
experts have cracked LFSRs
35
Pros and cons of LFSRs
Pros:
• Very simple encryption method.
• Decrypt with the same method.
• Scalable: 20 cells for 1 million bits; 30 cells for 1 billion bits. a commercially available LFSR
+=y=i^i/8^i>>4^i>>12,
Example. i=i>>8^y<<17,a^=a>>14,y=a^a*8^a<<6,a=a
>>8^y<<9,k=s[j],k ="7Wo~'G_\216"[k
&7]+2^"cr3sfw6v;*k+>/n."[k>>4]*2^k*257/
• CSS encryption widely used for DVDs. 8,s[j]=k^(k&k*2&34)*6^c+~y
;}}
Image sources
http://pixabay.com/en/ball-http-www-crash-administrator-216837/
http://commons.wikimedia.org/wiki/File:KnuthAtOpenContentAlliance.jpg
http://commons.wikimedia.org/wiki/File:Einstein-formal_portrait-35.jpg
LFSRs and general-purpose computers
Critical differences: Operations, input. but the simplest computers differ only slightly from LFSRs!
Programming. We can write a Python program to simulate the operation of any meaningful abstract
machine.
• Basis for theoretical understanding of computation.
• Basis for bootstrapping real machines into existence.
Stay tuned (we cover these sorts of issues later in this course).
39
Profound questions
• It is not obvious how to distinguish the bits LFSRs produce from random,
• BUT experts have figured out how to do so.
− Albert Einstein 40
C O M P 10 0 3
COMPUTER SCIENCE
An Introduction
Image sources
http://pixabay.com/en/ball-http-www-crash-administrator-216837/
http://commons.wikimedia.org/wiki/File:KnuthAtOpenContentAlliance.jpg
http://pixabay.com/en/galaxy-space-universe-all-11098/
http://commons.wikimedia.org/wiki/File:Einstein-formal_portrait-35.jpg
A final thought: Randomness and Chaos
These Slides
• principally based on slides by Robert Sedgewick and Kevin Wayne
Recommended Reading
• same as before:
• online booksite (Python version) chapters 1, 2 and 3
• make sure that you are familiar and comfortable with the basics of using functions and classes
Wider reading:
• if cryptography interests you, then Simon Singh’s The Code Book is a great read
Activity:
• code, code code!
• in particular, practice working with lists in Python, including how to:
add and remove elements
find elements
concatenate two lists
43