tkuehn

tkuehn

AES256 CBC encryption returning empty string

Hi everyone.
I’m currently looking into AES256 CBC and how to encrypt/decrypt plaintext using Elixir. The Erlang crypto module provides the crypto_one_time/5 function which seems like the right function to use.
https://www.erlang.org/docs/28/apps/crypto/crypto#crypto_one_time/5
The code I used:

iv = :crypto.strong_rand_bytes(16)
key = :crypto.strong_rand_bytes(32)
data_to_encrypt = "the_plaintext"
:crypto.crypto_one_time(:aes_256_cbc, key, iv, data_to_encrypt, true)

The problem I’m facing is that the crypto_one_time/5 function returns an empty string.

crypto.info/1 returns:

:crypto.info
%{
  otp_crypto_version: ~c"5.5.1",
  compile_type: :normal,
  link_type: :dynamic,
  cryptolib_version_compiled: ~c"OpenSSL 3.3.2 3 Sep 2024",
  cryptolib_version_linked: ~c"OpenSSL 3.4.0 22 Oct 2024",
  fips_provider_available: false
}

aec_256_cbc gets listed when using :crypto.supports()

:crypto.supports[:ciphers]
[:chacha20, :aes_256_ofb, :aes_192_ofb, :aes_128_ofb, :sm4_ctr, :sm4_ofb,
 :sm4_cfb, :sm4_ecb, :sm4_cbc, :blowfish_ecb, :blowfish_ofb64, :blowfish_cfb64,
 :blowfish_cbc, :des_ede3_cfb, :des_ecb, :des_cfb, :des_cbc, :rc4, :rc2_cbc,
 :aes_128_cbc, :aes_192_cbc, :aes_256_cbc, :aes_128_cfb128, :aes_192_cfb128,
 :aes_256_cfb128, :aes_128_cfb8, :aes_192_cfb8, :aes_256_cfb8, :aes_128_ecb,
 :aes_192_ecb, :aes_256_ecb, :sm4_gcm, :sm4_ccm, :chacha20_poly1305,
 :aes_256_gcm, :aes_256_ccm, :aes_192_gcm, :aes_192_ccm, :aes_128_gcm,
 :aes_128_ccm, :aes_256_ctr, :aes_192_ctr, :aes_128_ctr, :des_ede3_cbc,
 :aes_cbc, :aes_ccm, :aes_cfb128, :aes_cfb8, :aes_ctr, :aes_ecb, ...]

Any help would be greatly appreciated.

Marked As Solved

al2o3cr

al2o3cr

Further review turns up the padding option, which mentions this important fact:

This option handles padding in the last block. If not set, no padding is done and any bytes in the last unfilled block is silently discarded.

You can see this happening with a longer-than-16-byte string:

iex(28)> data_to_encrypt = "the_plaintext1234"
"the_plaintext1234"

iex(29)> byte_size(data_to_encrypt)
17

iex(30)> :crypto.crypto_one_time(:aes_256_cbc, key, iv, data_to_encrypt, true)
<<112, 120, 31, 19, 167, 197, 128, 142, 183, 99, 178, 94, 193, 254, 84, 224>>

iex(31)> byte_size(v(30))
16

The last byte is silently dropped because the block wasn’t full.

Explicitly passing a value for padding solves the original issue:

iex(26)> data_to_encrypt = "the_plaintext"
"the_plaintext"
iex(27)> :crypto.crypto_one_time(:aes_256_cbc, key, iv, data_to_encrypt, encrypt: true, padding: :pkcs_padding)
<<128, 42, 49, 230, 33, 26, 157, 34, 189, 47, 164, 21, 246, 50, 96, 230>>

Also Liked

tkuehn

tkuehn

Thank you for your quick response.
It’s the data which should get encrypted.
I’ll edit the question to make more clear.

al2o3cr

al2o3cr

There’s something going on here with the length of the input - making the plaintext longer eventually gives a non-empty result, at exactly 16 bytes :thinking:

iex(19)> data_to_encrypt = "the_plaintext1"
"the_plaintext1"

iex(20)> :crypto.crypto_one_time(:aes_256_cbc, key, iv, data_to_encrypt, true)
""

iex(21)> data_to_encrypt = "the_plaintext12"
"the_plaintext12"

iex(22)> :crypto.crypto_one_time(:aes_256_cbc, key, iv, data_to_encrypt, true)
""

iex(23)> data_to_encrypt = "the_plaintext123"
"the_plaintext123"

iex(24)> :crypto.crypto_one_time(:aes_256_cbc, key, iv, data_to_encrypt, true)
<<112, 120, 31, 19, 167, 197, 128, 142, 183, 99, 178, 94, 193, 254, 84, 224>>

iex(25)> byte_size(data_to_encrypt)
16
garrison

garrison

16 bytes (128 bits) is the block size of AES, so perhaps you are expected to pad the input yourself? (I don’t know much about cryptography)

hauleth

hauleth

In general that is an API that should not be used if you aren’t aware what you are doing (so unless you are cryptography expert or you are trying to follow well known protocol description). Otherwise you will burn yourself.

Where Next?

Popular in Questions Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
Kagamiiiii
Student &amp; New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New

Other popular topics Top

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement