{"id":140,"date":"2008-07-16T05:44:16","date_gmt":"2008-07-16T05:44:16","guid":{"rendered":"http:\/\/www.blueraja.com\/blog2\/?p=140"},"modified":"2012-03-09T19:04:51","modified_gmt":"2012-03-10T00:04:51","slug":"rsa-encryption-part-3","status":"publish","type":"post","link":"https:\/\/www.blueraja.com\/blog\/140\/rsa-encryption-part-3","title":{"rendered":"RSA Encryption, Part 3"},"content":{"rendered":"<p>Finally, we get to the point:  how RSA encryption works.  You may want to review parts <a href=\"http:\/\/www.blueraja.com\/blog\/135\/rsa-encryption-part-1\">1<\/a> and <a href=\"http:\/\/www.blueraja.com\/blog\/138\/rsa-encryption-part-2\">2<\/a> to make sure all that modular arithmetic stuff is fresh in your mind.<\/p>\n<p><span class=\"heading\">3. RSA Encryption<\/span><br \/>\nFor those of you who are unaware, the term <em>encryption<\/em> is used to describe any method that changes a message so that only its intended recipient can read it.  RSA encryption (named after its creators) belongs to a special class of encryption called <em>public key encryption<\/em>.  In public key encyption, separate keys are used to encrypt and decrypt the message:  the public key is known to everyone, and can be used to encrypt a message, while the private key is known only to the recipient, and is required to decode the message.  This is opposed to <em>symmetric-key encryption<\/em>, in which the same key must be known to both the sender and receiver.<br \/>\nThus, if you want to send me a message, all you have to do is encode your message with my public key, and only I will be able to read it (since only I have the corresponding private key).  If I then want to send you a message in response, I would simply encrypt my response using your public key.<br \/>\nThink safes:  public key encryption would be akin to a safe with a slot on top for inserting messages.  Anyone with access to the safe (that is, who has the public key) can drop messages into the safe through the slot, but only the person who has the key to the safe (the private key) can read them.<\/p>\n<p>As a quick refresher, remember what we finished with last time.  For all cases important to us, if we want to determine <span class=\"equation\">a<sup>k<\/sup> (mod n)<\/span>, then<br \/>\n<span class=\"equation\">a<sup>k<\/sup> &equiv; a<sup>k mod &Phi;(n)<\/sup> (mod n)<\/span>.<\/p>\n<p>First of all, we need some convention for mapping letters to numbers and vice versa.  Let&#8217;s use the convention that A=0, B=1, C=2, &#8230;, Y=24, Z=25.<sup>&dagger;<\/sup><\/p>\n<p>Next, we begin the encryption process by choosing any two prime numbers; for convenience, let&#8217;s choose 3 and 11.  The product of these will be our n: thus, <span class=\"equation\">n = 3*11 = 33<\/span>.  Also, since n is the product of two primes, <span class=\"equation\">&Phi;(n) = (3-1)(11-1) = 20<\/span> (as was discussed last time).<br \/>\nWe also choose some number less than (and relatively prime to) &Phi;(n) and compute its inverse (mod &Phi;(n)) &#8211; we will see why in a minute.  Let&#8217;s choose <span class=\"equation\">e=3<\/span> and <span class=\"equation\">d = e<sup>-1<\/sup> = 7<\/span> (since <span class=\"equation\">3*7 &equiv; 21 &equiv; 1 (mod &Phi;(n))<\/span>).  Keep in mind that, since we defined e and d to be inverses, their product <span class=\"equation\">ed &equiv; 1 (mod &Phi;(n))<\/span>.<br \/>\nWe&#8217;ll call the pair of numbers <span class=\"equation\">(d,n)=(7,33)<\/span> our <em>Private Key<\/em>, and the pair of numbers <span class=\"equation\">(e,n)=(3,33)<\/span> our <em>Public Key<\/em>.<\/p>\n<p>Now we have everything we need &#8211; this is where it gets cool.  Let&#8217;s try to encrypt the message &#8220;HELLO&#8221; using the public key.  We will do this by encoding the message into numbers using our convention and then taking each number in turn to the power of <span class=\"equation\">e<\/span> (mod n).  We can then decrypt by taking each number to the power <span class=\"equation\">d<\/span> (mod n).  Let&#8217;s go over the example, and then I&#8217;ll explain why it works.<\/p>\n<p>Under our encoding, the message &#8220;HELLO&#8221; is given by the numbers &#8220;7 4 11 11 14&#8221;.  Since <span class=\"equation\">e=3<\/span>, we raise 7 to the 3rd power to get <span class=\"equation\">7<sup>3<\/sup> &equiv; 13 (mod n)<\/span>; so the first number in our encrypted message is 13.  Likewise, <span class=\"equation\">4<sup>3<\/sup> &equiv; 31 (mod n)<\/span>.  After a bit of calculation, we get an encrypted message of &#8220;13 31 11 11 5&#8221;.  We can now transmit this message down the line without having to worry about somebody reading it without the private key.<br \/>\nNow let&#8217;s decrypt the message:  taking each of the numbers &#8220;13 31 11 11 5&#8221; and raising them in turn by the power <span class=\"equation\">d=7<\/span>, we get &#8220;7 4 11 11 14&#8221;, which decodes to the message &#8220;HELLO&#8221;.  Great!<\/p>\n<p>So how did this all work?  The reasoning is simple, actually.  If you&#8217;ll remember back to last time, we concluded with the formula<br \/>\n<span class=\"equation\">a<sup>k<\/sup> &equiv; a<sup>k (mod &Phi;(n))<\/sup> (mod n)<\/span><br \/>\nIn this case, a is our message, and k is e, our public key exponent.  Thus, for each digit <span class=\"equation\">a<\/span>, what we&#8217;re transmitting is <span class=\"equation\">a<sup>e<\/sup> (mod n)<\/span>.  When the message is received, we take it to the exponent d; thus, the new value is<br \/>\n<span class=\"equation\">(a<sup>e<\/sup>)<sup>d<\/sup> &equiv; a<sup>ed<\/sup> &equiv; a<sup>ed mod &Phi;(n)<\/sup> (mod n)<\/span><br \/>\nby the above formula.  However, if you&#8217;ll remember, <span class=\"equation\">ed &equiv; 1 (mod &Phi;(n))<\/span>.  Thus, after decryption we&#8217;re left with simply <span class=\"equation\">a (mod n)<\/span>, the original message!<sup>&dagger;&dagger;<\/sup><\/p>\n<p>Yep &#8211; it&#8217;s just that simple.  So simple, in fact, it might seem too simple.  Why, you may be asking, can&#8217;t someone who&#8217;s given e (part of the public key) just calculate its inverse to get d (the unknown half of the private key)?  Keep in mind that <span class=\"equation\">d<\/span> is the inverse of <span class=\"equation\">e (mod &Phi;(n))<\/span> &#8211; and in order to calculate <span class=\"equation\">&Phi;(n)<\/span>, we need to know p and q, the two primes which make up n.  And here&#8217;s the point &#8211; integers, as every computer scientist knows, are hard to factor into primes.  Very hard<sup>&Dagger;<\/sup>.  This means that if n is very large (usually along the order or tens or hundreds of decimal digits), it will be nearly impossible for even the fastest super computers to determine p and q, meaning an attacker is very unlikely to be able to determine &Phi;(n) and thus determine d. It is this inability to &quot;reverse&quot; our choice of n that gives RSA its security.<\/p>\n<div class=\"footnote\">\n<p><sup>&dagger;<\/sup> For those of you more familiar with computers, mappings based on strings of several ASCII or Unicode characters are most common.<br \/>\n    <sup>&dagger;&dagger;<\/sup> The astute reader will have noticed that this only holds true if the encoded message is relatively prime to n.  As it turns out, however, for RSA it just happens to hold true even when the message is not relatively prime to n; a proof of this can be found on <a href=\"http:\/\/en.wikipedia.org\/wiki\/RSA#Decryption\" title=\"The smartest place on the internet.\">wikipedia<\/a>.<br \/>\n    <sup>&Dagger;<\/sup> Probably. That is, it&#8217;s <em>believed<\/em> to be very hard, but nobody knows for sure.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Finally, we get to the point: how RSA encryption works. You may want to review parts 1 and 2 to make sure all that modular arithmetic stuff is fresh in your mind. 3. RSA Encryption For those of you who are unaware, the term encryption is used to describe any method that changes a message [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.blueraja.com\/blog\/wp-json\/wp\/v2\/posts\/140"}],"collection":[{"href":"https:\/\/www.blueraja.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.blueraja.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.blueraja.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.blueraja.com\/blog\/wp-json\/wp\/v2\/comments?post=140"}],"version-history":[{"count":4,"href":"https:\/\/www.blueraja.com\/blog\/wp-json\/wp\/v2\/posts\/140\/revisions"}],"predecessor-version":[{"id":348,"href":"https:\/\/www.blueraja.com\/blog\/wp-json\/wp\/v2\/posts\/140\/revisions\/348"}],"wp:attachment":[{"href":"https:\/\/www.blueraja.com\/blog\/wp-json\/wp\/v2\/media?parent=140"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.blueraja.com\/blog\/wp-json\/wp\/v2\/categories?post=140"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.blueraja.com\/blog\/wp-json\/wp\/v2\/tags?post=140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}