tkuehn
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.
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.
Trending in Questions
I’m aware of 2 such projects, are there others?
git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Hi everyone.
My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
Hello folks!
So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
How Can I Optimise Compile Time Dependencies
I have been building an elixir application for about 2 years now. Many modules and files ha...
New
Hi,
we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
Other Trending Topics
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with.
But I don’t use it anymore.
Serverless was the promis...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #performance










Showing Posts 1 to 9- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
D4no0
I am not familiar with how this API works, but is the third parameter
Plaintexta valid one?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
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
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)
al2o3cr
Further review turns up the
paddingoption, which mentions this important fact:You can see this happening with a longer-than-16-byte string:
The last byte is silently dropped because the block wasn’t full.
Explicitly passing a value for
paddingsolves the original issue:tkuehn
Thank you very much guys
garrison
That is quite a gotcha if you use it to encrypt some data and don’t double-check the result! Always read the docs, I guess
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.
garrison
I see, if it’s something that’s standardized that makes a lot more sense.