Niranjan

Niranjan

Help with crypto module - crypto_one_time method - (ArgumentError) argument error Issue

Hi Everyone,

Any help will be really appreciated!

I am new to elixir and was trying the crypto module for aes_128_cbc but facing issue

not sure why for eg. string “Hello” - it gives argument error while decryption and for large string eg. “testing the aes_cbc_128 encryption logic” first few words are getting dropped.

encryption process needs to be iv + plaintext with secret_key in AES 128 CBC mode and decrypt with the same secret_key.

sample I/O -

iex(5)> Aes.encrypt("hello")
"Wz0sXikhrijDsYjzz3AhnQ=="
iex(6)> Aes.decrypt("Wz0sXikhrijDsYjzz3AhnQ==")
** (ArgumentError) argument error

iex(6)> Aes.encrypt("testing the aes_cbc_128 encryption logic")
"R9OxXtbhEyyklWxGc6dffsUkXvQVTHnAeBhlSJgQ8nFpOlEYm2fMJuDAhXSKDEa8"
iex(7)> Aes.decrypt("R9OxXtbhEyyklWxGc6dffsUkXvQVTHnAeBhlSJgQ8nFpOlEYm2fMJuDAhXSKDEa8")
"cbc_128 encryption logic"
iex(8)> 

Erlang/OTP 23 [erts-11.1.8]
Interactive Elixir (1.9.1)

–Below is the code snippet for the same

defmodule Aes do
  # Use AES 128 Bit Keys for Encryption.
  @block_size 16

  def encrypt(plaintext) do
    # create random Initialisation Vector
    iv = :crypto.strong_rand_bytes(16)
    # sample secret_key is a 32 bit hex string 
    secret_key = Base.decode16!("548F11DE7F4393EAC0BB2391797AF7B5")
    plaintext = pad(plaintext, @block_size)
    encrypted_text = :crypto.crypto_one_time(:aes_128_cbc, secret_key, iv, plaintext, true )
    :base64.encode(encrypted_text)
  end

  def decrypt(ciphertext) do
    secret_key = Base.decode16!("548F11DE7F4393EAC0BB2391797AF7B5")
    ciphertext = :base64.decode(ciphertext)
    <<iv::binary-16, ciphertext::binary>> = ciphertext
    decrypted_text = :crypto.crypto_one_time(:aes_128_cbc, secret_key, iv, ciphertext, false )
    unpad(decrypted_text)
  end

  def unpad(data) do
    to_remove = :binary.last(data)
    :binary.part(data, 0, byte_size(data) - to_remove)
  end

# PKCS5Padding
  def pad(data, block_size) do
    to_add = block_size - rem(byte_size(data), block_size)
    data <> :binary.copy(<<to_add>>, to_add)
  end
end

Thanks!
Niranjan

Marked As Solved

NobbZ

NobbZ

I might missremember things, though :crypto.crypto_one_time/4,5 does not carry the iv in the result.

You need to prepend it manually if you want to have it part of the message.

Also Liked

Niranjan

Niranjan

Thanks a Lot @NobbZ - Solution worked as expected.

The updated code snippet I used -

def encrypt(plaintext) do
    # create random Initialisation Vector
    iv = :crypto.strong_rand_bytes(16)
    # sample secret_key is a 32 bit hex string 
    secret_key = Base.decode16!("548F11DE7F4393EAC0BB2391797AF7B5")
    plaintext = pad(plaintext, @block_size)
    encrypted_text = :crypto.crypto_one_time(:aes_128_cbc, secret_key, iv, plaintext, true )
    encrypted_text = ( iv <>  encrypted_text )
    :base64.encode(encrypted_text)
  end

before base64’ing I concatenated with IV.
Thanks,
Niranjan

Niranjan

Niranjan

@McTheels Please check if you are using Erlang version 23 and above - crypto_one_time/5 is supported in this version .

Where Next?

Popular in Questions Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
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
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; somethi...
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
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43622 214
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52341 488
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 47930 226
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

We're in Beta

About us Mission Statement