dersar00

dersar00

Encrypt string with my key

Hello!
I want to encrypt some string with my own key, how I can do it? I prefer to do it with some encryption technology that I can use to decrypt this in my js frontend.

Most Liked

voltone

voltone

This approach has several weaknesses:

  • ECB mode can very easily lead to data exposure if multiple messages are encrypted with the same key, or if a single message spans multiple blocks; use CBC or GCM mode, and a unique random IV for each message (to be transmitted along with the ciphertext)
  • Using SHA256 as a key derivation function (KDF) provides very little protection against brute-force attacks to recover the password; use a proven KDF, or a secure random key

Instead of building an encryption scheme from primitives, consider using high-level API such as JWE or Plug.Crypto (https://hex.pm/packages/plug_crypto)

muelthe

muelthe

I have been tinkering with a requirement to encrypt some data before storing in a database. Now I am extremely new to a lot of this and in all fairness my need is very small, but I found a lot of good references with the following that certainly helped: https://github.com/dwyl/phoenix-ecto-encryption-example.

Most likely it doesn’t fit your requirements, but just in case :smile:

cmkarlsson

cmkarlsson

An AES key must be 16 bytes long. Payload must be a in blocks of even 16 bytes but if using aes_ecb you should not encrypt more than 1 block (16 bytes). It is not secure to do so.

If using another crypto mode, such as aes_cbc you must pad it so that the payload is evenly divided by the block size but if you pad you must authenticate the crypto otherwise it is not secure. (See padding oracle)

And if you authenticate the crypto please make sure that you use a constant time compare when checking the authentication otherwise this can be utilized to crack the crypto.

Alternatively use a crypto with authentication such as AES-GCM.

EDIT:

I’ve had a look at the AES_ENCRYPT in MySQl (which I assume you are using?). They do pad the string but they don’t mention which padding algorithm, iv or what type of aes mode they are using but from some stackoverflow post it seems like they are using AES-ECB, padded with PKCS#5.

voltone

voltone

This approach does not really offer any benefits over symmetrical crypto, and will have significant performance impact.

The fact that the key stored in the Radius configuration files is notionally the “public” half of the key pair does not change the fact that anyone who has access to the plaintext config file will be able to decrypt all passwords. The fact that the “private” half is kept more secure just means someone with only the public key can’t encrypt new passwords, which presumably isn’t what you are concerned about.

wyrdforge

wyrdforge

Thanks for all the input :smile:

Though we decided against aes just now, I came up with the following solution to the mysql implementation of aes_ecb as a personal challenge, which works well in my test cases:

#
# Encrypt password with 128-bit aes and insert into database as hex value
#
@aes_blocksize 16

def encrypt(data, key) do
  :crypto.block_encrypt(
    :aes_ecb,
    mysql_key_pad(key),
    pkcs7_pad(data, @aes_block_size)
  )
  |> Base.encode16()
end

# PKCS7 Padding
defp pkcs7_pad(data, blocksize) do
  pad = blocksize - rem(byte_size(data), blocksize)
  data <> to_string(List.duplicate(pad, pad))
end

defp mysql_key_pad(key) when byte_size(key) < @aes_block_size do
  bits = 8 * (@aes_block_size - byte_size(key))
  key <> <<0 :: size(bits)>>
end

defp mysql_key_pad(key) when byte_size(key) == @aes_block_size do
  key |> :binary.bin_to_list()
end

defp mysql_key_pad (key) do
  bytelist = :binary.bin_to_list(key)
  bytes_to_pad = (@aes_block_size - rem(length(bytelist), @aes_block_size))

  bytelist ++ List.duplicate(<<0::8>>, bytes_to_pad)
  |> Enum.chunk_every(@aes_block_size)
  |> Enum.reduce(
      List.duplicate(<<0::8>>, @aes_block_size),
        fn chunk, acc ->
          :crypto.exor(acc, chunk)
        end
    )
end

You can also implement the functionality of :crypto.exor(acc, chunk) in Elixir with a recursive function like
do_xor_calculation(chunk1, chunk2, accumulator), as I did first, but with :crypto.exor/2 it is way more comfortable.

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
New
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
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
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

Other popular topics Top

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
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
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
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
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement