Cryptography in Context: Nathaniel Karst
Cryptography in Context: Nathaniel Karst
Cryptography in Context: Nathaniel Karst
in Context
Nathaniel Karst
Preface
It is safe to say that the world as we know it could exist without modern communication systems. But from online credit card transactions to secure messages
between organizations and their agents, these technologies allow us to trust
each other in an increasingly digital world. In these notes, we will begin to unpack these technologies, from both mathematical and practical perspective. You
will have the opportunity to implement many of these communication protocols
yourself, and in so doing, hopefully gain a deeper understanding and appreciation for the underlying ideas. We will regularly confront the ethical issues that
arise at the interface of these theoretic tools and their real world implementation, as the use and abuse of modern cryptography has serious implications for
the trajectory of our society.
Acknowledgements
I would like to thank the Teaching Innovation Fund at Babson College for supporting the development of these notes and exercises. My thanks also goes to
the Spring 2014 cohort of Cryptology and Coding Theory for its help in molding
these notes for general consumption.
Chapter 0. Preface
Contents
Preface
1 Introduction
1.1 Communication Systems . . . .
1.1.1 Compression . . . . . .
1.1.2 Encryption . . . . . . .
1.1.3 Protection . . . . . . . .
1.1.4 Representations of Data
1.2 Matlab . . . . . . . . . . . . . .
1.2.1 Basic Arithmetic . . . .
1.2.2 Variable Assignment . .
1.2.3 Arrays . . . . . . . . . .
1.2.4 Scripts and Functions .
1.2.5 Loops . . . . . . . . . .
1.2.6 Provided Functions . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
1
2
2
3
4
4
6
6
6
7
9
9
10
2 Classical Cryptosystems
2.1 Transposition Ciphers . . . . . . . . . . . . . . . . .
2.1.1 Scytales to Matrices . . . . . . . . . . . . . .
2.1.2 Encryption in Matlab . . . . . . . . . . . . .
2.1.3 Decryption in Matlab . . . . . . . . . . . . .
2.1.4 Cryptanalysis . . . . . . . . . . . . . . . . . .
2.2 Caesar Ciphers . . . . . . . . . . . . . . . . . . . . .
2.2.1 Caesar Encryption and Modular Arithmetic .
2.2.2 Caesar Decryption and Additive Inverses . .
2.2.3 Groups . . . . . . . . . . . . . . . . . . . . .
2.2.4 Caesar Cryptanalysis . . . . . . . . . . . . . .
2.3 Ane Ciphers . . . . . . . . . . . . . . . . . . . . . .
2.3.1 Ane Encryption . . . . . . . . . . . . . . .
2.3.2 Ane Decryption and Multiplicative Inverses
2.3.3 Rings . . . . . . . . . . . . . . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
13
14
14
15
19
19
21
21
23
23
24
27
27
27
28
iii
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
Contents
2.4
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
32
34
34
35
35
38
3 Modern Cryptosystems
3.1 Die-Hellman Key Exchange . . . . . . .
3.1.1 Primitive Roots . . . . . . . . . . .
3.1.2 Key Exchange . . . . . . . . . . .
3.1.3 Matlab Implementation . . . . . .
3.1.4 Man-in-the-middle Attacks . . . .
3.2 RSA . . . . . . . . . . . . . . . . . . . . .
3.2.1 Encryption . . . . . . . . . . . . .
3.2.2 Decryption . . . . . . . . . . . . .
3.2.3 Security . . . . . . . . . . . . . . .
3.3 Cryptographic Hashes . . . . . . . . . . .
3.3.1 Properties . . . . . . . . . . . . . .
3.3.2 Applications . . . . . . . . . . . .
3.3.3 Cryptanalysis . . . . . . . . . . . .
3.4 Digital Signatures . . . . . . . . . . . . .
3.4.1 RSA-based Signature Scheme . . .
3.4.2 DLP-based Signature Scheme . . .
3.5 Zero-Knowledge Proofs . . . . . . . . . . .
3.5.1 Schnorr Authentication . . . . . .
3.5.2 Feige-Fiat-Shamir Authentication .
3.6 Ethics in Cryptography . . . . . . . . . .
3.6.1 Obligations to Customers . . . . .
3.6.2 Utility of Hacking . . . . . . . . .
3.6.3 What to do with a break-through .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
41
42
42
43
44
46
47
49
50
50
51
52
53
54
57
57
59
63
64
66
69
69
70
71
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
73
74
75
76
77
79
82
84
85
87
89
93
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
Contents
Appendix B Studio solutions
B.1 Studio 1.1 Solutions: Communication Systems .
B.2 Studio 1.2 Solutions: Matlab . . . . . . . . . .
B.3 Studio 2.1 Solutions: Transposition Ciphers . .
B.4 Studio 2.2 Solutions: Caesar Ciphers . . . . . .
B.5 Studio 2.3 Solutions: Ane Ciphers . . . . . .
B.6 Studio 2.4 Solutions: Polyalphabetic Ciphers .
B.7 Studio 3.1 Solutions: Die-Hellman . . . . . .
B.8 Studio 3.2 Solutions: RSA . . . . . . . . . . . .
B.9 Studio 3.3 Solutions: Cryptographic Hashes . .
B.10 Studio 3.4 Solutions: Digital Signatures . . . .
B.11 Studio 3.5 Solutions: Zero-Knowledge Proofs .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
95
96
98
100
103
106
110
112
115
118
121
128
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
131
132
133
134
135
136
137
138
139
140
141
142
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
Contents
Chapter
Introduction
Cryptography is just one of a collection of technologies that allows us to communicate with one another in a digital world. In fact, these technologies can be
abstracted outside the digital context into a so-called communication system.
This theoretic object is helpful in allowing us to examine what we consider
important in a communication scheme and how these various important pieces
might t together. The general communication systems also give us the opportunity to introduce some important archetypical characters, namely Alice, Bob,
and Eve, into our vocabulary.
In both the classical and modern cryptosystems that we will study, you will
have the chance to implement the system itself and a successful attack on that
system, when one exists. This implementation is incredibly helpful, as it allows
us to see the details of a particular system in action. These exercises do require
some experience programming. Well assume here that the typical student has
no prior knowledge. Well begin with some general examples and move into
more cryptographic applications as we gain condence.
Chapter 1. Introduction
1.1
Communication Systems
$OLFH
&RPSUHVV
3URWHFW
(QFU\SW
(YH
%RE
'HFRPSUHVV
'HFU\SW
&KDQQHO
&RUUHFW
Figure 1.1: General communication system with Alice sending a message to Bob
through a public channel monitored by Eve.
1.1.1
Compression
In order to save time and energy, Alice typically wants to send as short a message
as possible while getting her point across. She can use a suite of mathematical
techniques broadly called compression to make her message as small as possible while retaining most or all of its meaning. For instance, if Alice were trying
to send the message Lets meet together tonight at 9 oclock on the Boston
1.1.2
Encryption
Since Alice wants her message to Bob to be secret, she somehow needs to make
the message she sends across the public channel unintelligible to everyone but
Bob. She can accomplish this with a number of mathematical techniques broadly
termed encryption. An encryption technique matches each character in the
original message (called the plaintext in encryption protocols) into a unique
character in the encrypted message (called the ciphertext in encryption protocols). As far as displaying these messages, the traditional approach is to
Use only capital letters
Ignore punctuation and other non-alphabetic characters
Group characters in small sets in order to minimize human errors in processing
We will ask ourselves how our cryptographic tools would have to change if we
ignored some or all of these bullet points, but for now, lets stick to the easier
case.
Example 1.1.2. Suppose Alice and Bob could have agreed that an A in plaintext
will become a B in the ciphertext. Similarly, a B would turn into a C, and so on.
Using the display conventions laid out above, Alices compressed message Meet
tonight nine pm Boston Common would read
MEETT ONIGH TNINE PMBOS TONCO MMON
Using the encryption scheme outlined above, Alices message would be transformed in the following way:
NFFUU POJHI UOJOF QNCPT UPODP NNPO
Chapter 1. Introduction
While this ciphertext looks like complete gibberish, both Alice and Bob know
that a meaningful message lies underneath. But what about our eavesdropper
Eve? We could assume that she knows nothing about how Alice and Bob have
decided to encrypt their messages. This approach is called secrecy through
obscurity; Alice and Bob keep everything about their encryption scheme secret,
and in that way keep Eve from deciphering their messages. The issue here is that
if Eve somehow manages to get an idea of how the message is being encoded,
then Alice and Bob will underestimate her capabilities, which is in general a
very bad thing to do to an adversary. Instead we typically follow Shannons
maxim (also called Kerckho s law) which says in short The enemy knows
the system. In more detail, we assume that Eve knows everything about how
the message was encrypted except for a special piece of secret information call
the key. In Example 1.1.2 for instance, we assume that Eve knows that Alice
and Bob are encrypting by substituting one letter for another, but that she does
not know the secret substitution rule. If Eve wants to break the code, she needs
to gure out, in one way or another, what the substitution rule is. Both the
encryption process, called cryptography, and the breaking of ciphers, called
cryptanalysis, are huge elds, each with their own beautiful and powerful
results. We will discuss both in the context of several cryptosystems in these
notes.
1.1.3
Protection
The nal step Alice performs before transmission is protection. The public channel across which Alice and Bob communicate is a noisy place. We assume that
Alice and Bob are not the only pair of people trying to communicate over this
space. Moreover, many channels have inherent background noise that makes
hearing and understanding a message dicult. For a physical example, imagine
Alice is trying to tell Bob a secret in a crowded restaurant. Other pairs of people
are talking, theres noise from the kitchen, and the music playing is drowning
out much of the conversation. What can Alice do to get her message across?
She could talk louder. Or perhaps she could repeat herself several times, in the
hopes that Bob could piece together her meaning. Well see that these are only
two of the most basic attempts at message protection. There are sophisticated
mathematical techniques that allow Alice to systematically introduce extra information into her message so that Bob will be able to reconstruct her meaning,
even if some of her message is corrupted during transmission.
1.1.4
Representations of Data
.
V E
21 4
In the modern cryptosystems discussed in Chapter 3, messages are traditionally represented by strings of 0s and 1s. Each 0 or 1 is called a bit (a
contraction of the phrase binary digit). We can represent text as a bit string
by representing each character as itself a bit string and then concatenating, that
is appending one after the other. Perhaps the most popular way to accomplish
this conversion is the ASCII (American Standard Code for Information Interchange). The ASCII alphabet is extensive. For instance, the ASCII bit string
for the letter A is 1000001, the ASCII bit string for the letter a is 1100001, and
the bit string for the character ! is 0100001. For a full list, check out the ASCII
Wikipedia page (click here).
The good news is that we rarely have to deal with this conversion explicitly.
Almost always it is enough to know that we can convert any string of characters
into a unique string of bits, and vice versa. We can therefore think of encoding just the bits themselves, and leave the worrying about the conversion to a
computer. Well discuss this paradigm in much more detail at the beginning of
Chapter 3.
Chapter 1. Introduction
1.2
Matlab
Matlab is an industry-standard computational package developed by MathWorks. In this section, we will cover some core computer programming ideas
and their corresponding implementation in Matlab.
1.2.1
Basic Arithmetic
At its most basic, Matlab is a high powered calculator. For instance, if we enter
1 + 2 in the command line, we observe
>> 1 + 2
ans =
3
Sometimes we dont want to show the output of a particular computation. We
can suppress the output by appending a semicolon:
>> 1 + 2;
>>
Note that Matlab has still performed the computation; it just hasnt shown us
the results. Basic arithmetic works much the way youd probably expect, with
+, -, /, *, and ^ all performing their traditional roles.
1.2.2
Variable Assignment
We often need to use the result of a computation for some other purpose. We can
store the results of a computation using variable assignment. An assignment
always has the form
variable name = variable value.
We assign the value on the right of the equals sign to the variable name on the
left of the equals sign. For example,
>> x = 1 + 2;
>> x
x =
3
The variable x now has value 3 and will continue to have this value until we
overwrite it or clear it.
Variable assignment can produce expressions that might not make sense at
rst look. For instance, consider
1.2. Matlab
>> x = 1 + 2;
>> x = 2*x;
The last line seems strange from a mathematical perspective. If we consider this
to be an equation, there is only one value of x that satises the constraint. But
this is not an equation! Its a variable assignment! So we take the value on the
right side of the equals sign and assign it to the variable name on the left side
of the equals sign. After the rst line executes, we have x = 3. The value on
the right side of the equals sign on the second line is therefore 6. So the second
line actually overwrites the old value of x with a new value of 6. While these
types of assignments are perfectly legal, and in some cases desirable, they can
also easily confuse people who are trying to read your code. Be careful using
them.
1.2.3
Arrays
One of the strongest aspects of Matlab is that it can deal with arrays and
matrices very eciently. We can generate arrays very simply.
>> a = 1:5
a =
1
To see exactly what the size function is telling us, we can type help size
in the command line.
We could turn a into a column vector b by appending a single apostrophe.
>> b = a
b =
1
2
3
4
5