5
votes

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
It would be better if you post the code used to encrypt, sample unencrypted and encrypted text (hexdump), and the code you wrote to decrypt which didn't work. I successfully implemented decryption functions for a client where the data was being encrypted by some crypto module in Perl, so as long as your algorithm is implemented by Erlang's crypto, it should be possible. - Dogbert
@Dogbert, thanks for the input. Ill put something together and update my question. - Botonomous
@Dogbert Edited Question. - Botonomous
In the crypto docs it says May throw exception notsup in case the chosen Type is not supported by the underlying OpenSSL implementation. - ryanwinchester
fwiw, I can run it without an exception if I remove unpad: s.ryanwinchester.ca/2W2n0L0H062v - ryanwinchester

1 Answers

0
votes

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!