## Public-key Cryptography for Dummies

For the last few days I have been busy in this amazing 29c3 conference in Hamburg, Germany (Not in NewYork). Basically, it’s a conference about everything that is about security and data. One thing that struck me was this talk RSA factorisation in the real world from facthack. Why it struck me, it’s because we have been using it a lot but I think I know nothing about it. And every time you Google about it, you get a lot of mathematical examples (Of course the algorithm is about maths). So, I guess I would just want to write it down so I could remember it myself also if it would benefit others that’d be fantastic.

So, we all like Wikipedia, here’s the wiki version. But if you’re lazy like me you might want to watch this video which I think they virtualise it pretty good.

And here is my version of it. I think the concept is basic and really easy to understand, though it took me a couple days to understand it. Imagine you want to send a note to a girl you like across the room where you have a lot of friends who try to bully or mock you. I ask the girl to buy a box with a lock and a key so nobody with that key can open the box. Then, she will send me the box across the room, it may pass a lot of people but they cannot open the box without her key. I will put the message in the box and close it and send it back. Then she could open that box with her key. I know this is not exactly right, but the point is that she has a key which is private-key and the box is public-key. As the names suggest, public means it’s wide open you can send it to anywhere publicly, but the key is private and you keep it.

Another example that I can think of is SSH. I’m gonna skip what SSH is but when you want to connect to the server you need to enter a password and then the connection is open. So, one example that I can think of is Github. When you create an account on Github, it asks you to upload your public key. You have to do these steps

First, you use this command to generate public/private keys

Basically, that would generate public/private keys in your home folder. if you saved the keys in your home folder that you can view your keys.

file id_rsa.pub is your public key and id_rsa is your private key. Then you upload id_rsa.pub to Github and then you can connect to Github via SSH without having to use password every time. I think EC2 and Heroku are the same.

Now, we all love coding what about in code? I have some example here which is in Python and I think all the languages are the same.

I use the library rsa. You can install it easily

Then you can generate the keys.

it might take a couple seconds to get the keys, but 2048 should be your standard by now. Now have a look at the keys

You can see some prime numbers. Then, I will send the public_key to my friend, or Github. Now my friend with encrypt the message and send it back to me.

You will get an encrypted message, we can take a peek let’s see what’s inside

You can see it’s gibberish. Then my friend can send this crpyto back to me. And I’ll try to decrypt it.

Now let’s try to decrypt the message with some other private key. You’ll get this.

If you’re interested some really hardcore stuff how to hack it. I recommend he talk from 29c3 conference.

Dec 29th, 2012