{"id":707,"date":"2008-12-01T09:29:57","date_gmt":"2008-12-01T09:29:57","guid":{"rendered":"http:\/\/scientopia.org\/blogs\/goodmath\/2008\/12\/01\/public-key-cryptography-using-rsa\/"},"modified":"2008-12-01T09:29:57","modified_gmt":"2008-12-01T09:29:57","slug":"public-key-cryptography-using-rsa","status":"publish","type":"post","link":"http:\/\/www.goodmath.org\/blog\/2008\/12\/01\/public-key-cryptography-using-rsa\/","title":{"rendered":"Public Key Cryptography using RSA"},"content":{"rendered":"<p><span class=\"technoratitag\">Technorati Tags: <a href=\"http:\/\/www.technorati.com\/tags\/cryptography\" rel=\"tag\">cryptography<\/a>, <a href=\"http:\/\/www.technorati.com\/tags\/public-key\" rel=\"tag\">public-key<\/a>, <a href=\"http:\/\/www.technorati.com\/tags\/encryption\" rel=\"tag\">encryption<\/a>, <a href=\"http:\/\/www.technorati.com\/tags\/RSA\" rel=\"tag\">RSA<\/a>, <a href=\"http:\/\/www.technorati.com\/tags\/asymmetric-encryption\" rel=\"tag\">asymmetric encryption<\/a><\/span><\/p>\n<p> The most successful public key cryptosystem in use today is RSA &#8211; named for its inventors Rivest, Shamir, and Adleman. I first learned about RSA in grad school from one of my professors, <a href=\"http:\/\/www.cis.udel.edu\/~elloyd\/\">Errol Lloyd<\/a>, who was one of Ron Rivest&#8217;s students. Errol is without a doubt the best <em>teacher<\/em> I&#8217;ve ever had (and also a thoroughly nice guy). If you want to go to grad school to study algorithms, you frankly couldn&#8217;t do better than heading to Delaware to work with Errol. I have very fond memories of Errol&#8217;s class where we talked about this. He&#8217;s got a way of teaching where he doesn&#8217;t come out and <em>tell<\/em> you anything; what he does is ask questions that lead you through the process of figuring it out yourself. That&#8217;s an incredibly effective way to teach <em>if<\/em> you can carry it off. Personally, I <em>can&#8217;t<\/em>. And I&#8217;ve never known anyone except Errol who could do it for a topic like RSA encryption!<\/p>\n<p> Anyway, moving on&#8230; In general, public key cryptosystems are based on problems that are easy to solve computationally in one direction, but really hard to solve computationally in the  other. In the case of RSA, the basic underlying problem involves prime numbers: if you&#8217;ve got two really large prime numbers, then multiplying them together is easy; but if you&#8217;ve got a number that&#8217;s the product of two really large primes, factoring it is <em>very<\/em> hard.<\/p>\n<p><!--more--><\/p>\n<p> One of the things that makes RSA difficult to explain is that it&#8217;s hard to find a starting point. The actual encryption and decryption processes are so simple that<br \/>\nthey seem like they can&#8217;t possibly work; there&#8217;s just no way to make sense out<br \/>\nof <em>why<\/em> they work without understanding where the keys come from; but it&#8217;s strange to describe an encryption system by starting with how to generate keys, when you don&#8217;t yet know how they&#8217;re going to get used. <\/p>\n<p> The reason for this tangling comes back to the fundamental goal of a public key<br \/>\nsystem: you&#8217;ve got to have two keys which share a complex mathematical relationship.<br \/>\nThe encryption system itself is really just a simple expression of the relationship<br \/>\nbetween the keys. So the algorithm is (conceptually) very simple; the whole thing is<br \/>\nbased on the nature of the keys, their relationship, and the way that they&#8217;re<br \/>\ngenerate. It&#8217;s not like symmetric cryptography, where you can simply chose a key; in<br \/>\npublic key systems, the keys have to be generated to satisfy a set of mathematical<br \/>\nrelationships.<\/p>\n<p> With that it mind, we&#8217;ll start working through the way that RSA works by showing<br \/>\nhow you can create a public\/private key pair. The key generation process is actually<br \/>\npretty simple, but most descriptions of it get tangled up because it uses a lot of<br \/>\nterminology from number theory. I&#8217;ll try to present the standard terms as clearly as I<br \/>\ncan.<\/p>\n<p> The basic structure underlying an RSA key pair is a pair of two large prime<br \/>\nnumbers. What&#8217;s large? That depends on how hard you want it to be to crack. The<br \/>\ntradeoff is that the larger the original pair of primes are, the more complex it is to<br \/>\ncompute ciphertext using a key. So you need to choose a key size which makes your keys<br \/>\nhard to crack, without making the cost of encryption and decryption unacceptably<br \/>\nhigh. Once you&#8217;ve decided on a key size, that dictates the size of your two prime numbers, and you&#8217;re ready to compute a key pair, as follows.<\/p>\n<ol>\n<li> Compute two large primes, which are typically named P and Q. <\/li>\n<li> Using P and Q, you compute a number N=P&times;Q, which is called the <em>modulus<\/em> of the keys being generated. <\/li>\n<li> Compute the <em>totient<\/em> of the modulus.  For any integer, I,<br \/>\nthe totient of I (written &phi;(I)) is the number of integers <em>smaller<\/em><br \/>\nthan I that are relatively prime to I. Because P and Q are prime, &phi;(P&times;Q)=(P-1)&times;(Q-1). (The totient of P is P-1, because there are P-1 numbers relatively prime to P; the totient of Q is Q-1 for the same reason; and since P and Q are (trivially) relatively prime to each other, the totient of P&times;Q is (P-1)&times;(Q-1)).<\/li>\n<li> Choose an integer, E, smaller than and relatively prime to &phi;(N). E is<br \/>\ncalled the <em>public key exponent<\/em>.<\/li>\n<li> Compute an integer D such that D&times;E=1 mod &phi;(N). D is called the <em>private key exponent<\/em>.<\/li>\n<\/ol>\n<p> The public key is the pair (N, E) of the modulus and the public key exponent; and the private key is the pain (N, D) of the modulus and the private key exponent. So you&#8217;ve got your key pair.<\/p>\n<p> Encryption and decryption are amazingly simple.<\/p>\n<p> Suppose that the ubiquitous Alice and Bob want to communicate. Alice gives Bob her<br \/>\npublic key, (N, E). Now, when Alice wants to send a message to Bob, she encodes the<br \/>\nplaintext of the message as an integer, M. (I&#8217;ll leave the exact encoding of plaintext open for now.) To encrypt with her private key, (N, D),<br \/>\nshe takes that integer and computes:<\/p>\n<p>Ciphertext = M<sup>D<\/sup> mod N<\/p>\n<p> Then to decrypt the message, Bob uses his key pair, and computes:<\/p>\n<p>M = Ciphertext<sup>E<\/sup> mod N<\/p>\n<p> For Bob to encrypt a message for Alice, he does <em>exactly<\/em> the same thing that he did to the ciphertext &#8211; except he does it to the encoded message, M. For Alice to decrypt that, she does exactly what she did to <em>encrypt<\/em> the original M, except that she uses the ciphertext she recieved from Bob instead of the encoded plaintext M. In other words, if you&#8217;ve got a ciphertext message encrypted by the private key, decrypting it is <em>exactly<\/em> the same process as <em>encrypting<\/em> a plaintext with the public key, and vice versa. (This point is what used to cause me lots of confusion remembering what was symmetric and what was assymetric &#8211; RSA style asymmetric encryption is really very symmetric in how the algorithm works.)<\/p>\n<p> How can this possibly work? On the face of it, it looks ridiculous! You encode by exponentiating once; you decode not by taking a root, but by exponentiating again!<\/p>\n<p> It all comes back to the way the keys were generated. If we look at the process<br \/>\nin terms of modulo arithmetic, it&#8217;s pretty easy to see why it works:<\/p>\n<ul>\n<li> Take an original message, M. Encrypted, it&#8217;s<br \/>\nC = M<sup>D<\/sup> mod N.<\/li>\n<li> Now, take the ciphertext, C, and decrypt it.<br \/>\nM&#8217; = C<sup>E<\/sup> mod N.<\/li>\n<li> Now, expand C: M&#8217; = (M<sup>D<\/sup> mod N)<sup>E<\/sup> mod N.<\/li>\n<li> Now, we can combine the exponents: M&#8217; = M<sup>D&times;E<\/sup> mod N.<\/li>\n<li> D&times;E = 1 mod N.<\/li>\n<li> By some trickiness, related to the fact that D and E are relative primes related to both each other and N by their relation to the<br \/>\ntotient of N, we can show that the fact that D&times;E = 1 mod N means<br \/>\nthat M&#8217; = M<sup>D&times;E<\/sup> = M<sup>1<\/sup> = M.<\/li>\n<\/ul>\n<p> Watch how we can walk through a ridiculously simplified example. Let&#8217;s start with<br \/>\na pair of primes &#8211; 29 and 61.<\/p>\n<ol>\n<li> Generate keys:\n<ul>\n<li> First, we compute the modulus: 29&times;61=1769.<\/li>\n<li> Then we compute the totient: 1680.<\/li>\n<li> Then we choose an E which is relatively prime with 1680. To make things easy,<br \/>\nI&#8217;ll just pick a prime number: E=13.<\/p>\n<li> Now, I need to compute a D, such that D&times;E=1 mod 1680;<br \/>\nor to pull out a bit of standard terminology, D is the modular multiplicative<br \/>\ninverse of E mod 1680. Doing that is an exercise in modulo arithmetic<br \/>\nwhich gets beyond the scope of what I want to talk about today, so I&#8217;ll<br \/>\ncheat: whipping out Mathematica, I get D=517.<\/li>\n<li> So, the public key is (1769,13), and the private key is (1769,517).<\/li>\n<\/ul>\n<\/li>\n<li> Now, let&#8217;s encode a message. Say the message is 236. So, encrypted by<br \/>\nthe public key, that&#8217;s 236<sup>13<\/sup> mod 1769 = 7044595136617487310722334457856 mod 1769 = 573.<\/li>\n<li> And now, let&#8217;s decode it. That&#8217;s 573<sup>517<\/sup> mod 1769 = \t9245694881849602770197401240655288154608138663291308421093411147578949<br \/>\n4462244595981994549450775979702694154377539110666284415651476199963925<br \/>\n1321729713042853618725578035043275339491371655153997356248940804495270<br \/>\n8984801258907252216498797520780679511957335315751292762455167434140192<br \/>\n4399386435156204841871858091160737587648566008200581107378219553124074<br \/>\n9374669246678685272914050993442585237015640426625522901746517901417734<br \/>\n7954757584818934596889432709457570226928654243870833959974236930834811<br \/>\n3204280174093386319717080086558480154647619900966739378086145377566860<br \/>\n9195850562761848872414474511076491179637551417498920992360433000710853<br \/>\n3935087767676514264669956171228256961906386882369681633698604708335082<br \/>\n4532038380922358459546076540454839797340473363177061704334483341984626<br \/>\n0992808331110751927026090969022077767175228102822099312339565763871188<br \/>\n4006940217478961345132678704142880421227822352750998264777937728911493<br \/>\n6283819708613556665220171457755862562705567302041244568283475209225680<br \/>\n3828781673802145987568796646578059853165517716374603716155560308698631<br \/>\n2650416428650673915642280074852668462543146649056536263032788685526436<br \/>\n7863995916605455190338263161582118236712167656565774640875461698797822<br \/>\n0025671340803745351386743506255677806855116853206286798808454276083160<br \/>\n2812779603110688541701764925723493557773173917037390830611160570524069<br \/>\n7471206139919064156642428626093922418127017110711925314998235480530680<br \/>\n56675958655700624098981453 mod 1769 = 236.<\/li>\n<\/ol>\n<p> So it works &#8211; but computing those results is not exactly trivial. It happens that there is a fast algorithm for doing those sorts of exponentiations &#8211; but &#8220;fast&#8221; is a relative term. RSA encryption is a <em>lot<\/em> slower than most symmetric encryptions, by a significant factor.<\/p>\n<p> In real-world use, RSA is rarely used for full-message encryption. It&#8217;s used for signatures (where you encrypt a small summary of a message), and for protocol negotiations to allow the two sides to settle on a symmetric encryption key to<br \/>\nuse for the rest of their communication. <\/p>\n<p> You&#8217;ll notice that I still haven&#8217;t gotten back to how you take a message<br \/>\nand convert it to an integer. That&#8217;s <em>not<\/em> trivial. The problem is,<br \/>\nfor certain message sizes, or messages with certain numeric properties,<br \/>\nRSA can be easily broken. So you need to protect against those cases. That&#8217;s<br \/>\ndone by <em>padding<\/em> the message &#8211; adding bits to it in a way that removes<br \/>\nor obscures the properties of the message that would otherwise make it vulnerable.<br \/>\nDiscussing the vulnerabilities of RSA for certain types of messages, and how<br \/>\nto build a padding system that defeats them is a topic for another post.<\/p>\n<p> In future posts, I&#8217;ll also describe the two most common uses of RSA in more detail &#8211; both using RSA for signing a message to prove that you really wrote it; and using<br \/>\nRSA as part of a secure cryptographic protocol where most of the traffic is really<br \/>\nencrypted using a symmetric algorithm.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Technorati Tags: cryptography, public-key, encryption, RSA, asymmetric encryption The most successful public key cryptosystem in use today is RSA &#8211; named for its inventors Rivest, Shamir, and Adleman. I first learned about RSA in grad school from one of my professors, Errol Lloyd, who was one of Ron Rivest&#8217;s students. Errol is without a doubt [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[84],"tags":[],"class_list":["post-707","post","type-post","status-publish","format-standard","hentry","category-encryption"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p4lzZS-bp","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/707","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/comments?post=707"}],"version-history":[{"count":0,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/707\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/media?parent=707"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/categories?post=707"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/tags?post=707"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}