this post was submitted on
71 points (74% like it)
107 up votes 36 down votes
all 36 comments

[–]gbeier 28 points29 points ago

sorry, this has been archived and can no longer be voted on

The Handbook of Applied Cryptography is now 100% free and explains it very well. Two of the authors are Menezes and Vanstone (the 'M' and 'V' in MQV). If you need to understand this for anything remotely important, you should really read this book.

[–]podjackel 11 points12 points ago

sorry, this has been archived and can no longer be voted on

me crypta! _^

[–]joe_n 52 points53 points ago*

sorry, this has been archived and can no longer be voted on

The math depends on the algorithm. Here's my best stab at explaining it for RSA.

Consider the powers of a number subject to a modulus, e.g. the powers of 9 mod 33:

9 * 9 = 81 = 15 (mod 33)

15 * 9 = 135 = 3 (mod 33)

3 * 9 = 27 = 27 (mod 33)

27 * 9 = 243 = 12 (mod 33)

12 * 9 = 108 = 9 (mod 33)

In this case we see that 9 * 95 = 9 (mod 33). This also forms the cycle 9 -> 15 -> 3 -> 27 -> 12 -> 9

The same can happen for all numbers 2-32 (technically 0 and 1 as well), though the cycle may be longer or shorter. So suppose I tell you to pick a number in this range and send me the 3rd element in the cycle it forms. In other words, if you want to tell me x, you send y = x3 (mod 33). Without even looking at what number you sent, I would know that the cycle length is a factor of 20, because we're using 33 as the modulus (more on this later). Therefore I know that x * x20 = x (mod 33) no matter what you chose for x. Combining that with the fact that 3 * 7 = 21, I know that:

y7 (mod 33) = (x3 )7 (mod 33) = x21 (mod 33) = x * x20 (mod 33) = x (mod 33)

Why x3 ? Why not x5 or x10 ? Well, even if I know the cycle length and y, I won't know immediately what is x (that is the entire point), so I can't directly advance or retreat in the cycle one element at a time. However, if I know x3 then I know how to go 3 steps at a time, and I can keep doing that until I know I must be at the start of the cycle. In this case, advancing seven times in this fashion puts me at the 21st element, which is the original element after some number of full cycles. This can only be done if I choose an exponent coprime to 20. So it doesn't need to be 3; it could have instead been 7, 11, etc. (but not 5 or 10).

Now the question is what do I know that an attacker wouldn't know? To understand the answers you have to bear in mind that in reality the scale of numbers here is much larger. Here, 33 is the product of two prime numbers, and this is no coincidence. However, instead of 3 and 11, you might use two very large prime numbers. It's important that they are chosen with some randomness so they cannot be guessed. Then, instead of 33 you would use the product of these prime numbers for your modulus.

The length of the cycle formed by the powers of a number is always a factor of 20 = (11-1) * (3-1). In general it will be the number of positive integers coprime to and less then your modulus. Since the modulus is a product of two primes, we count the positive integers less than 33 (11 * 3 - 1) and subtract the multiples of 3 (11-1 of them) and the multiples of 11 (3-1 of them). Again you can imagine that with large prime numbers the length of cycles can get very long, and because (or, assuming) an attacker can not figure out the factorization, this cycle length multiple is unknown to them as well. Without knowing any such multiple, even though it can also advance three steps at a time, it does not know when to stop its advancement. Also note that it is practically unfeasible to advance through step by step. Implementations of this algorithm will use repeated squaring or other methods to get to the specific power (x3 and y7 ) much more quickly.

Finally, the keys are setup as follows. To send me something, you need to know the modulus (33) and the index in the cycle that you should send (3). In RSA this is generally referred to as N and e, N being the product of two prime numbers which are not public.

In order to decipher what you've sent, I'll need to know the modulus as well (33) and the number of times to advance it to get back to the original element (7). These are generally referred to as N and d.

Note that the two prime numbers (p=3, q=11) are the base secrets involved. N is computed as their product and e,d are chosen such that e * d = 1 (mod (p-1) * (q-1)). However, after computing N/e/d, p/q are no longer necessary and can be discarded.

