Cryptography is the discipline that studies the transmission of information according to three aspects:
Think of everyday situations where those aspects are important. Piensa en situaciones en que esos aspectos sean importantes. 
Some applications:
We will commonly use the names plaintext (P), ciphertext (C), encryption and decryption algorithms/functions (E/D).
Encryption and decryption are based on functions of a special kind.
Kerckhoffs's principle: a cryptosystem should be secure even if everything about the system, except the key, is public knowledge.




A shift cryptosystem consists on an encryption procedure where we take each letter of a message and change it by a shift (displacement) of a number of positions. The classical Caesar cipher is based on a shift of distance 3:
Let us explore them briefly with Sage. We create a plain text from a sentence in English.

We shift every letter; the size of the shift is the key. The ciphertext cannot be understood. With the same key we can decrypt.


This system is very weak: there are only 26 possible keys, so we can try to decode with all of them and see if we find a sentence we can understand. This is called a bruteforce attack.

We still needed to look at 26 messages to find the correct one. We can do better: statistical cryptanalysis. In our situation, we can use knowledge about English language: in English, the most common letter is "E". So, if the plaintext is long, it will have more "E"'s than other letters. If we find the most common letter in the ciphertext, we will know that it probably comes from "E". Here are the frequencies in English.

In this case, Sage has a statistical method to find which keys are more probable, by comparing frequencies. We see that the first candidate, the most probable key and message, is precisely the correct one. The message has been discovered!

We see why it is important that the set of possible keys is very large, and that the mathematical and statistical properties of the system and the messages are studied properly to make sure that no attacks can be done successfully.
Create a message in a nonEnglish language and encrypt it with a shift. See if the bruteforce attack of Sage can decrypt it. Crea un mensaje que no esté en inglés y encríptalo con un cifrado por desplazamiento. Después mira si Sage consigue descifrarlo. 


Think about the following situation: Alice and Bob need to agree on a cryptosystem. How can they do it if someone could be listening? Piensa en la situación siguiente: Alice y Bob quieren ponerse de acuerdo en un criptosistema, pero ¿cómo hacerlo si alguien podría estar escuchando? 
In the 70s, two cryptologists called Diffie and Hellman published a method to exchange keys over a public channel. Since then, public key cryptography has become widely used. We will not go into the details here. The concept is:
Today we are going to learn a popular public key cryptosystem: RSA. It is used in different contexts and it requires little mathematical knowledge to understand it.
As we saw in the ciphers of before, we need to work in a cyclic way. Numerically this can be done using the so called modular arithmetic. We will learn it now. If we number the letters as A=0, B=1, C=2,..., Z=25 then the cipher is $E(x)=x+k$. But the last letters go back to the beginning... mathematically this is achieved by the equivalences $26\equiv0$, $27\equiv1$, ... and, if we go in the other direction, $1\equiv25$, $2\equiv24$, ... The equivalences continue indefinitely and they are based on the number 26 (because we make numbers equivalent when we add or subtract 26). We are doing arithmetic modulo 26. In this number system there are essentially 26 different numbers.
Find three numbers that are equivalent to 11 modulo 26. One of them must be greater than 100 and another one must be negative. Encuentra tres números equivalentes a 11 módulo 26. Uno debe ser mayor que 100 y el otro debe ser negativo. 

In books you will see that the equivalence is written like this: $a \equiv b \pmod{26}$.
Since adding or subtracting 26 repeatedly gives equivalent numbers, we have another way of expressing this: $a \equiv b \pmod{26}$ if $ab$ is a multiple of 26. We will use this often.
Of course, instead of 26 the same thing can be done for any other natural number.
The numbers modulo $n$ have three operations: +, , and *. But in principle we don't know if we can divide. When we have these three operations and their usual properties (commutativity...), the technical name is ring (if we can also divide, it is called a field, as we saw in the codes session). Let us calculate with Sage.

