Search code examples
javascriptcryptojs

understanding different encrypt mode in cryptojs


I am learning about various hashing technique and found interesting library to start with cryptoJs

In the documentation, there are multiple options defined as below hashing HMAC PBKDF2 Ciphers Encoders

I understanding hashing is about generating the ciphertext. HMAC is about generating message authenticate code. But I am struggling to differentiate between PBKDF2, Ciphers, and Encoders. Which one to choose when?

Any pointers are helpful.


Solution

  • Password-Based Key Derivation Function 2 - PBKDF2 are functions used to create cryptographic keys that are harder to brute force using key-stretching. because humans are lazy and create passwords way too easy to brute force.

    For example: our favorite password is "password"

    Given a salt of "5C52FBAE9A4D97A49D14C8AF338DA55C"

    The cryptographic key becomes (Hex)A2EB261802FFD1965D034AC252E880A44955078D6D4F12EDCDF6D03549F0 (B64)ousmGAL/0ZZdA0rCUuiApElVB41tTxLtzfbQNUnw

    try it here

    It becomes apparent that the hash is not as easy to break as "password" on its own.

    Nevertheless still possible with pre-computed hashes. You can see more here.

    Ciphers on the other hand constitutes of methods for performing encryption as well as decryption. Some ciphers you see in cryptoJs are your basic AES, DES, triple DES etc.

    Encoders are simply used for Encoding where encoding is very general. It is largely used to transform data so that another system can understand it. In the technology field, this is largely because every system architecture and technology has their own interpretations. Different applications will understand different encoding as per their need.

    In Summary,

    Encryption and Encoding are are designed 2 ways whereas PBKDF2 is a method of generating cryptographic keys (hashes) which are designed one way. Encoders are used to encode data into a form that can be transmitted or interpreted by another system.

    Putting it in context:

    If we want to store the password in a database we hash it because we do not need to know what the password is (no reversal required). However when we sent an encrypted mail to a friend we want to be able to reverse that encryption (decryption). Otherwise the content is lost. When the mail is sent, we added an attachment. The attachment is encoded in a way that other email clients can decode otherwise the other system cannot open up the attachment or will wrongly interpret the data sent.

    So Encoding and Encrypting are similar in that encoded text and encrypted text can both be reversed. However, encoded text are meant to be reversed by anyone or any system that gets its hand on the encoded text since the encoding schemes are publicly available but encrypted text such as ciphertext are meant to be reversed only by certain specified individuals i.e. people who possess the key or decryption algorithms. In our example above, we want our attachment to be interpreted by any system but we do not want the content of the email including the attachment to be opened by everyone.