This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
1lann's profile picture

RSA public-key cryptography

Started by 1lann, 11 October 2015 - 10:56 AM
1lann #1
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.
Edited on 11 October 2015 - 10:56 AM
Anavrins #2
Posted 11 October 2015 - 09:17 PM
That is pretty awesome, a huge keysize improvement over my puny 60-bits keys proof of concept.
Thought I wonder, are you limited to 256-bits keys because of the key generation, or because of encryption/decryption?
What if larger keypairs were generated outside of CC, would it still encrypt/decrypt efficiently?
Edited on 11 October 2015 - 07:18 PM
1lann #3
Posted 12 October 2015 - 07:21 AM
That is pretty awesome, a huge keysize improvement over my puny 60-bits keys proof of concept.
Thought I wonder, are you limited to 256-bits keys because of the key generation, or because of encryption/decryption?
What if larger keypairs were generated outside of CC, would it still encrypt/decrypt efficiently?

I limited it to 256 bit keys because they already take on average about 100 seconds to generate, and 1-2 seconds to encrypt/decrypt. Larger keys take longer to generate and to encrypt/decrypt with. It becomes impractical if you try to generate keys that are any larger, but you can try it for yourself.
クデル #4
Posted 13 October 2015 - 05:54 AM
Thanks for sharing. :D/> +1
jaredallard #5
Posted 07 November 2015 - 01:07 AM
You should implement signing & verification. Would be *super* useful.
Luca_S #6
Posted 04 December 2015 - 04:46 PM
You've implemented quantum physics. GJ. I tried to run your demo on CC 1.74 and it errored "Arguments must be the same lenght". So I went to CC 1.73 and tried to look at it, and it worked like it should.