Updated at 2013-05-12 07:11

This note is about cryptography. Cryptography is the practice and study of techniques for secure communication in the presence of potential third parties.

Data leaks happen because people do not know what they are doing. Most of the time, cryptography does not fail, but systems get compromised because of using the wrong tools for the wrong job or using the right tools the wrong way.

Best practice for web is using the SSL-GPG combo. Use SSL for transfer and GPG for safekeeping. SSL is not foolproof but unfortunately the only option. If you can, distribute an asymmetric signature verification key with the client side of the client-server software and use that to bootstrap your cryptography. When they download a client, give a unique key to the user.

Hashes are used for data integrity. This is generally validating that data is transferred without modification. Hashing is taking an arbitrary block of data and returns a fixed-size string. Hashes are one way, you cannot generate the original data from a hash. As of 2013, use SHA-256 from SHA-2. Use SHA-3 after it is out.

Symmetric-key algorithms are used for encrypting data. They use the same key for encrypting and decrypting. The key of the message provides a level of authentication for the message. Block ciphers use symmetric-key algorithms for encrypting data.

AES and Blowfish are both good for encrypting data.

Asymmetric-key algorithms are used for confidentiality. They use different keys for encryption and decryption. Usually, the key that is used for encryption is kept secret while decryption key is made public. These algorithms are called "public-key algorithms". Mainly used for digital signatures.

RSA encryption with Optimal Asymmetric Encryption Padding is good.
Use 2048-bit RSA key, public component of 65537, SHA-256 and MGF1-SHA-256.

Consider using a one-time-pads for storing highly sensitive data. One-time-pad means having as long password as the message. They are truly unbreakable but problem is that the password takes as much space as the data and the password must be transferred securely. Impossible to use in everyday communication.

If you are serious about data security, plan for the 4B. Four human elements: Burglary, Bribery, Blackmail, (Guantamo) Bay a.k.a. torture. If your company is doing immoral things, you have to include the fifth B, Bravery (whistleblows), but that is a rare situation.

Consult a cryptographist when:

  • Evil person may have physical access to the device.
  • Evil person can run code on same physical device.
  • Want to use the minimum possible amount of power or storage.

Encryptions rely on standards. There are usually multiple implementations, but they should all follow the standard. E.g. AES (2001), Salsa20 (2008).

Why XOR operation is used so much in cryptography. Assuming uniformly random 1-bit inputs, the AND function output probability distribution is 75% 0 and 25% 1. Conversely, OR is 25% 0 and 75% 1. The XOR function is 50% 0 and 50% 1, therefore it is good for combining uniform probability distributions.