Plug v1.5.0-rc.0 Plug.Crypto.MessageEncryptor View Source

MessageEncryptor is a simple way to encrypt values which get stored somewhere you don’t trust.

The encrypted key, initialization vector, cipher text, and cipher tag are base64url encoded and returned to you.

This can be used in situations similar to the Plug.Crypto.MessageVerifier, but where you don’t want users to be able to determine the value of the payload.

Example

secret_key_base = "072d1e0157c008193fe48a670cce031faa4e..."
encrypted_cookie_salt = "encrypted cookie"
encrypted_signed_cookie_salt = "signed encrypted cookie"

secret = KeyGenerator.generate(secret_key_base, encrypted_cookie_salt)
sign_secret = KeyGenerator.generate(secret_key_base, encrypted_signed_cookie_salt)

data = "José"
encrypted = MessageEncryptor.encrypt(data, secret, sign_secret)
decrypted = MessageEncryptor.decrypt(encrypted, secret, sign_secret)
decrypted # => {:ok, "José"}

Link to this section Summary

Functions

Decrypts a message using authenticated encryption

Encrypts a message using authenticated encryption

WARNING: This function is deprecated in favor of encrypt/3. Encrypts and signs a message

WARNING: This function is deprecated in favor of decrypt/3. Decrypts and verifies a message

Link to this section Functions

Link to this function decrypt(encrypted, secret, sign_secret) View Source

Decrypts a message using authenticated encryption.

Link to this function encrypt(message, secret, sign_secret) View Source

Encrypts a message using authenticated encryption.

Link to this function encrypt_and_sign(message, secret, sign_secret, cipher \\ nil) View Source

WARNING: This function is deprecated in favor of encrypt/3. Encrypts and signs a message.

Link to this function verify_and_decrypt(encrypted, secret, sign_secret, cipher \\ nil) View Source

WARNING: This function is deprecated in favor of decrypt/3. Decrypts and verifies a message.

We need to verify the message in order to avoid padding attacks. Reference: http://www.limited-entropy.com/padding-oracle-attacks