Search code examples
encryptionerlangaes

Erlang returns empty binary for aes encryption


Erlang/OTP 22 [erts-10.6.4] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [hipe] [dtrace]

Eshell V10.6.4  (abort with ^G)

1> application:ensure_all_started(crypto).
{ok,[crypto]}

2> Key = <<1:128>>.
<<0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1>>

3> Text = <<"hello crypto">>.
<<"hello crypto">>

4> crypto:crypto_one_time(aes_128_ecb, Key, Text, true).
<<>>

5> Ref = crypto:crypto_init(aes_128_ecb, Key, true).
#Ref<0.3726173343.1880227841.208986>

6> crypto:crypto_update(Ref, Text).
<<>>

I don't know what I'm doing wrong here but as you can see above, crypto_one_time and crypto_update both return an empty binary when I try to encrypt something.


Solution

  • Example of use crypto:crypto_one_time/5:

    1> Key = <<1:128>>. 
    <<0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1>> 
    2> IV = <<0:128>>. 
    <<0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0>> 
    3> Txt = [<<"First bytes">>,<<"Second bytes">>]. 
    [<<"First bytes">>,<<"Second bytes">>] 
    4> crypto:crypto_one_time(aes_128_ctr, Key, IV, Txt, true).
    <<67,44,216,166,25,130,203,5,66,6,162,16,79,94,115,234, 197,94,253,16,144,151,41>> 
    

    Example of use crypto:crypto_one_time/4:

    1> Key = <<1:128>>. 
    <<0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1>>
    2> Txt = [<<"First bytes">>,<<"Second bytes">>].
    [<<"First bytes">>,<<"Second bytes">>]
    3> crypto:crypto_one_time(aes_128_ecb, Key,  Txt, true).   
    <<231,108,83,104,188,131,182,65,2,128,78,162,210,149,128,248>>