Make sure that you understand these results. Asegúrate de entender esos resultados. 
The RSA system works superficially as follows: we will choose a number $n$ for doing computations mod $n$ (later we will see how), and also an exponent $e$. Then we will write a message as an integer $m$, and the encryption process will be $E(m)=m^e \pmod n$. The numbers $n$ and $e$ are your public key; anyone can encrypt a message for you with those numbers.
For decryption you will use another number, your private key, $d$. This number must satisfy
\[ (m^e)^d \equiv m \pmod n. \]
We will talk later about choosing $n$, $e$ and $d$, so that it is very difficult to calculate $d$ from the other two. But first, let us briefly talk about exponentiation, since it is an operation we use in both halves of the process.
To get an idea of the computations, encrypt by hand the message $m=3$ using $n=5$ and $e=12$. Hint: you may think of doing 11 multiplications, but you can do it with less. Another hint: you need not wait until the end to reduce the result mod 5. Para que entiendas cómo son estos cálculos, encripta a mano el mensaje $m=3$ usando $n=5$ y $e=12$. Pista: se puede hacer con menos de 11 multiplicaciones. Otra pista: no hace falta esperar al final para reducir módulo 5. 

One more: encrypt by hand the message $m=3$ using $n=10$ and $e=128$. Hint: $128=2^7$. Conclusion: powers mod $n$ are not so long to compute as it seems. Una más: encripta a mano el mensaje $m=3$ usando $n=10$ y $e=128$. Pista: $128=2^7$. Conclusión: las potencias módulo $n$ no se tardan tanto en calcular como parece. 

In short, we can calculate powers with large exponents in a relatively fast way by doing repeated squaring and some multiplications. For example, a usual value in practice for $e$ is $65537=2^{16}+1$. (How many operations do we need for such a power?)
Note that $d$ is something like an inverse of $e$. Next we will talk about inverses in general, and later about our particular situation of inverting exponents.
Can we divide numbers modulo $n$? In fact, in order to divide we only need to find inverses: $\frac{a}{b} = b\cdot\frac{1}{a}$.
Unfortunately, it does not always work because some numbers don't have inverses:


Find by hand the inverse of 5 modulo 26, then check it with Sage. You can instead write a loop to check all the elements from 1 to 25 (we need not check 0, why?) Calcula a mano la inversa de 5 módulo 26 y luego comprueba con Sage. En vez de eso puedes escribir un bucle que compruebe los números de 1 a 25 (no hace falta comprobar el 0, ¿por qué?) 

Which numbers have inverses modulo 26? if $a$ has an inverse $b$ then $ab\equiv1\pmod{26}$. We will see next that this equation is very related to a basic mathematical object: the greatest common divisor or gcd.
Recall the definition of gcd with this example:



In order to compute the gcd of two numbers, it seems that we need to factor them. This is difficult in general (we will talk about that later). But there is a much faster method: the Euclidean algorithm. It is based on the following observation:
\[ \gcd(a,b) = \gcd(a,ba) \]
By repeated application of this, we can reduce the numbers until it is easy to calculate the gcd directly.
Compute the gcd below with this method. Then evaluate with Sage to check the result. Calcula el gcd siguiente con este método. Después evaluá la celda para comprobar el resultado. 



A faster way: if $b$ is much greater than $a$, instead of subtracting many times, $ba$, $b2a$, $b3a$, ..., you can jump to the final value by doing a division with remainder:
\[ b = a\cdot q + r \qquad\Rightarrow\qquad \gcd(a,b) = \gcd(a,aq+r) = \gcd(a,r). \]
We redo the previous example. In Sage, the remainder is calculated with the symbol %. If you want both the quotient and the remainder you can apply the method .quo_rem.



So the gcd is 13 (because it divides 39, and it is obviously the greatest divisor of 13).
Now comes the relation between the gcd and the modular inverse. The relation is given by some extra information that we can extract from the Euclidean algorithm. Let us write the divisions that we did:
\[ 195=52\cdot3+39; \quad 52=39\cdot1+13; \quad 39=13\cdot3. \]
In the penultimate relation we can write the gcd (the remainder) as a combination of the other two numbers: $13=5239\cdot1$.
But in the relation before, the reminder (39) can also be isolated, and substituted into the relation that we just wrote:
\[ 39 = 19552\cdot3 \qquad\Rightarrow\qquad 13 = 5239\cdot1 = 52(19552\cdot3) = 195\cdot(1) + 52\cdot4. \]
In this way, the gcd is a linear integer combination of the two original numbers. This fact, and the method to calculate it, is called extended Euclidean algorithm, because it gives the gcd but also the two coefficients.
Compute the gcd of 144 and 25 using the previous algorithm. Then write it as a combination of the two numbers. Calcula el gcd de 144 y 25 con el algoritmo anterior. Después escríbelo como combinación de los dos números. 


