[EN] [Crypto] The RSA Algorithm
For those who know me, you're well aware of my love for this field.
And after long enough avoidance, I've finally decided to put pen to paper and share some thoughts on cryptography, starting with the most known to muggles, The RSA Algorithm
.
The RSA algorithm, named after its creators Rivest, Shamir, and Adleman, is (approximately) the first and most widely used public key crypto system. It's method employs two distinct keys for encryption and decryption, also known as asymmetric cryptography.
In public-key cryptography, one key can be publicly shared while the other remains a closely guarded secret.
The Alorithm
Here's a simplified breakdown of how the RSA algorithm works:
Key Generation: Two large prime numbers, and , are chosen.
- These numbers are multiplied together to create , also known as the modulus.
- Then, we'll calculate the Euler's totient of and using
- A first number is chosen such that and
- Finally, a number is calculated such that .
The pair forms the private key, while is the public key.
Encryption: To encrypt a message , the sender uses the public key of the receiver. The ciphertext is then calculated as .
Decryption: Conversely, the receiver uses hes private key to decrypt the message. The original message M is calculated as .
Example
Let's illustrate this with an example. Suppose we have calculated our keys as follows:
let use both prime numbers (IRL both and needs to be very long)
Calculate
Calculate
Select such as and
Following our example, select such as and , Thus choosing any number within .
I'll chooseCalculate such as ,
I'll choose, ,
So my keys are ,
Whenever I need to send a message I'd need to:
Encode the message into usable numbers. Note that any symbol can be converted to a number. For example, here
The ciphertext C would be calculated as .
The message M can be recalculated using , which gives us back our original message.
Yay ! You finally encrypted your message !
That all you need to know about The RSA Algorithm. Hope it was fun to read and easy to work with.
Now is your turn to make it work by Rolling your own RSA
Spoiler (example)
# Here is a sample short implementation of RSA Algorithm, from
# Prof Bill's website https://asecuritysite.com/encryption/rsa12
from Crypto.Util.number import *
from Crypto import Random
import Crypto
import libnum
import sys
bits=60
msg="Hello"
if (len(sys.argv)>1):
msg=str(sys.argv[1])
if (len(sys.argv)>2):
bits=int(sys.argv[2])
p = Crypto.Util.number.getPrime(bits, randfunc=Crypto.Random.get_random_bytes)
q = Crypto.Util.number.getPrime(bits, randfunc=Crypto.Random.get_random_bytes)
n = p*q
PHI=(p-1)*(q-1)
e=65537
d=libnum.invmod(e,PHI)
## d=(gmpy2.invert(e, PHI))
m=bytes_to_long(msg.encode('utf-8'))
c=pow(m,e, n)
res=pow(c,d ,n)
print ("Message=%s\np=%s\nq=%s\n\nd=%d\ne=%d\nN=%s\n\nPrivate key (d,n)\nPublic key (e,n)\n\ncipher=%s\ndecipher=%s" % (msg,p,q,d,e,n,c,(long_to_bytes(res))))
A sample run,
Message=hello
p=242648958288128614541925147518101769011
q=299356840913214192252590475232148200447
N=72638625604016464006874651287120524699932001616388639276131104258310920947917
cipher=5847803746095553957863305890801268831081138920772806292673259864173015661385
decipher=hello
- https://asecuritysite.com/rsa/rsa12