Friday, January 22, 2016

Trying to understand the RSA algorithm with a Perl script

I tried to understand the RSA encryption algorithm and wrote two Perl scripts. Maybe someone else finds this useful, so I share it.

The first script (generate_keys.pl) creates the public and the private key. It takes two prime numbers as arguments, in this context commonly referred to as p and n:

U:\> generate_keys.pl 643 947 public key e = 65537 n = 608921 private key d = 412697 n = 608921

Now, I can encrypt a message with the second script (encrypt_decrypt.pl) and the public key: the first two parameters are the public key, the third parameter the message to be encrypted.

The message I want to encrypt is the number 42:

U:\> encrypt_decrypt.pl 65537 608921 42 166097

In order to decrypt 166097, I use encrypt_decrypt.pl again, this time with the private key:

U:\> encrypt_decrypt.pl 412697 608921 166097 42

Links

RSA key generation example with python was a very helpful page for me.

Modular multiplicative inverse on wikipedia.

Extended euclidean algorithm on wikipedia.

RSA (cryptosystem) on wikipedia.

The scripts on github.

No comments:

Post a Comment