Skip to main content
\( \newcommand{\lt}{ < } \newcommand{\gt}{ > } \newcommand{\amp}{ & } \)

Section11.3A Modular Exponentiation Cipher

To prepare for discussion of a famous public-key system, we will first discuss a (symmetric) system that leads to it. This system needs yet another invertible number theory procedure, one that we finally should be comfortable with.

That procedure is modular exponentiation as cipher. Recall that we have methods to solve modular exponentials (such as primitive roots). That means we have the tools to tackle these subtle techniques.

Sage note11.3.1Another reminder to evaluate definitions

Don't forget to evaluate this so we can use words as messages instead of just numbers.

Subsection11.3.1The Diffie-Hellman method

In the cell below, we will pick a few numbers relevant to this method. To use it, we will need a prime number \(p\), and some legitimate exponent \(e\) that won't mess things up too badly. (Also, suppose our secret is still that math is cool.)

What do I mean by ‘won't mess things up too badly?’ Recall that when we solved \begin{equation*}x^{3}\equiv 5 \text{ mod}(17)\text{ as }3^{3i}\equiv 3^{5}\text{ mod}(17)\end{equation*} we ended up in the world of \(\phi(17)=16\) and solved \begin{equation*}3i\equiv 5 \text{ mod}(16)\, .\end{equation*} This required a solution \(i\) to exist, which wouldn't happen for all possible numbers!

In order to keep using these ideas easily, we will pick an exponent coprime to \(\phi(p)\).

Now, here is the algorithm (see also Algorithm 11.3.3). I just take my message (as a number) and raise it to the \(e\) power modulo \(p\). It's as simple as that!

In the cell below, we pick a convenient \(e\) and \(p\).

Here I picked \(p=29\) since it's close to \(26\), and more or less arbitrarily picked an exponent \(e=9\).

Note the steps. I first had to encode “MathIsCool” to numbers. Then I exponentiated each number in the coded version, modulo 29. To be precise, I sent each number \begin{equation*}a\to a^9\text{ (mod }29)\; .\end{equation*}

Remark11.3.2

Notice that decoding the secret message code is not so useful anymore! (What would we do with the number \(28\) as an output, for instance?) So we usually just stick with the numbers.

Leaving aside for the moment that the letter A will now have the unfortunate property that it always stays 1, and hence basically unencrypted (this is because we are doing a toy example), how on earth would we ever decrypt this? Do we have a way to invert \begin{equation*}a^9\text{ (mod }29)\end{equation*} in any way?

Naturally, we do! We will use exponentiation again to do so. We just need something that solves \begin{equation*}\left(a^9\right)^f\equiv a\text{ (mod }29)\; ,\end{equation*} or more concisely \begin{equation*}a^{9f}\equiv a^1\text{ (mod }29)\; .\end{equation*} (We can think of \(f\) as a power that inverts the original power \(9\).).

From our earlier discussion, this is just a solution to \begin{equation*}9f\equiv 1\text{ (mod }28)\end{equation*} and we know we can find this. In the cell below, we do it computationally, but you could do this one ‘by hand’.

This method of encryption is known as the Diffie-Hellman, or D-H, method (named after its originators, who proposed it in the mid-70's).

Subsection11.3.2A bigger example

Now we will do a more real example of this. Notice how important it was that we chose an initial exponent \(e\) that was coprime to \(\phi(p)=p-1\).

For convenience, I'll just take the next prime bigger than my message.

Next, I pick an exponent. Not every exponent will work! Beforehand I factored \(p-1\) so I could find something coprime to it.

The encrypted message is now just one number. Now we need the decryption key. Luckily, that's just as easy as taking an inverse modulo \(p-1\):

Here is one more extended Sage example. Here, the interesting point is that I allow Sage to pick a prime for me using next_prime().

Subsection11.3.3Recap

Here is the formal explanation of our first awesome encryption scheme.

Feel free to use the following Sage cells to see what happens with your own short messages.

Or you can choose a prime on your own.

Sage note11.3.4Compute what you need

Remember, you can always compute anything you need. For instance, if you for some reason didn't pick a big enough prime, you can use the following command to find one.

Remark11.3.5

In 2015, Whitfield Diffie and Martin Hellman won the Turing Award for their contribution, the highest award in computer science.

Subsection11.3.4A brief warning

Remember, the key that makes it all work (thanks to Fermat's Little Theorem/Euler's Theorem) is that exponents of congruences mod \(n\) live in the world of congruences mod \(\phi(n)\), as long as they are numbers coprime to \(\phi(n)\). That's why \(\gcd(e,p-1)=1\) is important.

Here's an example of how not choosing your exponent wisely can go wrong.

Sage note11.3.6Change values right in the code

You don't have to have a Sage cell with little boxes for interacting to change the values! Try changing them above to encode your own secret.

Assuming you followed along, so far, so good; it got encrypted. But what happens when we try to decrypt?

You should have gotten an error (in fact, a ZeroDivisionError, which should sound relevant). It turns out not even to be possible to go backwards. Be warned that you must know the mathematics to use cryptography wisely.