From http://slashdot.org/article.pl?sid=00/09/06/1252204&mode=nested how RSA works (Score:5, Insightful) by pemerson on Wednesday September 06, @08:51AM EST (#89) (User #179241 Info) Here's a somewhat simplified taste of how RSA works, for those of you who are curious. Note: I took this from a document that I wrote for my students, so this is how I personally had them implement RSA, NOT how RSA is really done in real life. But the basic premise of key generation is the same. Background math: gcd is greatest common divisor. mod means modular arithmetic. To generate your personal key: 1. Generate two prime numbers, p and q. 2. Calculate n = p*q. 2. Calculate phi(n) = (p-1)(q-1). 3. Pick a public key b where 00. 5. n and the public key can be published in a directory. Keep the private key secret. To crack a key given n and the public key b: 1. Factor n into p and q. 2. Calculate phi(n) = (p-1)(q-1). 3. Calculate the private key; it's a=b^-1 mod phi(n). To encrypt code, translate from an array of characters to numbers. let a=0 .. z=25. Encrypt in blocks of three like this: abc = 0*26*26 + 1*26 + 2 = 28 dog = 3*26*26 + 14*26 + 6 = 2398 cat = 2*26*26 + 0*26 + 19 = 1371 zzz = 25*26*26 + 25*26 + 25 = 17575 Call chunks of text converted to numbers m (for message). Compute m^b mod n. Each of these numbers go on separate lines in the file. To decrypt code, do the process in reverse. Call the encrypted message m. Compute m^a mod n. Then you can convert from unencrypted numbers back into plaintext. You can also do a double encryption (digital signature) by taking already encrypted code and encrypting those numbers. Suppose Alice wants to send a message to Bob which only Bob can decrypt and Bob knows can only have come from Alice. Alice uses her own private key to encrypt the message. Then she applies Bob's public key and gives the file to Bob. Bob takes the file and applies his private key to it, and then Alice's public key, leaving him with the plaintext. This ensures that Alice sent the message and only Bob can decode it.