You can also notice the symmetric relationship between e and d, and this can be used to create digital signatures. If I want to sign something, I 'encrypt' it using (N,d) which is known only to me. Others can decrypt it with (N,e) to see that I did in fact sign it. However all of this assumes that I can reliably send you (N,e), but that's somewhat of a different problem.

edit: fixing a bunch of typos >.<

[–]DudeWheresMyQuran 1 point2 points ago

sorry, this has been archived and can no longer be voted on

I wrote an application that did RSA from scratch, if I can find the source code I'll post it.

tl;dr: modular arithmatic is magical.

[–]snoobie 6 points7 points ago

sorry, this has been archived and can no longer be voted on

While cool and and an interesting personal project, never roll your own crypto.

[–]iEqualsj 2 points3 points ago

sorry, this has been archived and can no longer be voted on

Just use Ruby's implementation.

[–]snoobie 1 point2 points ago

sorry, this has been archived and can no longer be voted on

lol. Are you referring to this: http://t.co/daI630Dq

[–]iEqualsj 1 point2 points ago

sorry, this has been archived and can no longer be voted on

Yep, exactly that. :)

[–]DudeWheresMyQuran 3 points4 points ago

sorry, this has been archived and can no longer be voted on

It was merely an educational project to understand exactly how RSA works, from the ground up.

[–]snoobie 0 points1 point ago

sorry, this has been archived and can no longer be voted on

Fair enough, and something I would do for funsies as well, just wanted to let you know what you were being downvoted for.

If you want a more in depth reason: http://rdist.root.org/2009/08/06/google-tech-talk-on-common-crypto-flaws/

[–]datenwolf 0 points1 point ago

sorry, this has been archived and can no longer be voted on

Unless you're core developer of one of the well tested crypto libraries, or a crypto researcher who knows his stuff ;)

[–]dokuhebi 26 points27 points ago

sorry, this has been archived and can no longer be voted on

One-way mathematical functions are problems that are easy to solve one way, but difficult to reverse. For example, it's trivially easy to multiply two prime numbers together, but very difficult to figure out the two prime numbers that were multiplied together with just the result.

That's the gist of it. gooz's link to RSA gives a lot more of the details particular to RSA.

[–][deleted] 1 point2 points ago

sorry, this has been archived and can no longer be voted on

Thanks, great simple explanation for those who don't want to study it. Answered something I've wanted to know a long time as well.

[–]gooz 7 points8 points ago

sorry, this has been archived and can no longer be voted on

The Wikipedia article on RSA is a pretty good start.

[–]gbeier 5 points6 points ago

sorry, this has been archived and can no longer be voted on

That's very RSA specific (as the article's title would imply). If you're interested in the math behind asymmetric cryptography in general and not just RSA (which we can only hope will be mostly replaced before we really need to require 128-bit strength in asymmetric algs) chapters 3 and 8 of the HAC are a much better place to start.

[–]Davbot 8 points9 points ago

sorry, this has been archived and can no longer be voted on

HAC = Handbook of Applied Cryptography (http://www.cacr.math.uwaterloo.ca/hac/)

[–]gbeier 2 points3 points ago

sorry, this has been archived and can no longer be voted on

Yep. Sorry. Didn't mean to just throw that out there... I'd linked it in another comment :)

[–]Davbot 1 point2 points ago

sorry, this has been archived and can no longer be voted on

Absolutely no need to apologise! I was just trying to save someone the trip to Google if they were following the thread :)

[–]ef4 0 points1 point ago

sorry, this has been archived and can no longer be voted on

which we can only hope will be mostly replaced before we really need to require 128-bit strength in asymmetric algs

Is that because of the computational cost of RSA? Do you see elliptic curve crypto as the better alternative? Just curious.

[–]gbeier 0 points1 point ago

sorry, this has been archived and can no longer be voted on

Yep. Server operators don't even like 2048-bit RSA. This page discusses key sizes and computational cost.

[–]px403 2 points3 points ago

sorry, this has been archived and can no longer be voted on

