Posted 11 October 2015 - 12:56 PM

So a few months ago, I implemented RSA in pure Lua, and I thought I may as well publish it here for people to use.

Don't know what RSA or public-key cryptography is? Here are 2 great videos by Brady Haran: https://www.youtube....h?v=GSIDS_lvRv4 and https://www.youtube....h?v=M7kEpw1tn50

Just a word of warning, I am no cryptographer whatsoever. My implementation is probably flawed in a million different ways, and can probably be optimized to be 2x faster. I'm just some random guy who wanted to see if I could implemented RSA in pure Lua with little idea about the underlying maths and concepts. If you find any issues or have any suggestions, tell me!

Here's the key pair generator: http://pastebin.com/udGZapmD

And here's the encryption/decryption library with an example at the bottom that you can run: http://pastebin.com/0zZYjxLw

Licenses: http://pastebin.com/9gWSyqQt

The generator generates a public and private key pair stored under /public.key and /private.key respectively. The keys it generates are extremely insecure, as the prime numbers are only 39 digits, so the keys are 256 bit. This means the key can be factored in less than a minute on a decent desktop computer using something like http://sourceforge.net/projects/yafu/. I tried running YAFU in a virtual machine on my MacBook Pro, and it only took 3 minutes to factor the key, on average it only used 30% of my CPU (not sure why):

So yeah, super insecure keys, don't use them for anything super serious. If anything, this is more for educational purposes rather than proper cryptography.

If you have any questions, feel free to ask me here. If I don't respond here, email me at [email="me@chuie.io"]me@chuie.io[/email] to get my attention.

Don't know what RSA or public-key cryptography is? Here are 2 great videos by Brady Haran: https://www.youtube....h?v=GSIDS_lvRv4 and https://www.youtube....h?v=M7kEpw1tn50

Just a word of warning, I am no cryptographer whatsoever. My implementation is probably flawed in a million different ways, and can probably be optimized to be 2x faster. I'm just some random guy who wanted to see if I could implemented RSA in pure Lua with little idea about the underlying maths and concepts. If you find any issues or have any suggestions, tell me!

Here's the key pair generator: http://pastebin.com/udGZapmD

And here's the encryption/decryption library with an example at the bottom that you can run: http://pastebin.com/0zZYjxLw

Licenses: http://pastebin.com/9gWSyqQt

The generator generates a public and private key pair stored under /public.key and /private.key respectively. The keys it generates are extremely insecure, as the prime numbers are only 39 digits, so the keys are 256 bit. This means the key can be factored in less than a minute on a decent desktop computer using something like http://sourceforge.net/projects/yafu/. I tried running YAFU in a virtual machine on my MacBook Pro, and it only took 3 minutes to factor the key, on average it only used 30% of my CPU (not sure why):

So yeah, super insecure keys, don't use them for anything super serious. If anything, this is more for educational purposes rather than proper cryptography.

If you have any questions, feel free to ask me here. If I don't respond here, email me at [email="me@chuie.io"]me@chuie.io[/email] to get my attention.

Edited on 11 October 2015 - 10:56 AM