diff --git a/content/RSACrypto.ipynb b/content/RSACrypto.ipynb index bef0d1a..8e0b341 100644 --- a/content/RSACrypto.ipynb +++ b/content/RSACrypto.ipynb @@ -41,22 +41,57 @@ " \n", " The idea behind the usage of public and private keys is similar to the one anounced by William Stanley Jevons: the public and private keys create a unique combination for each sender and recipient. \n", " \n", - "### The Algorithm\n", - " \n", - " The algorithm uses two main principals of number arithmetic and field theory: prime numbers and modular arithmetics.\n", - " \n", - " **Key Generation:** \n", - " \n", - " 1 - Select two very large prime numbers. Those will be called *p* and *q*. \n", + "\n", " \n", - " 2 - Compute *n = p.q*.\n", + " 5 - Compute the modular inverse of *e* to the modulus of Φ$(n)$. One will always exist. That number will be called *d* and is the private key. In other words, to compute *d*, you must use the equation: $$ de = 1 (mod \\Phi) $$\n", " \n", - " 3 - Compute Euler Totient Function Φ$(n) = (p-1)(q-1)$. This is an interesting function, which reveals the amount of numbers that are coprime to *p* and *q*.\n", + " **Encryption and Decryption:**\n", " \n", - " 4 - Choose a number *e* that must be relatively prime to Φ$(n)$. Those two numbers constitute the public key. \n", + " Suppose you wish to send a number *m* to your friend, who has *e* and Φ as his public keys. In order to encrypt *m* you must apply the following equation: \n", " \n", - " 5 - Compute the modular inverse of *e* to the modulus of Φ$(n)$. That number will be called *d* and is the private key.\n", - " " + "$$ c = m^e (mod n)$$\n", + "\n", + "c is now the encrypted message. \n", + "\n", + " For your friend to decrypt *c*, he must do the following:\n", + "\n", + "$$ m = c^d (mod n) $$\n", + "\n", + "### The Magic of Coprime Numbers\n", + "\n", + "It might seem strange that calculating the multiplicative inverse of a number using a certain modulo will help you find the original message that was encoded in another modulo. However, there are a few mathematical theorems that guarantee such procedure. \n", + "\n", + "**Fermat's Little Theorem:** Originally proposed by French mathematician Pierre de Fermat in 1640, it states that if *p* is prime, *a* is an integer coprime to *p*, then:\n", + "\n", + "$$ a^{(p-1)} = 1 mod(p)$$ \n", + "\n", + "**Euler's Theorem:** A generalization of Fermat's Little Theorem, it states that if b and a are coprime integers, then:\n", + "\n", + "$$a^{\\Phi(b)} = 1 (mod b)$$\n", + "\n", + "where Φ is Euler's totient function $\\Phi(b) = b-1$\n", + "\n", + "On an important note, the Totient Function for a non-prime number is equal to the product of each of the primes that compose it reduced in 1. You can see this being used in the previous section.\n", + "\n", + "We know that RSA uses the equation $de = 1 (mod \\Phi)$ to find the private key. We know *e* and Φ are coprimes. One of the properties of coprimes is that a number will always have a multiplicative inverse under a modulo that is coprime to it. That is why the equation above is always true. \n", + "\n", + "Knowing congruency in modular arithmetics, we know that if we add to the value of Φ to the right side of the equation, that is still equal to *de*. In other words: \n", + "\n", + "$$de = 1 + \\Phi (mod \\Phi)$$.\n", + "\n", + "If we elevate both sides of the equation above to the original message (*m*) and define modulo *n*, we have: \n", + "\n", + "$$m^{de} = m^{1+\\Phi} (mod n)$$ \n", + "\n", + "$$m^{de} = mm^{\\Phi} (mod n)$$\n", + "\n", + "Using Euler's theorem, we know that $m^{\\Phi}$ is equal to one. Therefore: $$m^{de} = m (mod n)$$\n", + "\n", + "If we define $c = m^e (mod n)$, and we elevate c to d ($ c^d (mod n)$), we will obtain m.\n", + "\n", + "That is the mathematical basis of this cryptosystem. \n", + "\n", + "Notice that if *d* is unknown, it is impossible to find the original message. " ] }, {