I like to think of it like a rotation. One key moves the number left, the other key moves the number right. If you keep doing the same operation (encrypting with the public key) you won't get back to where you started for a while, but if you rotate the other direction (with the private key) you get right back to your clear text data.

[–]iheartrms 6 points7 points ago

sorry, this has been archived and can no longer be voted on

Bruce Scheier's "Applied Cryptography" covers this in a very nice way also.

[–][deleted] 1 point2 points ago

sorry, this has been archived and can no longer be voted on

Awesome, awesome book. His blog is similarly great. http://www.schneier.com/

[–]iheartrms 0 points1 point ago

sorry, this has been archived and can no longer be voted on

Definitely. I've had it in my RSS feed for years.

[–]aviewanew 2 points3 points ago

sorry, this has been archived and can no longer be voted on

ElGamal is another public key cryptosystem, similar to RSA, but with different math. But it's not too difficult to follow. Here's my attempt at explaining it.

[–]rainereality 3 points4 points ago

sorry, this has been archived and can no longer be voted on

From "download: the true story of the Internet". This is probably the most straight forward explanation I've ever seen. I even recommended it to my university professor. http://www.youtube.com/watch?v=a72fHRr6MRU

[–]ltx 1 point2 points ago

sorry, this has been archived and can no longer be voted on

I think this is a pretty good explanation (even though I still don't understand it).

[–]ldpreload 1 point2 points ago

sorry, this has been archived and can no longer be voted on

AES is symmetric; the question here is how asymmetric encryption is mathematically possible.

[–]dguido 1 point2 points ago

sorry, this has been archived and can no longer be voted on

It looks like I'm late to this game, but if you want just the basics then the chapter on Cryptography from Security Engineering should do it: http://www.cl.cam.ac.uk/~rja14/Papers/SE-05.pdf

[–]terremoto 0 points1 point ago

sorry, this has been archived and can no longer be voted on

There are two parts to public key cryptography. There's the key exchange then the actual communication. Take a look at this, and if you want a more detailed explanation after looking that over, there's this on a key exchange methodology.

As for the communication with the exchanged keys, there's a decent summary of the actual math here.

[–]futurespice 7 points8 points ago

sorry, this has been archived and can no longer be voted on

PK crypto doesn't require key exchange + symmetric crypto, that's just the way some protocols like TLS do secure communication because it's less computationally demanding. For other applications staight asymmetric-key is used, e.g. mail.

[–]gbeier 6 points7 points ago

sorry, this has been archived and can no longer be voted on

PK crypto doesn't require key exchange + symmetric crypto

OK.

For other applications staight asymmetric-key is used, e.g. mail.

No.

Depending on the asymmetric algorithm in question, mail most commonly uses an asymmetric algorithm for key transfer or key agreement only. The bulk encryption is still done with a symmetric key. So, for instance, if I wanted to send encrypted mail to a recipient for whom I have an RSA public key, I would generate an AES key, encrypt my message using AES, and encrypt that AES key for my recipient(s) using their RSA public key(s). If I wanted to send email to someone for whom I had an EC public key, I'd generate an AES key to encrypt the mail and encrypt that AES key using a secret generated either with static-ephemeral ECDH or 1-pass ECMQV.

I cannot, offhand, think of a single real application where asymmetric encryption is used for something much larger than a symmetric key. Did you have something other than email in mind?

[–]futurespice 0 points1 point ago

sorry, this has been archived and can no longer be voted on

You're right - sorry about that.

I can actually think of some applications using straight El-Gamal or RSA but they are not "real-world" in the sense of being used for something more than research purposes.

[–]ef4 1 point2 points ago

sorry, this has been archived and can no longer be voted on

RSA and friends get inconvenient as soon as you're trying to encrypt (or sign) things longer than your key. And if you want semantic security the limit is even shorter, because you need to include space for a padding scheme as well.

This, even more than the raw computational cost, is what leads to session key exchange.

[–]niczar -2 points-1 points ago

sorry, this has been archived and can no longer be voted on

Magic.

[–]EryHax -2 points-1 points ago

sorry, this has been archived and can no longer be voted on

PKI is the coolest thing ever.