To ensure that data encryption and decryption processes are secure, minimizing the risk of data being compromised
defmodule InsecureCipher do
def encrypt(data, key, iv) do
:crypto.block_encrypt(:aes_cbc128, key, iv, data)
end
def decrypt(ciphertext, key, iv) do
:crypto.block_decrypt(:aes_cbc128, key, iv, ciphertext)
end
end
This code snippet is using the :aes_cbc128 mode for the block cipher which is considered insecure due to vulnerabilities like the padding oracle attack. It makes it possible for an attacker to decrypt data without knowing the key.
defmodule SecureCipher do
def encrypt(data, key, iv, aad) do
:crypto.crypto_one_time(:aes_gcm, key, iv, {aad, data}, :encrypt)
end
def decrypt(ciphertext_with_tag, key, iv, aad) do
:crypto.crypto_one_time(:aes_gcm, key, iv, {aad, ciphertext_with_tag}, :decrypt)
end
end
This secure code example uses the AES GCM mode for the block cipher which provides authenticated encryption. It also adds data integrity through the use of additional authenticated data (AAD). This makes it secure against attacks like the padding oracle attack.