Sage can calculate the gcd and the coefficients at the same time, with the function xgcd. Note that we can assign the three values to three variables at the same time.


And finally we will relate all this story of the gcd with modular inverses! With an example.
Before, you calculated the inverse of 5 mod 26. We compute the gcd between these two numbers with the extended Euclidean algorithm:

This means that $1=5\cdot(5)+1\cdot26$. But what happens if you write this equality of integers as an equality mod 26?
\[ 1\equiv 5\cdot(5)+1\cdot26 \equiv 5\cdot(265)+1\cdot0 = 5\cdot21 \pmod{26} \]
This means that the inverse mod 26 of 5 is 21 (their product is $105\equiv1 \pmod{26}$).
In summary, $a$ has inverse $\pmod n$ when $gcd(a,n)=1$ (we say that $a$ and $n$ are coprime), and its inverse is the number $s$ in the expression $1=as+nt$ computed with the extended Euclidean algorithm.
Write a program to check which numbers from 1 to 25 have inverses mod 26 and to calculate them when they exist. Escribe un programa que calcule qué números de 1 a 25 tienen inversa módulo 26 y que las calculen. 


In general, not all the numbers from 1 to $n$ have an inverse mod $n$ because not all of them are coprime with $n$. Euler discovered a function that counts how many coprime numbers are there for any $n$; it is usually called $\varphi(n)$ (the Greek letter "phi"). For example:

Euler's phi function is very important for us, because it appears in a theorem that talks precisely about the exponents that we care about:
Euler's theorem: if $gcd(a,n)=1$ then $a^{\varphi(n)}\equiv1 \pmod n$.
Let $n=15$. Calculate how many numbers from $1$ to $n$ have inverses mod $n$. Then calculate $a^{\varphi(n)} \pmod n$ for every $a$ from 1 to $n$ and check that the results agree with the first calculation. Sea $n=15$. Calcula cuántos números de 1 a $n$ tienen inversa mod $n$. Después calcula $a^{\varphi(n)} \pmod n$ para todos los $a$ entre 1 y $n$ y comprueba que coincide con lo calculado antes. 


We have almost all the ingredients now.
We need:
With the last condition we have, for every message $m$:
\[ (m^e)^d = m^{e\cdot d} = m^{1+w\varphi(n)} = m\cdot (m^{\varphi(n)})^w \equiv m\cdot 1^w = m \pmod n. \]
This means that decryption really recovers the plaintext ($m$) from the ciphertext ($m^e$).
In order to build the keys, we would do then:
Construct a set of keys. Then exchange messages with another person. Construye unas llaves. Después intercambia mensajes con otra persona. 




Think about this: if you reveal $\varphi(n)$ then your messages can be discovered. Piensa en esto: si revelas $\varphi(n)$ se pueden descubrir tus mensajes. 
If you followed the procedure, it is very likely that your keys are not secure, because there are many mathematical properties that allow someone with some computing power to find your $d$. Let us see how to make it (more) secure.
Take someone else's public keys. Try to break their system: see if in less than 60 seconds of computations you can find their private key (you can take as long as you need to think and to write code). Toma las llaves públicas de alguien. Intenta romper su sistema: a ver si con menos de 60 segundos de cálculo puedes encontrar su clave privada (puedes tomarte el tiempo que quieras para pensar y escribir código). 


The weakness in the system comes from being able to find $\varphi(n)$ from $n$. This depends, in fact, on the factorization of $n$. So we should make $n$ be difficult to factor. Remember that we said that factoring is a difficult function (multiplication is a oneway function).
It is believed that the safest way of choosing $n$ is:
Once we have done this, we find some $e$ coprime with $\varphi(n)$ (just choose at random one that is not very small).
Choose a random number $n$ of 50 digits (use the function randint). See how long it takes to compute $\varphi(n)$ and also factor $n$. Repeat all this five times. Elige al azar un $n$ de 50 cifras (usa la función randint). Calcula cuánto se tarda en calcular $\varphi(n)$ y también en factorizar $n$. Repite todo el procedimiento cinco veces. 




An example of RSA adapted from http://www.sagemath.org/files/thesis/nguyenthesis2009.pdf:












Richard A. Mollin, "An Introduction to Cryptography", second edition, CRC (2007).
Jeffrey Hoffstein, Jill Pipher, J.H. Silverman, "An introduction to mathematical cryptography", Springer (2008).
