AES Decryption with key derivation function

Viewed 460

I am working on porting a decryption function from .NET to elixir as a proof of concept.

Can anyone give me some guidance on if this is doable with the Erlang crypto module? I have played around the block_decrypt functions but am never able to get the correct result. I am thinking my problem is coming from an incorrect key and IV.

I am unsure how to derive byte data from the encrypted plain text value to pass into block_decrypt.

Here is the elixir code I'm using to attempt to decrypt:

defmodule TestApp.Decrypt do
  @iv <<30,64,180,159,172,197,92,10,197,3,39,75,53,92,93,37>>

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


  def decrypt(data, key) do
    IO.puts "WOrking to decrypt #{data} using #{key}"
    padded = :crypto.block_decrypt(:aes_cbc256, key, @iv, :base64.decode(data))
    unpad(padded)
  end
end

I have a 32 byte key I'm trying to pass in but am getting this error:

Erlang error: :notsup 

The Crypto Library points that error to the fact that dirty scheduler wasn't enabled on my erlang build, but I don't know if I'm going about this in the right direction before I look into that.

1 Answers

The erlang crypto errors are not very helpful. It looks like you might have the wrong cipher specified. Instead of aes_cbc256 you probably want aes_256_cbc. See http://erlang.org/doc/man/crypto.html#block_decrypt-4

You may also like to check-out a project I'm working on called Apoc which encapsulates this stuff into a more "Elixir like" API: https://github.com/coderdan/apoc

It supports AES in GCM mode so far but I would love to have some contributions for other ciphers!

Related