Search code examples
reactjsencryptionpassword-encryptionencryption-symmetricencryption-asymmetric

Is there any way to get same encryption key while encrypting a same data?


Is there any way to get the same key while encrypting the same data,

For eg: If I have to encrypt a text "bacon" and the encrypted data looks like "e7b75a472b65bc4a42e7b3f78833a4d00040beba796062bf7c13d9533b149e5ec3784813dc20348fdf248d28a2982df85b83d1109623bce45f08238f6ea9bd9bb5f406427b2a40f969802635b8907a0a57944f2c12f334bd081d5143a357c173a611e1b64a"

This encrypted data changes for the same text "bacon"

Is there any way to get this same key as a permanent one for the word "bacon".


Solution

  • The input key material (the secret) doesn't change, but cryptr, a one man project on GitHub, uses a random salt to derive the encryption key using PBKDF2 and a random IV for the ciphertext. Both are prefixed to the ciphertext, which also contains a tag as it uses AES-GCM; hence the large ciphertext.

    You need to reprogram the simple cryptr module to use the same salt and IV to get the same ciphertext, but note that if you encrypt other plaintext messages using the same salt and IV you'd be immediately vulnerable against many-time pads and recalculation of the GMAC authentication key (part of GCM).

    Currently cryptr uses PBKDF2 with a static iteration count to calculate a key from a secret (which may or may not be a password?). It also uses an IV of 16 bytes instead of 96 bits / 12 bytes. This shows that the author doesn't fully understand the cryptography himself if you ask me. Don't use one-man cryptography, and if you want specific properties, program them yourself instead of relying on a higher level library.