You are on page 1of 9

Mar 30, 2007

An Introduction to Cryptography for Non-Cryptographers with


Mathematical Background

David J. Venable

E-Mail: davidjvenable@yahoo.com

Abstract: A brief introduction to cryptography, covering different types of cryptosystems,


including block ciphers, stream ciphers, and public key cryptosystems. Encryption and decryption
operations are discussed for each cryptosystem. This is followed by a brief discussion of digital
signatures, hash algorithms, and mathematical background. Finally, there is a real-world
example of key generation, encryption, and decryption using the RSA algorithm.

Keywords: Encryption, Decryption, Cryptographic Mathematics, RSA, Block Cipher, Stream


Cipher, Public Key Cryptosystem, Elliptic Curve, Quantum Cryptography, Key Exchange, Digital
Signatures, Cryptographic Hash Functions
Mar 30, 2007 1

Introduction

Cryptography has become an integral part of nearly everyone’s daily life. It is used to protect our
financial information from thieves, to protect our personal information from marketing companies,
and in some cases it’s even used to protect individuals’ freedoms from malicious governments.
Cryptography is, indisputably, one of the most important fields within the security profession.
Unfortunately, it also seems to be the least understood.

So how can it be that such an important technology receives such little attention in books and
classes alike? The answer is simple, there seems to be an intimidation factor within cryptography
that is not present in any other field within the security professional’s purview: mathematics—and
not the sort of mathematics with which most people are comfortable. Cryptography comes with
phrases that can sound down-right frightening. Words like “finite fields,” “Euler’s totient function,”
and “modulus.” Fortunately, these phrases describe concepts that most people have mastered
by the end of elementary school.

This article will touch on the building blocks of cryptography including the different types of
cryptosystems, their various uses, and some of the underlying mathematics involved, and will
culminate in an example usage of the RSA algorithm.

Different Types of Cryptosystems

Stream Ciphers

Stream ciphers are usually the combination of pseudo-random key information with plaintext for
encryption, or ciphertext for decryption one bit at a time. This is usually done using the XOR
operation. Because stream ciphers are typically much faster than other types of ciphers, they are
used when encrypting phone calls or network traffic. Stream ciphers are symmetric key
algorithms, meaning that the encryption key and decryption key are the same. Common stream
ciphers include RC4 and A5/1. See Illustration 1.

Illustration 1: Stream Cipher


Mar 30, 2007 2

Block Ciphers

Block ciphers typically take a block of input, perform an operation (encryption or decryption) and
output a same-sized block. For example, when encrypting, a block cipher will read a block of
plaintext, operate on it using the key, and output a block of ciphertext. When decrypting, it reads
a block of ciphertext, operates on it using the key, and outputs a block of plaintext.

Additionally, block ciphers may be run in a variety of modes which affect the operations. Some
block ciphers may even be used as stream ciphers, however they are typically slower than actual
stream ciphers. Common block ciphers include AES, DES, and IDEA.

Illustration 2: Block Cipher

Public Key Cryptography

Public key cryptosystems are unique, in that they use different keys for encryption and
decryption. This is based on a relationship between three numbers: the encryption key, the
decryption key, and the modulus. Encryption operates on plaintext using the encryption key and
the modulus to produce ciphertext. Decryption operates on ciphertext using the decryption key
and the modulus to produce plaintext. One’s public (encryption) key may be widely distributed
without fear of compromising messages encrypted with it. As long as the private (decryption) key
is kept secret, the communications are secure.

Asymmetric cryptography solves several problems inherent in symmetric cryptography, such as


key exchange over insecure channels, authentication, and non-repudiation using digital
signatures.

Public key encryption is slow. For this reason, most implementations of asymmetric encryption
use the technology to encrypt a randomly generated session key that is then used to encrypt and
decrypt the plain text with either a block or stream cipher. Common public key cryptosystems
include RSA, Diffie-Hellman, and ElGamal.
Mar 30, 2007 3

Illustration 3: Public Key Cryptosystem

Elliptic Curve Cryptography

Elliptic curve cryptography is a form of public key cryptography that uses elliptic curves to reduce
numbers instead of a modulus. One effect of using elliptic curve cryptography is that the key
lengths required are much closer to those for block and stream ciphers. At this point in time,
elliptic curve cryptography is relatively new, and has not been sufficiently analyzed for practical
use.

Quantum Cryptography

