{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Part 6: Ciphers and Key exchange in Python 3.x"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this notebook, we introduce cryptography -- how to communicate securely over insecure channels. We begin with a study of two basic ciphers, the Caesar cipher and its fancier variant, the Vigenère cipher. The Vigenère cipher uses a **key** to turn **plaintext** (i.e., the message) into **ciphertext** (the coded message), and uses the same key to turn the ciphertext back into plaintext. Therefore, two parties can communicate securely if they -- and only they -- possess the key. \n",
"\n",
"If the security of communication rests on possession of a common key, then we're left with a new problem: how do the two parties agree on a common key, especially if they are far apart and communicating over an insecure channel? \n",
"\n",
"A clever solution to this problem was published in 1976 by Whitfield Diffie and Martin Hellman, and so it's called Diffie-Hellman key exchange. It takes advantage of modular arithmetic: the existence of a primitive root (modulo a prime) and the difficulty of solving the **discrete logarithm** problem. \n",
"\n",
"This part complements Chapter 6 of [An Illustrated Theory of Numbers](http://illustratedtheoryofnumbers.com)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Table of Contents\n",
"\n",
"- [Ciphers](#cipher)\n",
"- [Key exchange](#keyexchange)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Ciphers"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A cipher is a way of transforming a message, called the **plaintext** into a different form, the **ciphertext**, which conceals the meaning to all but the intended recipient(s). A cipher is a *code*, and can take many forms. A **substitution cipher** might simply change every letter to a different letter in the alphabet. This is the idea behind \"Cryptoquip\" puzzles. These are not too hard for people to solve, and are easy for computers to solve, using frequency analysis (understanding how often different letters or letter-combinations occur).\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### ASCII code and the Caesar cipher\n",
"\n",
"Even though substitution ciphers are easy to break, they are a good starting point. To implement substitution ciphers in Python, we need to study the *string* type in a bit more detail. To declare a string variable, just put your string in quotes. You can use any letters, numbers, spaces, and many symbols inside a string. You can enclose your string by single quotes, like `'Hello'` or double-quotes, like `\"Hello\"`. This flexibility is convenient, if you want to *use* quotes within your string. For example, the string *Prince's favorite prime is 1999* should be described in Python with double-quotes `\"Prince's favorite prime is 1999\"` so that the apostrophe doesn't confuse things. \n",
"\n",
"Strings are indexed, and their letters can be retrieved as if the string were a *list* of letters. Python experts will note that strings are *immutable* while lists are *mutable* objects, but we aren't going to worry about that here."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"W = \"Hello\"\n",
"print(W)\n",
"for j in range(len(W)): # len(W) is the length of the string W.\n",
" print(W[j]) # Access the jth character of the string."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Each \"letter\" of a string again belongs to the string type. A string of length one is called a **character**. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(type(W))\n",
"print(type(W[0])) # W[0] is a character."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Since computers store data in binary, the designers of early computers (1960s) created a code called [**ASCII**](https://en.wikipedia.org/wiki/ASCII) (American Standard Code for Information Interchange) to associate to each character a number between 0 and 127. Every number between 0 and 127 is represented in binary by 7 bits (between 0000000 and 1111111), and so each character is stored with 7 bits of memory. Later, ASCII was extended with another 128 characters, so that codes between 0 and 255 were used, requiring 8 bits. 8 bits of memory is called a **byte**. One byte of memory suffices to store one (extended ASCII) character.\n",
"\n",
"You might notice that there are 256 ASCII codes available, but there are fewer than 256 characters available on your keyboard, even once you include symbols like `#` and `;`. Some of these \"extra\" codes are for accented letters, and others are relics of old computers. For example, ASCII code 7 (0000111) stands for the \"bell\", and readers born in the 1970s or earlier might remember making the Apple II computer beep by pressing Control-G on the keyboard (\"G\" is the 7th letter). You can look up a [full ASCII table](http://www.asciitable.com/) if you're curious. \n",
"\n",
"Nowadays, the global community of computer users requires far more than 256 \"letters\" -- there are many alphabets around the world! So instead of ASCII, we can access over 100 thousand **unicode** characters. Scroll through a [unicode table](https://unicode-table.com/en/) to see what is possible. With emoji, unicode tables have entered [unexpected territory](https://xkcd.com/1813/). Python version 3.x fully supports Unicode in all strings.\n",
"\n",
"But here we stay within old-fashioned ASCII codes, since they will suffice for basic English messages. Python has built-in commands `chr` and `ord` for converting from code-number (0--255) to character and back again."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"chr(65)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ord('A')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The following code will produce a table of the ASCII characters with codes between 32 and 126. This is a good range which includes all the most common English characters and symbols on a U.S. keyboard. Note that ASCII code 32 corresponds to an empty space (an important character for long messages!)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for a in range(32,127):\n",
" c = chr(a)\n",
" print(\"ASCII {} is {}\".format(a, c))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Since we only work with the ASCII range between 32 and 126, it will be useful to \"cycle\" other numbers into this range. For example, we will interpret 127 as 32, 128 as 33, etc., when we convert out-of-range numbers into characters.\n",
"\n",
"The following function forces a number into a given range, using the mod operator. It's a common trick, to make lists loop around cyclically."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def inrange(n,range_min, range_max):\n",
" '''\n",
" The input number n can be any integer.\n",
" The output number will be between range_min and range_max (inclusive)\n",
" If the input number is already within range, it will not change.\n",
" '''\n",
" range_len = range_max - range_min + 1\n",
" a = n % range_len\n",
" if a < range_min:\n",
" a = a + range_len\n",
" return a"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"inrange(13,1,10)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"inrange(17,5,50)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we can implement a **substitution cipher** by converting characters to their ASCII codes, shuffling the codes, and converting back. One of the simplest substitution ciphers is called a **Caesar cipher**, in which each character is shifted -- by a fixed amount -- down the list. For example, a Caesar cipher of shift 3 would send 'A' to 'D' and 'B' to 'E', etc.. Near the end of the list, characters are shifted back to the beginning -- the list is considered cyclicly, using our `inrange` function. \n",
"\n",
"Here is an implementation of the Caesar cipher, using the ASCII range between 32 and 126. We begin with a function to shift a single character."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def Caesar_shift(c, shift):\n",
" '''\n",
" Shifts the character c by shift units\n",
" within the ASCII table between 32 and 126. \n",
" The shift parameter can be any integer!\n",
" '''\n",
" ascii = ord(c)\n",
" a = ascii + shift # Now we have a number between 32+shift and 126+shift.\n",
" a = inrange(a,32,126) # Put the number back in range.\n",
" return chr(a)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's see the effect of the Caesar cipher on our ASCII table."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for a in range(32,127):\n",
" c = chr(a)\n",
" print(\"ASCII {} is {}, which shifts to {}\".format(a, c, Caesar_shift(c,5))) # Shift by 5."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we can use the Caesar cipher to encrypt *strings*."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def Caesar_cipher(plaintext, shift):\n",
" ciphertext = ''\n",
" for c in plaintext: # Iterate through the characters of a string.\n",
" ciphertext = ciphertext + Caesar_shift(c,shift) \n",
" return ciphertext"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(Caesar_cipher('Hello! Can you read this?', 5)) # Shift forward 5 units in ASCII."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As designed, the Caesar cipher turns *plaintext* into *ciphertext* by using a shift of the ASCII table. To *decipher* the ciphertext, one can just use the Caesar cipher again, with the negative shift."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(Caesar_cipher('Mjqqt&%%Hfs%~tz%wjfi%ymnxD', -5)) # Shift back 5 units in ASCII."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### The Vigenère cipher\n",
"\n",
"The Caesar cipher is pretty easy to break, by a brute force attack (shift by all possible values) or a frequency analysis (compare the frequency of characters in a message to the frequency of characters in typical English messages, to make a guess). \n",
"\n",
"The Vigenère cipher is a variant of the Caesar cipher which uses an **ecryption key** to vary the shift-parameter throughout the encryption process. For example, to encrypt the message \"This is very secret\" using the key \"Key\", you line up the characters of the message above repeated copies of the key.\n",
"\n",
"T | h | i | s | | i | s | | v | e | r | y | | s | e | c | r | e | t\n",
"--|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|--\n",
"K | e | y | K | e | y | K | e | y | K | e | y | K | e | y | K | e | y | K\n",
"\n",
"Then, you turn everything into ASCII (or your preferred numerical system), and use the bottom row to shift the top row.\n",
"\n",
"ASCII message | 84 | 104 | 105 | 115 | 32 | 105 | 115 | 32 | 118 | 101 | 114 | 121 | 32 | 115 | 101 | 99 | 114 | 101 | 116 \n",
"---|-----|-----\n",
"Shift | 75 | 101 | 121 | 75 | 101 | 121 | 75 | 101 | 121 | 75 | 101 | 121 | 75 | 101 | 121 | 75 | 101 | 121 | 75 \n",
"ASCII shifted | 159 | 205 | 226 | 190 | 133 | 226 | 190 | 133 | 239 | 176 | 215 | 242 | 107 | 216 | 222 | 174 | 215 | 222 | 191 \n",
"ASCII shifted in range | 64 | 110 | 36 | 95 | 38 | 36 | 95 | 38 | 49 | 81 | 120 | 52 | 107 | 121 | 32 | 79 | 120 | 32 | 96 \n",
"\n",
"Finally, the shifted ASCII codes are converted back into characters for transmission. In this case, the codes 64,110,36,95, etc., are converted to the ciphertext ``\"@n$_&$_&1Qx4ky Ox \\`\"``\n",
"\n",
"The Vigenère cipher is much harder to crack than the Caesar cipher, if you don't have the key. Indeed, the varying shifts make frequency analysis more difficult. The Vigenère cipher is weak by today's standards (see [Wikipedia](https://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher#Cryptanalysis) for a description of 19th century attacks), but illustrates the basic actors in a **symmetric key** cryptosystem: the plaintext, ciphertext, and a single key. Today, symmetric key cryptosystems like [AES](https://en.wikipedia.org/wiki/Advanced_Encryption_Standard) and [3DES](https://en.wikipedia.org/wiki/Triple_DES) are used all the time for secure communication.\n",
"\n",
"Below, we implement the Vigenère cipher."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def Vigenere_cipher(plaintext, key):\n",
" ciphertext = '' # Start with an empty string\n",
" for j in range(len(plaintext)): \n",
" c = plaintext[j] # the jth letter of the plaintext\n",
" key_index = j % len(key) # Cycle through letters of the key.\n",
" shift = ord(key[key_index]) # How much we shift c by.\n",
" ciphertext = ciphertext + Caesar_shift(c,shift) # Add new letter to ciphertext\n",
" return ciphertext"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(Vigenere_cipher('This is very secret', 'Key')) # 'Key' is probably a bad key!!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The Vigenère cipher is called a **symmetric** cryptosystem, because the same key that is used to encrypt the plaintext can be used to decrypt the ciphertext. All we do is *subtract* the shift at each stage."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def Vigenere_decipher(ciphertext, key):\n",
" plaintext = '' # Start with an empty string\n",
" for j in range(len(ciphertext)): \n",
" c = ciphertext[j] # the jth letter of the ciphertext\n",
" key_index = j % len(key) # Cycle through letters of the key.\n",
" shift = - ord(key[key_index]) # Note the negative sign to decipher!\n",
" plaintext = plaintext + Caesar_shift(c,shift) # Add new letter to plaintext\n",
" return plaintext"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(Vigenere_decipher('@n$_&$_&1Qx4ky Ox `', 'Key'))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Try a few cipher/deciphers yourself to get used to the Vigenere system.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The Vigenère cipher becomes an effective way for two parties to communicate securely, as long as they **share a secret key**. In the 19th century, this often meant that the parties would require an initial in-person meeting to agree upon a key, or a well-guarded messenger would carry the key from one party to the other. \n",
"\n",
"Today, as we wish to communicate securely over long distances on a regular basis, the process of agreeing on a key is more difficult. It seems like a chicken-and-egg problem, where we need a shared secret to communicate securely, but we can't share a secret without communicating securely in the first place! \n",
"\n",
"Remarkably, this secret-sharing problem can be solved with some modular arithmetic tricks. This is the subject of the next section."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercises\n",
"\n",
"1. A Caesar cipher was used to encode a message, with the resulting ciphertext: `'j!\\'1r$v1\"$v&&+1t}v(v$2'`. Use a loop (brute force attack) to figure out the original message. \n",
"\n",
"2. Imagine that you encrypt a long message (e.g., 1000 words of standard English) with a Vigenère cipher. How might you detect the *length* of the key, if it is short (e.g. 3 or 4 characters)?\n",
"\n",
"3. Consider running a plaintext message through a Vigenère cipher with a 3-character key, and then running the ciphertext through a Vigenère cipher with a 4-character key. Explain how this is equivalent to running the original message through a single cipher with a 12-character key."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Key exchange"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"Now we study Diffie-Hellman **key exchange**, a remarkable way for two parties to share a secret without ever needing to directly communicate the secret with each other. Their method is based on properties of modular exponentiation and the existence of a **primitive root** modulo prime numbers. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Primitive roots and Sophie Germain primes\n",
"\n",
"If $p$ is a prime number, and $GCD(a,p) = 1$, then recall Fermat's Little Theorem: $$a^{p-1} \\equiv 1 \\text{ mod } p.$$\n",
"It may be the case that $a^\\ell \\equiv 1$ mod $p$ for some smaller (positive) value of $\\ell$ however. The smallest such positive value of $\\ell$ is called the **order** (multiplicative order, to be precise) of $a$ modulo $p$, and it is always a divisor of $p-1$.\n",
"\n",
"The following code determines the order of a number, mod $p$, with a brute force approach."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def mult_order(a,p):\n",
" '''\n",
" Determines the (multiplicative) order of an integer\n",
" a, modulo p. Here p is prime, and GCD(a,p) = 1.\n",
" If bad inputs are used, this might lead to a \n",
" never-ending loop!\n",
" '''\n",
" current_number = a % p\n",
" current_exponent = 1\n",
" while current_number != 1:\n",
" current_number = (current_number * a)%p\n",
" current_exponent = current_exponent + 1\n",
" return current_exponent\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for j in range(1,37):\n",
" print(\"The multiplicative order of {} modulo 37 is {}\".format(j,mult_order(j,37)))\n",
" # These orders should all be divisors of 36."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A theorem of Gauss states that, if $p$ is prime, there exists an integer $b$ whose order is precisely $p-1$ (as big as possible!). Such an integer is called a **primitive root** modulo $p$. For example, the previous computation found 12 primitive roots modulo $37$: they are 2,5,13,15,17,18,19,20,22,24,32,35. To see these illustrated (mod 37), check out [this poster](https://fineartamerica.com/featured/epicycles-modulo-37-martin-weissman.html) (yes, that is blatant self-promotion!)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For everything that follows, suppose that $p$ is a prime number. Not only do primitive roots *exist* mod $p$, but they are pretty common. In fact, the number of primitive roots mod $p$ equals $\\phi(p-1)$, where $\\phi$ denotes Euler's totient. On average, $\\phi(n)$ is about $6 / \\pi^2$ times $n$ (for positive integers $n$). While numbers of the form $p-1$ are not \"average\", one still expects that $\\phi(p-1)$ is a not-very-small fraction of $p-1$. You should not have to look very far if you want to *find* a primitive root."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The more difficult part, in practice, is determining whether a number $b$ is or is not a primitive root modulo $p$. When $p$ is very large (like hundreds or thousands of digits), $p-1$ is also very large. It is certainly not practical to cycle all the powers (from $1$ to $p-1$) of $b$ to determine whether $b$ is a primitive root!\n",
"\n",
"The better approach, sometimes, is to use the fact that the multiplicative order of $b$ must be a **divisor** of $p-1$. If one can find all the divisors of $p-1$, then one can just check whether $b^d \\equiv 1$ mod $p$ for each divisor $d$. This makes the problem of determining whether $b$ is a primitive root just about as hard as the problem of factoring $p-1$. This is a hard problem, in general!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"But, for the application we're interested in, we will want to have a large prime number $p$ **and** a primitive root mod $p$. The easiest way to do this is to use a **Sophie Germain** prime $q$. A Sophie Germain prime is a prime number $q$ such that $2q + 1$ is also prime. When $q$ is a Sophie Germain prime, the resulting prime $p = 2q + 1$ is called a **safe prime**.\n",
"\n",
"Observe that when $p$ is a safe prime, the prime decomposition of $p-1$ is \n",
"$$p-1 = 2 \\cdot q.$$\n",
"That's it. So the possible multiplicative orders of an element $b$, mod $p$, are the divisors of $2q$, which are\n",
"$$1, 2, q, \\text{ or } 2q.$$\n",
"In order to check whether $b$ is a primitive root, modulo a safe prime $p = 2q + 1$, we must check just three things: is $b \\equiv 1$, is $b^2 \\equiv 1$, or is $b^q \\equiv 1$, mod $p$? If the answer to these three questions is NO, then $b$ is a primitive root mod $p$."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def is_primroot_safe(b,p):\n",
" '''\n",
" Checks whether b is a primitive root modulo p,\n",
" when p is a safe prime. If p is not safe,\n",
" the results will not be good!\n",
" '''\n",
" q = (p-1) // 2 # q is the Sophie Germain prime\n",
" if b%p == 1: # Is the multiplicative order 1?\n",
" return False\n",
" if (b*b)%p == 1: # Is the multiplicative order 2?\n",
" return False\n",
" if pow(b,q,p) == 1: # Is the multiplicative order q?\n",
" return False\n",
" return True # If not, then b is a primitive root mod p."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This would not be very useful if we couldn't find Sophie Germain primes. Fortunately, they are not so rare. The first few are 2, 3, 5, 11, 23, 29, 41, 53, 83, 89. It is expected, but unproven that there are infinitely many Sophie Germain primes. In practice, they occur fairly often. If we consider numbers of magnitude $N$, about $1 / \\log(N)$ of them are prime. Among such primes, we expect about $1.3 / \\log(N)$ to be Sophie Germain primes. In this way, we can expect to stumble upon Sophie Germain primes if we search for a bit (and if $\\log(N)^2$ is not too large).\n",
"\n",
"The code below tests whether a number $p$ is a Sophie Germain prime. We construct it by simply testing whether $p$ and $2p+1$ are both prime. We use the Miller-Rabin test (the code from the previous Python notebook) in order to test whether each is prime."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from random import randint # randint chooses random integers.\n",
"\n",
"def Miller_Rabin(p, base):\n",
" '''\n",
" Tests whether p is prime, using the given base.\n",
" The result False implies that p is definitely not prime.\n",
" The result True implies that p **might** be prime.\n",
" It is not a perfect test!\n",
" '''\n",
" result = 1\n",
" exponent = p-1\n",
" modulus = p\n",
" bitstring = bin(exponent)[2:] # Chop off the '0b' part of the binary expansion of exponent\n",
" for bit in bitstring: # Iterates through the \"letters\" of the string. Here the letters are '0' or '1'.\n",
" sq_result = result*result % modulus # We need to compute this in any case.\n",
" if sq_result == 1:\n",
" if (result != 1) and (result != exponent): # Note that exponent is congruent to -1, mod p.\n",
" return False # a ROO violation occurred, so p is not prime\n",
" if bit == '0':\n",
" result = sq_result \n",
" if bit == '1':\n",
" result = (sq_result * base) % modulus\n",
" if result != 1:\n",
" return False # a FLT violation occurred, so p is not prime.\n",
" \n",
" return True # If we made it this far, no violation occurred and p might be prime.\n",
"\n",
"def is_prime(p, witnesses=50): # witnesses is a parameter with a default value.\n",
" '''\n",
" Tests whether a positive integer p is prime.\n",
" For p < 2^64, the test is deterministic, using known good witnesses.\n",
" Good witnesses come from a table at Wikipedia's article on the Miller-Rabin test,\n",
" based on research by Pomerance, Selfridge and Wagstaff, Jaeschke, Jiang and Deng.\n",
" For larger p, a number (by default, 50) of witnesses are chosen at random.\n",
" '''\n",
" if (p%2 == 0): # Might as well take care of even numbers at the outset!\n",
" if p == 2:\n",
" return True\n",
" else:\n",
" return False \n",
" \n",
" if p > 2**64: # We use the probabilistic test for large p.\n",
" trial = 0\n",
" while trial < witnesses:\n",
" trial = trial + 1\n",
" witness = randint(2,p-2) # A good range for possible witnesses\n",
" if Miller_Rabin(p,witness) == False:\n",
" return False\n",
" return True\n",
" \n",
" else: # We use a determinisic test for p <= 2**64.\n",
" verdict = Miller_Rabin(p,2)\n",
" if p < 2047:\n",
" return verdict # The witness 2 suffices.\n",
" verdict = verdict and Miller_Rabin(p,3)\n",
" if p < 1373653:\n",
" return verdict # The witnesses 2 and 3 suffice.\n",
" verdict = verdict and Miller_Rabin(p,5)\n",
" if p < 25326001:\n",
" return verdict # The witnesses 2,3,5 suffice.\n",
" verdict = verdict and Miller_Rabin(p,7)\n",
" if p < 3215031751:\n",
" return verdict # The witnesses 2,3,5,7 suffice.\n",
" verdict = verdict and Miller_Rabin(p,11)\n",
" if p < 2152302898747:\n",
" return verdict # The witnesses 2,3,5,7,11 suffice.\n",
" verdict = verdict and Miller_Rabin(p,13)\n",
" if p < 3474749660383:\n",
" return verdict # The witnesses 2,3,5,7,11,13 suffice.\n",
" verdict = verdict and Miller_Rabin(p,17)\n",
" if p < 341550071728321:\n",
" return verdict # The witnesses 2,3,5,7,11,17 suffice.\n",
" verdict = verdict and Miller_Rabin(p,19) and Miller_Rabin(p,23)\n",
" if p < 3825123056546413051:\n",
" return verdict # The witnesses 2,3,5,7,11,17,19,23 suffice.\n",
" verdict = verdict and Miller_Rabin(p,29) and Miller_Rabin(p,31) and Miller_Rabin(p,37)\n",
" return verdict # The witnesses 2,3,5,7,11,17,19,23,29,31,37 suffice for testing up to 2^64. \n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def is_SGprime(p):\n",
" '''\n",
" Tests whether p is a Sophie Germain prime\n",
" '''\n",
" if is_prime(p): # A bit faster to check whether p is prime first.\n",
" if is_prime(2*p + 1): # and *then* check whether 2p+1 is prime.\n",
" return True\n",
" return False"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's test this out by finding the Sophie Germain primes up to 100, and their associated safe primes."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for j in range(1,100):\n",
" if is_SGprime(j):\n",
" print(j, 2*j+1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Next, we find the first 100-digit Sophie Germain prime! This might take a minute!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"test_number = 10**99 # Start looking at the first 100-digit number, which is 10^99.\n",
"while not is_SGprime(test_number):\n",
" test_number = test_number + 1\n",
"print(test_number)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In the seconds or minutes your computer was running, it checked the primality of almost 90 thousand numbers, each with 100 digits. Not bad!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### The Diffie-Hellman protocol\n",
"\n",
"When we study protocols for secure communication, we must keep track of the communicating parties (often called Alice and Bob), and **who** has knowledge of **what** information. We assume at all times that the \"wire\" between Alice and Bob is tapped -- anything they say to each other is actively monitored, and is therefore **public** knowledge. We also assume that what happens on Alice's private computer is private to Alice, and what happens on Bob's private computer is private to Bob. Of course, these last two assumptions are big assumptions -- they point towards the danger of computer viruses which infect computers and can violate such privacy!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The goal of the Diffie-Hellman protocol is -- at the end of the process -- for Alice and Bob to **share a secret** without ever having communicated the secret with each other. The process involves a series of modular arithmetic calculations performed on each of Alice and Bob's computers.\n",
"\n",
"The process begins when Alice or Bob creates and publicizes a **large prime number** `p` and a **primitive root** `g` modulo `p`. It is best, for efficiency and security, to choose a **safe** prime `p`. Alice and Bob can create their own safe prime, or choose one from a public list online, e.g., from the [RFC 3526 memo](https://tools.ietf.org/html/rfc3526). Nowadays, it's common to take `p` with 2048 bits, i.e., a prime which is between $2^{2046}$ and $2^{2047}$ (a number with 617 decimal digits!)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For the purposes of this introduction, we use a smaller safe prime, with about 256 bits. We use the `SystemRandom` functionality of the `random` package to create a good random prime. It is not so much of an issue here, but in general one must be very careful in cryptography that one's \"random\" numbers are really \"random\"! The `SystemRandom` function uses chaotic properties of your computer's innards in order to initialize a random number generator, and is considered cryptographically secure."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from random import SystemRandom # Import the necessary package.\n",
"\n",
"r = SystemRandom().getrandbits(256)\n",
"print(\"The random integer is {}\".format(r))\n",
"print(\"with binary expansion {}\".format(bin(r))) # r is an integer constructed from 256 random bits.\n",
"print(\"with bit-length {}.\".format(len(bin(r)) - 2)) # In case you want to check. Remember '0b' is at the beginning."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def getrandSGprime(bitlength):\n",
" '''\n",
" Creates a random Sophie Germain prime p with about \n",
" bitlength bits.\n",
" '''\n",
" while True:\n",
" p = SystemRandom().getrandbits(bitlength) # Choose a really random number.\n",
" if is_SGprime(p):\n",
" return p "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The function above searches and searches among random numbers until it finds a Sophie Germain prime. The (possibly endless!) search is performed with a `while True:` loop that may look strange. The idea is to stay in the loop until such a prime is found. Then the `return p` command returns the found prime as output and halts the loop. One must be careful with `while True` loops, since they are structured to run forever -- if there's not a loop-breaking command like `return` or `break` inside the loop, your computer will be spinning for a long time."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"q = getrandSGprime(256) # A random ~256 bit Sophie Germain prime\n",
"p = 2*q + 1 # And its associated safe prime\n",
"\n",
"print(\"p is \",p) # Just to see what we're working with.\n",
"print(\"q is \",q)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Next we find a primitive root, modulo the safe prime `p`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def findprimroot_safe(p):\n",
" '''\n",
" Finds a primitive root, \n",
" modulo a safe prime p.\n",
" '''\n",
" b = 2 # Start trying with 2.\n",
" while True: # We just keep on looking.\n",
" if is_primroot_safe(b,p):\n",
" return b\n",
" b = b + 1 # Try the next base. Shouldn't take too long to find one!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"g = findprimroot_safe(p)\n",
"print(g)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The pair of numbers $(g, p)$, the primitive root and the safe prime, chosen by either Alice or Bob, is now made public. They can post their $g$ and $p$ on a public website or shout it in the streets. It doesn't matter. They are just tools for their secret-creation algorithm below.\n",
"\n",
"### Alice and Bob's private secrets\n",
"\n",
"Next, Alice and Bob invent **private** secret numbers $a$ and $b$. They do not tell *anyone* these numbers. Not each other. Not their family. Nobody. They don't write them on a chalkboard, or leave them on a thumbdrive that they lose. These are really secret.\n",
"\n",
"But they don't use their phone numbers, or social security numbers. It's best for Alice and Bob to use a secure random number generator on their separate private computers to create $a$ and $b$. They are often 256 bit numbers in practice, so that's what we use below."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"a = SystemRandom().getrandbits(256) # Alice's secret number\n",
"b = SystemRandom().getrandbits(256) # Bob's secret number\n",
"\n",
"print(\"Only Alice should know that a = {}\".format(a))\n",
"print(\"Only Bob should know that b = {}\".format(b))\n",
"\n",
"print(\"But everyone can know p = {} and g = {}\".format(p,g))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now Alice and Bob use their secrets to generate new numbers. Alice computes the number \n",
"$$A = g^a \\text{ mod } p,$$\n",
"and Bob computes the number\n",
"$$B = g^b \\text{ mod } p.$$\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"A = pow(g,a,p) # This would be computed on Alice's computer.\n",
"B = pow(g,b,p) # This would be computed on Bob's computer."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now Alice and Bob do something that seems very strange at first. Alice sends Bob her new number $A$ and Bob sends Alice his new number $B$. Since they are far apart, and the channel is insecure, we can assume everyone in the world now knows $A$ and $B$."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(\"Everyone knows A = {} and B = {}.\".format(A,B))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now Alice, on her private computer, computes $B^a$ mod $p$. She can do that because everyone knows $B$ and $p$, and she knows $a$ too.\n",
"\n",
"Similarly, Bob, on his private computer, computes $A^b$ mod $p$. He can do that because everyone knows $A$ and $p$, and he knows $b$ too.\n",
"\n",
"Alice and Bob **do not** share the results of their computations!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(pow(B,a,p)) # This is what Alice computes."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(pow(A,b,p)) # This is what Bob computes."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Woah! What happened? In terms of exponents, it's elementary. For\n",
"$$B^a = (g^{b})^a = g^{ba} = g^{ab} = (g^a)^b = A^b.$$\n",
"So these two computations yield the same result (mod $p$, the whole way through).\n",
"\n",
"In the end, we find that Alice and Bob **share a secret**. We call this secret number $S$.\n",
"$$S = B^a = A^b.$$"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"S = pow(B,a,p) # Or we could have used pow(A,b,p)\n",
"print(S)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This common secret $S$ can be used as a key for Alice and Bob to communicate hereafter. For example, they might use $S$ (converted to a string, if needed) as the key for a Vigenère cipher, and chat with each other knowing that only they have the secret key to encrypt and decrypt their messages."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# We use the triple-quotes for a long string, that occupies multiple lines.\n",
"# The backslash at the end of the line tells Python to ignore the newline character.\n",
"# Imagine that Alice has a secret message she wants to send to Bob. \n",
"# She writes the plaintext on her computer. \n",
"\n",
"plaintext = '''Did you hear that the American Mathematical Society has an annual textbook sale? \\\n",
" It's 40 percent off for members and 25 percent off for everyone else.'''"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Now Alice uses the secret S (as a string) to encrypt. \n",
"ciphertext = Vigenere_cipher(plaintext, str(S))\n",
"print(ciphertext)\n",
"# Alice sends the following ciphertext to Bob, over an insecure channel."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# When Bob receives the ciphertext, he decodes it with the secret S again.\n",
"print(Vigenere_decipher(ciphertext, str(S)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To have confidence in this protocol, one needs to be convinced that their secret is truly secret! The public has a lot of information: they know \n",
"1. the prime $p$, \n",
"2. the primitive root $g$, \n",
"3. the number $A = g^a$ (mod $p$), and \n",
"4. the number $B = g^b$ (mod $p$). \n",
"\n",
"If the public could **figure out** either $a$ or $b$, they could figure out the secret (by raising $A^b$ or $B^a$ like Alice and Bob did). \n",
"\n",
"This is the essence of the **discrete logarithm problem**. If we know the value of $g^a$ mod $p$, can we figure out the possible value(s) of $a$? If this were ordinary arithmetic, we would say that $a = \\log_g(A)$. But this is modular arithmetic, and there's no easy way to figure out such logarithms. The values of $g^a$ tend to bounce all over the place, mod $p$, especially since we chose $a$ to be pretty large (256 bits!). \n",
"\n",
"The security of the Diffie-Hellman protocol, i.e., the security of Alice and Bob's shared secret, depends on the *difficulty* of the discrete logarithm problem. When $p$ is a large (e.g. 2048 bits) safe prime, and $a$ and $b$ are suitably large (roughly 256 bits), there seems to be no way to solve the discrete logarithm problem mod $p$ in any reasonable amount of time. Someday we might have quantum computers to quickly solve discrete logarithm problems, and the Diffie-Hellman protocol will not be secure. But for now, Alice and Bob's secret key seems safe."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercises\n",
"\n",
"1. How many Sophie Germain primes are there between 1 and 1000000? What proportion of primes in this range are Sophie Germain primes?\n",
"\n",
"2. [It is expected](https://en.wikipedia.org/wiki/Artin%27s_conjecture_on_primitive_roots) that there are infinitely primes $p$ such that 2 is a primitive root mod $p$. Study the density of such primes. For example, among the primes up to 1000, how often is $2$ a primitive root? Does this density seem to change? \n",
"\n",
"3. Adapt Diffie-Hellman to work with a group of *three* parties who wish to share a common secret. Hint: the common secret will have the form $g^{abc}$, and other exponents like $g^a$, $g^b$, $g^c$, $g^{ab}$, $g^{bc}$, $g^{ac}$ will be public information.\n",
"\n",
"4. Sadly, Alice and Bob have agreed to use the primitive root $g = 3$ and the prime $p = 65537$. Listening into their conversation, you intercept the following: $A = 40360$ and $B = 21002$ and the ciphertext is `$;6HWD;P5LVJ99W+EH9JVx=I:V7ESpGC^`. If you know that they use a protocol with a Vigenère cipher, with key equal the string associated to their secret $S$, what is the plaintext message? Hint: you should be able to solve the discrete logarithm problem with a brute force attack.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.4"
}
},
"nbformat": 4,
"nbformat_minor": 1
}