{ "metadata": { "name": "", "signature": "sha256:0a69eb3d1be73655770ac84793fbc2bad5a974b3090a5c7b462946799788e201" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "How does RSA work? It comes down to understand a few things about prime numbers. So first, remind yourself when a number is prime. Namely, when nothing divides it evenly, or in other words when the remainder is not zero, except for 1 and the number itself.\n", "\n", "Next, find some prime numbers, say [here.](http://primes.utm.edu/lists/small/millions/)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "prime1= 512927357\n", "prime2= 674506081" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 99 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next, compute the product. Easy peazy lemon squeezy." ] }, { "cell_type": "code", "collapsed": false, "input": [ "N = prime1*prime2" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 100 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that N has approximately twice as many digits!" ] }, { "cell_type": "code", "collapsed": false, "input": [ "N" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 101, "text": [ "345972621407757917" ] } ], "prompt_number": 101 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next, try to factor N, pretending you no longer know your primes.\n", "\n", "How would you do that? Possibly: write a for loop looking for factors\n", "\n", "First play with the \"remainder\" function in python." ] }, { "cell_type": "code", "collapsed": false, "input": [ "print 3%12\n", "print 12%3\n", "print 15%4\n", "print 4%15" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "3\n", "0\n", "3\n", "4\n" ] } ], "prompt_number": 102 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next, use that to look for factors less than n and larger than 1. This is a stupid algorithm but it works." ] }, { "cell_type": "code", "collapsed": false, "input": [ "def find_a_factor(n):\n", " for i in range(2, n):\n", " if n%i == 0:\n", " return n, i\n", " return n, \"Prime!\"\n", "print find_a_factor(100)\n", "print find_a_factor(101)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "(100, 2)\n", "(101, 'Prime!')\n" ] } ], "prompt_number": 103 }, { "cell_type": "markdown", "metadata": {}, "source": [ "How efficient is find_a_factor? Convince yourself it is maximally inefficient for primes, and try a few primes of increasing size." ] }, { "cell_type": "code", "collapsed": false, "input": [ "find_a_factor(859093)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 104, "text": [ "(859093, 'Prime!')" ] } ], "prompt_number": 104 }, { "cell_type": "code", "collapsed": false, "input": [ "find_a_factor(2814841)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 105, "text": [ "(2814841, 'Prime!')" ] } ], "prompt_number": 105 }, { "cell_type": "code", "collapsed": false, "input": [ "find_a_factor(15485863)" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 106, "text": [ "(15485863, 'Prime!')" ] } ], "prompt_number": 106 }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can estimate that it would take a seriously long time to find a factor in N. And yes, there are better ways to do it, but they are still incredibly slow. And the real RSA uses 100-digit prime numbers.\n", "\n", "So what we have is something that's _easy to do but very difficult to undo._\n", "\n", "How do we use this to our advantage?\n", "\n", "To explain this we need to explain a little bit about arithmetic mod N.\n", "\n", "1. Forms a group under addition\n", "1. Forms a group under multiplication if you remove stuff coprime to N. \n", "1. By definition you have Φ(N) elements in that group.\n", "1. But you secretly know that Φ(N)=(prime1 - 1) ∗ (prime2 - 1)\n", "1. Every element in the multiplicative group has the property that if you take it to the power Φ(N), you get 1 modulo N.\n", "1. If you turn a secret message into a number, and then bring that number to some power d modulo N, then you can \"go the rest of the way\" to get back to 1, and then go once further to get back to the coded message, but only if you know what Φ(N) is.\n", "1. There's actually a minor restriction on what d can be but don't worry about that because there are lots of choices for d. Namely, make sure d is coprime to Φ(N).\n", "1. Once you've chosen d, find also f so that f times d is 1 modulo Φ(N).\n", "1. Then if you first take something to the dth power, then to the fth power, you've altogether taken it to the power which is something 1 modulo Φ(N), which is the original message again, which is magic.\n", "\n", "#Here's what you make public. \n", "\n", "N and d.\n", "\n", "#Here's what you make people do. \n", "\n", "Take their message, which is a number, and take it to the dth power modulo N. Make sure you know this easy.\n", "\n", "#Here's what you can do after you get their encrypted message.\n", "\n", "Take their message to the f'th power to get it back to their message. Again, super easy.\n" ] }, { "cell_type": "code", "collapsed": false, "input": [ "Phi_N = (prime1 - 1)* (prime2-1)\n", "print Phi_N\n", "print N" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "345972620220324480\n", "345972621407757917\n" ] } ], "prompt_number": 107 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Choose a d and make sure it's prime to Φ(N)." ] }, { "cell_type": "code", "collapsed": false, "input": [ "d = 17\n", "print Phi_N%d" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "11\n" ] } ], "prompt_number": 108 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now find the multiplicative inverse of d modulo Φ(N). This amounts to finding an a and a b so that:\n", "\n", "a ∗ d + b ∗ Φ(N) = 1.\n", "\n", "First you write \n", "\n", "Φ(N) = q1 ∗ d + r" ] }, { "cell_type": "code", "collapsed": false, "input": [ "q1 = Phi_N/d\n", "r = Phi_N%d\n", "print q1, r" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "20351330601195557 11\n" ] } ], "prompt_number": 109 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Φ(N) = 20351330601195557 ∗ 17 + 11\n", "\n", "Next do the division algorithm on d and r and so on:\n", "\n", "d = 17 = 1 ∗ 11 + 6\n", "\n", "\n", "11 = 1 ∗ 6 + 5\n", "\n", "\n", "6 = 1 ∗ 5 + 1\n", "\n", "Now rewrite those equations slightly:\n", "\n", "1 = 6 - 1 ∗ 5 \n", "\n", "5 = 11 - 1 ∗ 6\n", "\n", "6 = d - 1 ∗ 11\n", "\n", "11 = Φ(N) - 20351330601195557 ∗ d\n", "\n", "And now go backwards from 1:\n", "\n", "1 = 6 - (11 - 6) = 2 ∗ 6 - 11\n", "\n", "= 2 ∗ (d - 11) - 11 = 2 ∗ d - 3 ∗ 11 \n", "\n", "= 2 ∗ d - 3 ∗ (Φ(N) - 20351330601195557 ∗ d)\n", "\n", "= - 3 ∗ Φ(N) + (3 ∗ 20351330601195557 + 2) ∗ d\n", "\n", "So 3 ∗ 20351330601195557 + 2 is the multiplicative inverse of d = 17. Let's check that." ] }, { "cell_type": "code", "collapsed": false, "input": [ "f = 3 * 20351330601195557 + 2" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 110 }, { "cell_type": "code", "collapsed": false, "input": [ "test = f*d\n", "print test" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1037917860660973441\n" ] } ], "prompt_number": 111 }, { "cell_type": "markdown", "metadata": {}, "source": [ "We just want to make sure test has remainder 1 when it's divided by Phi_N:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print test%Phi_N" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "1\n" ] } ], "prompt_number": 113 }, { "cell_type": "markdown", "metadata": {}, "source": [ "OK now we're all set to try out RSA. \n", "\n", "Should we try this? Why not?? What should our secret message be?\n", "\n", "Let's write \"hello\" as 08 = \"h\", 05 = \"e\", 12 = \"l\", and 15 = \"o\"" ] }, { "cell_type": "code", "collapsed": false, "input": [ "secret = 805121215" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 114 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's do something random to get the 17th power of \"secret\":" ] }, { "cell_type": "code", "collapsed": false, "input": [ "s2 = (secret**2)%N\n", "print s2" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "302247549435318308\n" ] } ], "prompt_number": 115 }, { "cell_type": "code", "collapsed": false, "input": [ "s4 = (s2*s2)%N\n", "s8 = (s4*s4)%N\n", "s16=(s8*s8)%N\n", "encrypted_message = (secret*s16)%N\n", "encrypted_message" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 116, "text": [ "16899822895020834L" ] } ], "prompt_number": 116 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let's pretend someone handed that to us, and we want to decrypt it. That will come down to taking the fth power of that. Let's remind ourselves what f is, and that it's way bigger than d, which is 17:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "f" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 117, "text": [ "61053991803586673" ] } ], "prompt_number": 117 }, { "cell_type": "markdown", "metadata": {}, "source": [ "In order to take our encrypted message to the fth power, we first write f in binary, or in other words we write f as a sum of powers of 2. I'll first write a little function that will find the biggest power of 2 smaller than f, and using that I'll form the list of exponents for f." ] }, { "cell_type": "code", "collapsed": false, "input": [ "def find_leading_binary_term(f):\n", " exponent = 1\n", " while 2**exponent0:\n", " leading_term = find_leading_binary_term(f)\n", " powers_list.append(leading_term)\n", " f = f - 2**leading_term\n", " \n", "print powers_list" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[55, 54, 52, 51, 47, 46, 45, 43, 38, 35, 29, 28, 26, 24, 23, 20, 17, 15, 11, 6, 5, 4, 0]\n" ] } ], "prompt_number": 118 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next we need to square our encrypted_message a bunch of times to get the components we will put together for the fth power. " ] }, { "cell_type": "code", "collapsed": false, "input": [ "biggest_power = max(powers_list)\n", "encrypted_dict = {}\n", "encrypted_dict[0] = encrypted_message\n", "for i in range(1, biggest_power+1):\n", " encrypted_dict[i] = (encrypted_dict[i-1]**2)%N" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 119 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we multiply all the appropriate terms together to get the fth power of encrypted_message. \"prod\" is our final result, and recover our secret message \"hello\"!" ] }, { "cell_type": "code", "collapsed": false, "input": [ "prod = 1\n", "for p in powers_list:\n", " prod = prod*encrypted_dict[p]%N\n", " \n", "prod" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 120, "text": [ "805121215L" ] } ], "prompt_number": 120 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Holy shit it worked." ] } ], "metadata": {} } ] }