The term quantum cryptography actually refers to a key exchange method, not an actual
cryptosystem or type of cryptosystem. Quantum cryptography relies on the laws of physics to
ensure that eavesdroppers are unable to successfully gain access to the key while it is in transit.
While this type of key exchange protocol is very promising, it is not very practical for widespread
use at this time.

Key Lengths

Anyone remotely familiar with cryptography has heard talk about key length—and with good
reason. Key length is directly proportional to the security of a cryptosystem. However, like all
things in security, key length is a tradeoff. Each additional bit of a key exponentially increases the
length of time required to perform a brute force attack against it. On the other hand, each bit also
adds to the time required for encryption and decryption. It is for this reason that everyone isn’t
using keys that are millions of bits in length.

Furthermore, different types of cryptosystems require different key lengths for similar levels of
security. Most public key cryptosystems require vastly longer keys than block or stream ciphers.
For instance, the current recommended key length of RSA is 2,048 bits, while the current
recommended key length for block or stream ciphers is 128 or 256 bits.

This discrepancy is based on the different types of problems that need to be overcome to break
the encryption. Unlike block or stream ciphers, public key cryptosystems generally rely upon the
difficulty of factoring large numbers or determining discrete logarithms. While these problems are
Mar 30, 2007 4

still considered highly difficult, key lengths should increase as advancements in both the
problems themselves and computing power come about.

Digital Signatures

The advent of public key cryptography brought about great changes in the security world.
Suddenly problems that had appeared to have no solution, such as non-repudiation, were easily
overcome. Just as anyone could encrypt a message with someone else’s private key, they could
easily encrypt it with their own.

A normal use of public key encryption looks like this:


• Alice encrypts a message with Bob’s public key
• Bob decrypts the message with his own private key

A digital signature is the same operation with different keys:


• Alice encrypts a message with her own private key
• Bob decrypts the message with Alice’s public key

As long as Alice’s private key has not been compromised, the message can be validated as
having come from Alice.

Another, more common, way to accomplish this is to take a cryptographic hash of the message,
and encrypt it with the sender’s private key.

One-Way Hash Functions

One-way hash functions, or cryptographic hashes, are often used in digital signatures, and have
the following attributes:
• No two messages produce the same hash
• It is infeasible to derive the original message from a hash
• It is infeasible to produce a message that hashes to a given value

In this case, Alice hashes a message, and encrypts the hash with her private key. This signature
is then appended to the message. Hashes, like keys, are measured in bits. Common
cryptographic hash functions include MD5, SHA-1, and SHA-256. Due to problems discovered in
MD5 and SHA-1, it is recommended to use stronger hash functions with larger hashes, such as
SHA-256.
Mar 30, 2007 5

A Brief Introduction to the Mathematics of Cryptography

Modular Arithmetic

Most people first encountered this somewhere around second or third grade, only then it was
called “clock math.” For instance, if Arthur left the house at 10:00pm and was gone for four
hours, what time was it when he got back home? The answer to this question is easy: 2:00am.

So how was it solved? Subtract 12 (our modulus) from 10 + 4.

But what about a problem like this: Charlie, who has an abnormally long life span, left the house
at 10:00pm and was gone for 1,327,495 hours, what time was it when he got back home? If we
solved this problem the same way, we’d be subtracting a LOT of 12s.

A much quicker way is to divide by 12 (once again, our modulus) and take the remainder. For
instance, in the first problem (10 + 2) divided by 12 gives us 1 with a remainder of 2—the time
that Arthur returned home. In this case, (10 + 1,327,495) divided by 12 gives us 110,625 with a
remainder of 5. So, Charlie got back home at 5:00am some 151 years later.

This is usually written as:

10 + 4 ≡ 2 mod 12

Euler’s Totient Function

Euler’s totient function (written as φ(n)), returns the number of positive integers less than n that
are relatively prime to n. Relatively prime simply means that the numbers do not have common
factors. For example, φ(12) = 4 because of the positive numbers less than 12 (1-11), only 1, 5, 7,
and 11 do not share common factors with 12. Note that if n is prime, φ(n) = n-1, therefore φ
(11)=10.

A Quick Look at RSA

The RSA algorithm is the most widely used public key encryption algorithm. It was developed by
three MIT mathematicians: Ron Rivest, Adi Shamir, and Len Adleman.

