Archived from http://world.std.com/~franl/crypto/rsa-guts.html, which is sadly no longer available.
The RSA algorithm was invented in 1978 by Ron Rivest, Adi Shamir, and Leonard Adleman.
Here's the relatively easy to understand math behind RSA public key encryption.
Your public key is the pair (PQ, E). Your private key is the number D (reveal it to no one). The product PQ is the modulus (often called N in the literature). E is the public exponent. D is the secret exponent.
You can publish your public key freely, because there are no known easy methods of calculating D, P, or Q given only (PQ, E) (your public key). If P and Q are each 1024 bits long, the sun will burn out before the most powerful computers presently in existence can factor your modulus into P and Q.
Here is an example of RSA encryption.
Though it is widely suspected to be true, it is not yet proven that no easy methods of factoring exist. It is not yet proven that the only way to crack RSA is to factor the modulus.
Adam Back has created an implementation of RSA in just 2 lines of Perl. It uses dc, an arbitrary precision arithmetic package that ships with most UNIX systems. Here's the Perl code:
print pack"C*",split/\D+/,`echo "16iII*o\U@{$/=$z;[(pop,pop,unpack"H*",<> )]}\EsMsKsN0[lN*1lK[d2%Sa2/d0<X+d*lMLa^*lN%0]dsXx++lMlN/dsM0<J]dsJxp"|dc`