I’ve been playing around with Publickey cryptography or asymmetric cryptography. With revaltions from Eric Snowden on programs like PRISM, I feel that encryption is more important than ever.
Publickey cryptography is a class of cryptographic algorithms which requires two separate keys, one of which is secret (or private) and one of which is public. Although different, the two parts of this key pair are mathematically linked. The public key is used to encrypt plaintext or to verify a digital signature; whereas the private key is used to decrypt ciphertext or to create a digital signature.
There are two main uses for public key crypto.
Public key encryption. A message is encrypted with a recipient’s public key. The message can only be unencrypted by the holder of the private key.
Digital Signature. A message is signed with a the senders private key to generate a cryptographic hash. Using the public key, one can verify the message was indeed sent with the coresponding private key and can ensure the message has not been altered.
Keys
First, let’s generate some keys. From the private key we will also generate the public key. Remember the private and public key are mathematically linked. This is important.
1 2 3 4 5 6 

Great. Now we have a key pair. We can write these keys to disk if we like.
1 2 3 4 5 6 7 8 

We can read the key in from key like so.
1


Public key enctryption
We’ll be encrypting a message with the public key. So it can only be unenctrypted by the owner of the private key.
1 2 3 

This will result in some unreadable binary data. Next we’ll decrypt this data with the private key.
1 2 3 

Nice!
The public key can encrypt but cannot decrypt the message. This enctryption only works in one direction. Only the private key can decrypt that message. You can also encrypt messages with a private key. Those messages can be unenctrypted with the public key and the private key.
Asymmetric public/private key encryption is slow and victim to attack in cases where it is used without padding or directly to encrypt larger chunks of data. Typical use cases for RSA encryption involve “wrapping” a symmetric key with the public key of the recipient who would “unwrap” that symmetric key again using their private key.
NOTE  The enctrypted messages are in binary format. If we want to encode the data for transmission accoss the net, we’ll want to use something like base64.
1 2 3 4 

This works for small messages but if you try and encrpt a string larget than your key… you’ll hit an errors. You could break up the string into smaller chunks and encrypt each chunk individually. But this is not secure. This is what cypher block chaining is for. Below is an example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 

Digital Signature
Asymmetric digital signatures is a great way to verify integrity and authenticity of data. Create a keypair, send the public key to your receivers, and use this method to create a digital signature. By combining the data and the public key, you can verify that the signature was created by the owner of the private key.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 

More
Using openssl library directly we can encrypt a file.
1


And to decrypt the file.
1