Encryption in RSA is merely exponentiation of the message by the encryption key and then
reduced by the modulus:

c ≡ me mod n

Where c represents the ciphertext, m represents the message, e represents the encryption key,
and n represents the modulus.
Mar 30, 2007 6

Decryption in RSA is merely the exponentiation of the ciphertext by the decryption key and then
reduced by the modulus:

p ≡ cd mod n

Where p represents the plaintext, c represents the ciphertext, d represents the decryption key,
and n represents the modulus.

RSA Key Generation

The first step of generating an RSA key pair involves selecting two large prime numbers (p and
q). These two primes are then multiplied together to produce the number that will be used as the
modulus:

n = pq

In RSA, the key length expresses the size of the modulus.

As both p and q are primes, φ(n) = (p-1)(q-1).

Once the modulus has been produced, an encryption key can be selected. This is usually a small
prime number. The decryption key may then be derived:

ed ≡ 1 mod φ(n)

An Example

Alice wants to generate an RSA key pair. First she selects two prime numbers:

p = 17
q = 19

She then calculates her modulus:

n = pq
n = 17 x 19
n = 323

Her next step is to calculate φ(n). In this case

φ(n) = (p-1)(q-1)
φ(n) = (17-1)(19-1)
φ(n) = 288.

Alice now picks an encryption key:

e=5
Mar 30, 2007 7

The last step to key generation is for Alice to derive her decryption key:

ed ≡ 1 mod φ(n)
5d ≡ 1 mod 288

This can, more easily be solved as:

5d – k288 = 1

Where one possible solution is:

5d – (3)288 = 1
5d – 864 = 1

Therefore, her decryption key is 173:

5 (173) – 864 = 1

Armed with her encryption key (5), decryption key (173), and modulus (323), Alice is ready to
communicate securely. She then sends her public key, which is her encryption key and modulus,
to Bob.

So, if Bob wants to send Alice a message, in this case 65 (the ASCII code for “A”):

m = 65

c ≡ me mod n
c ≡ 655 mod 323
c ≡ 1,160,290,625 mod 323
c = 12

Alice then decrypts:

m ≡ cd mod n
m ≡ 12173 mod 323

m≡
4,992,931,021,747,500,841,206,051,466,436,702,562,341,442,456,351,521,960,079,288,222,88
1,342,911,294,018,652,359,351,672,377,890,893,353,133,194,676,518,844,878,848,016,023,53
3,977,265,194,326,456,395,641,394,281,060,389,897,280,283,150,355,791,872
mod 323

m = 65

Note that the key length used in this example is 9 bits.

Selecting a Cryptosystem

When selecting a cryptographic algorithm, there are several factors to take into account:
Mar 30, 2007 8

Has the algorithm been subjected to extensive peer review?

Peer review is one of the most important aspects of selecting a cryptosystem. If an algorithm has
not been looked at by numerous professional cryptographers who have attempted to break it,
then its security is unknown at best.

Is the algorithm openly available?

Time has taught that if a company is selling products that use encryption algorithms that are not
publicly available, it is most likely snake oil. The security of a cryptosystem should always rely on
keeping the key, not the algorithm, a secret.

Is the algorithm patent-free?

While there are several good algorithms that are patented, there are more that are not.
Furthermore, there is no gain in using a patented algorithm over a non-patented algorithm of
equal security. The only difference between the two is their effect on the pocketbook.

Does the algorithm allow for sufficient key length?

Cryptosystems should remain secure for several years beyond the usefulness of the data they
protect. As computing power increases, the necessary key length increases as well. At this point
in time, block and stream ciphers should be moving toward using 256 bit keys, although 128 bit
keys are still considered secure. Public key systems should be moving toward using 2,048 bit
keys.

Implementations

The majority of cryptographic algorithms in use today have been subjected to extensive peer-
review, and are widely held to be secure. However, there are several other factors involved in the
security of a cryptosystem, one of the most important being the implementation. A simple
mistake in a cryptographic implementation can wreak havoc on the security of the system. It is
for this reason that it is widely held by cryptographers that cryptographic code, as well as the
algorithm, should be publicly available and subjected to peer review. Most commercially
available cryptosystems in use today hold true to this philosophy and publish their cryptographic
code.

As our society is building towards a more secure future, cryptography will be one of the primary
components. It is important for security professionals to have a solid understanding of the
technologies and techniques involved in order to lead the way.

You might also like