# Porting node.js encryption function to Elixir

**URL:** <https://forum.elixirforum.com/t/porting-node-js-encryption-function-to-elixir/39493>\
**Category:** Questions\
**Tags:** crypto\
**Created:** [May 7, 2021, 8:52pm UTC](https://forum.elixirforum.com/t/porting-node-js-encryption-function-to-elixir/39493 "2021-05-07T20:52:45Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![peoj](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/peoj/32/19305_2.png) [@peoj](https://forum.elixirforum.com/u/peoj)\
**Post date:** [May 7, 2021, 8:52pm UTC](https://forum.elixirforum.com/t/porting-node-js-encryption-function-to-elixir/39493/1 "2021-05-07T20:52:45Z")

</div>

Hi folks,

I’m attempting to port this JS code into its Erlang/Elixir equivalent:

```js
const key = "mysecretkey";

function decryptString(encryptedString) {
    const algorithm = 'aes256';
    const decipher = crypto.createDecipher(algorithm, key);
    return decipher.update(encryptedString, 'hex', 'utf8') +
      decipher.final('utf8');
  }

```

This is as far as I have gotten so far in E land:

```elixir
iex> key = "mysecretkey"
iex> encrypted_text = "myencryptedtext-changed-for-this-example"
iex> :crypto.crypto_one_time(:aes_256_ecb, key, encrypted_text, encrypt: false)
** (ErlangError) Erlang error: {:badarg, {'api_ng.c', 143}, 'Bad key size'}
    (crypto 4.8.3) :crypto.ng_crypto_one_time_nif(:aes_256_ecb, "mysecretkey", "", "myencryptedtext-changed-for-this-example", false, :undefined)

```

Clearly the precise example I’ve given will _never_ work since I’ve redacted both the key and encrypted text. However the error message I’ve included above is the real thing.

Does anyone know what I’m missing?

---

<div class="post-metadata">

**Author:** ![hauleth](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/hauleth/32/18942_2.png) [@hauleth](https://forum.elixirforum.com/u/hauleth)\
**Post date:** [May 7, 2021, 10:19pm UTC](https://forum.elixirforum.com/t/porting-node-js-encryption-function-to-elixir/39493/2 "2021-05-07T22:19:32Z")

</div>

AES256 require key to be **exactly** 32 byte long, your key is 11 bytes, 21 bytes too short. JS code probably does some padding to achieve what you want.

**However:**

For gods sake, do not use this code. Burry it in the dessert, and wear gloves. After that burn all your clothes.

Jokes aside - this is terrible way to do cryptography and encrypt data. Just don’t do it that way. If you have “AES” written in your code, then you are probably vulnerable. It is even hard to list what is wrong there:

- low entropy keys - just use KDF for dear gods
- no padding for data
- no authentication of data
- using ECB mode for encryption

This is nowhere to be even considered to be a remotely reasonable implementation. It is just insecure.

---

<div class="post-metadata">

**Author:** ![peoj](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/peoj/32/19305_2.png) [@peoj](https://forum.elixirforum.com/u/peoj)\
**Post date:** [May 8, 2021, 5:12pm UTC](https://forum.elixirforum.com/t/porting-node-js-encryption-function-to-elixir/39493/3 "2021-05-08T17:12:59Z")

</div>

Thanks for the suggestion and for the wisdom re the code. Yes I’m aware it’s flawed (actually I’m porting it in order to eventually remove it).

---

<div class="post-metadata">

**Author:** ![f0rest8](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/f0rest8/32/38551_2.png) [@f0rest8](https://forum.elixirforum.com/u/f0rest8)\
**Post date:** [May 9, 2021, 3:59am UTC](https://forum.elixirforum.com/t/porting-node-js-encryption-function-to-elixir/39493/4 "2021-05-09T03:59:04Z")

</div>

Not sure what you’re specific encryption needs are, but if you’re going to implement encryption in Elixir then you might want to consider using the [enacl](https://github.com/jlouis/enacl) library.

If you have simpler needs depending on your use case, i.e. transparent database field encryption with Ecto (symmetric), then you can look at a library like [cloak/cloak\_ecto](https://github.com/danielberkompas/cloak).

Hope it helps ❤

---

<div class="post-metadata">

**Author:** ![peoj](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/peoj/32/19305_2.png) [@peoj](https://forum.elixirforum.com/u/peoj)\
**Post date:** [May 17, 2021, 10:05pm UTC](https://forum.elixirforum.com/t/porting-node-js-encryption-function-to-elixir/39493/5 "2021-05-17T22:05:21Z")

</div>

I got a bit closer by trimming the key to the suggested length, thank you.

Unfortunately I’m clearly doing something else wrong too because the returned value is quite clearly not cleartext ;).

Any further hints on where to look? As you can probably already tell I’m fairly lacking in knowledge in this area!

---

<div class="post-metadata">

**Author:** ![mattbaker](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mattbaker/32/3698_2.png) [@mattbaker](https://forum.elixirforum.com/u/mattbaker)\
**Post date:** [May 19, 2021, 2:53am UTC](https://forum.elixirforum.com/t/porting-node-js-encryption-function-to-elixir/39493/6 "2021-05-19T02:53:02Z")

</div>

I like crypto! And I like puzzles, and I had a suspicion Node was doing something silly, so I chased this a bit. I’ll say two things right off the bat:

First, this would be confusing to _anyone_. Node made some choices that favored ease of use, and sacrificed clarity (and security!) in the process. Node is doing things hidden from you that you couldn’t possibly have known about without knowing exactly what to search for. With a little experience with cryptography concepts it’s not bad, but if you’re new to this stuff (and you mentioned you are) this is really hard to solve without help.

Second, @hauleth is right that a revamp of how you encrypt things is warranted, but we don’t always have the luxury of doing that with legacy data! Best practices here do not solve the problem in front of you. For what it’s worth, I don’t think you’re encrypting in ECB in Node, I think you’re actually using CBC.

With all that said, to make any progress there are some things to figure out before we can decrypt the data Node is producing in Elixir.

## Question 1: what block cipher mode is Node using?

You specified `aes256` in your JS code and `aes_256_ecb` in Elixir. The string “aes256” doesn’t really tell us the whole story, because encryption is not just about which encryption algorithm is being used, it’s also about how each block of data is operated on during the encryption or decryption process. You’ve selected “ECB” (electronic code book) but there are others, like “CBC” (cipher block chaining). You really need to know which one is being used if you want to decrypt your data.

If you head down the rabbit hole far enough with the [Node docs](https://nodejs.org/api/crypto.html#crypto_crypto_createcipher_algorithm_password_options) you’ll see that `aes256` value you’re passing is from a list of ciphers provided by OpenSSL. Unfortunately, the string `aes256` doesn’t tell us the block mode.

I couldn’t find a clear explanation of what `aes256` truly maps to so I just ran `openssl enc -aes256` to see what would happen. The first thing you’ll see is

```elixir
enter aes-256-cbc encryption password:

```

So, that makes me think when you say “aes256” in your code in Node it’s really a shortcut for aes-256-cbc.

That means the first thing you have wrong is the algorithm, instead of ECB you should be using `:aes_256_cbc.`

## Question 2: But if it’s CBC, what’s the initialization vector?

[This wikipedia page](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Confidentiality_only_modes) talks a bit about block cipher modes. The primary thing to notice is the difference between ECB and CBC mode. You could also google other resources explaining block cipher modes that might be more clear than that wikipedia page.

If you look at the pictures describing how ECB works, you’ll see decryption needs two inputs: the key and some ciphertext (aka encrypted data).

You’ll also see that CBC expects _three_ inputs. The key, the ciphertext, and something called an “initialization vector” (IV) to get things started.

Since you are (apparently) using CBC but _not_ supplying an IV in your JS code, the only conclusion is that Node is generating an IV for you, and it must be doing it deterministically (not a great thing in the crypto world, one of the reasons the Node docs push people toward `createCipheriv`).

Luckily you’re not only one dealing with this. [Here’s someone trying to solve the same problem in Ruby](https://stackoverflow.com/questions/57068729/how-can-i-decrypt-data-encrypted-by-nodes-deprecated-createcipher-in-ruby).

Summarizing the Stack Overflow post above: Node takes the “password” you provide (of any length) and uses _that_ to produce an encryption key and an IV. So it turns out that not only is Node secretely making an IV for you, it’s also creating an encryption key that’s based on your “password” but not literally your password. In your example, the decryption key is not actually “mysecretkey” (no surprise, since it’s too short to be an encryption key!)

That’s all outlined in the Stack Overflow answer I linked to, which I recommend you read.

If we copy what they did in Ruby in Elixir, that process could look something like this:

```elixir
a = :crypto.hash(:md5, password)
b = :crypto.hash(:md5, a <> password)
c = :crypto.hash(:md5, b <> password)

iv = a <> b
key = c

```

## And finally, encoding

Last but not least, your JS code makes me think that your encrypted data is probably being passed around encoded as base 16 (hex), for example are you providing something like “f07258ace89d16d847cb0ec520b19438” as input when you try to decrypt data?

If so, that means you need to decode the hex string before you try to decrypt it. You can use `Base.decode16!(encrypted_text, case: :lower)` for this.

## Putting it all together

So, if I had to summarize, it’d be this: Node is trying to be helpful by hiding a bunch of things from you, and that works if you never have to decrypt something outside the Node ecosystem, but it’s a crappy design if you do. In their defense, the `createCipher` function you’re using there has been deprecated in favor of a function that takes a true key (not a “password”) and an explicit IV value (you must generate it, they will not do it for you). I’m very happy they’ve deprecated the function you’re using.

The good news is you can definitely re-create what Node did behind the scenes so that you can decrypt your data in Elixir.

First, here’s an example of some JS code that I’m guessing is close to your own. We can use it to generate test data.

```elixir
const crypto = require('crypto')

function decryptString(key, encryptedString) {
  const algorithm = 'aes256';
  const decipher = crypto.createDecipher(algorithm, key);
  return decipher.update(encryptedString, 'hex', 'utf8') +
    decipher.final('utf8');
}

function encryptString(key, plaintext) {
  const algorithm = 'aes256';
  const encipher = crypto.createCipher(algorithm, key);
  const encrypted = Buffer.concat([encipher.update(plaintext), encipher.final()]);
  return encrypted.toString("hex")
}

const password = "mysecretkey"
const plaintext = "hello world"

console.log("Plaintext: ", plaintext)

console.log("Password: ", password)

var encrypted = encryptString(password, plaintext)

console.log("Encrypted: ", encrypted)

var decrypted = decryptString(password, encrypted)

console.log("Decrypted: ", decrypted)

```

That produces the following output:

```elixir
Plaintext: hello world
Password: mysecretkey
Encrypted: 686ca8793bf5e7317cfd451aa81b72cf
Decrypted: hello world

```

So in Elixir our goal is to decrypt the string “686ca8793bf5e7317cfd451aa81b72cf” by mimicking Node. If we succeed then we know we’ve copied Node’s approach.

```elixir
# As we discovered earlier, you want CBC:
algo = :aes_256_cbc

# Not literally your encryption key, just a value Node uses to generate a key:
password = "mysecretkey"

# Calling this "encoded" ciphertext because it looks like you're using hex strings:
encoded_ciphertext = "686ca8793bf5e7317cfd451aa81b72cf"

# Your ciphertext hex-decoded
ciphertext = Base.decode16!(encoded_ciphertext, case: :lower)

# Re-creating how Node generates the IV and key values from a "password" string
a = :crypto.hash(:md5, password)
b = :crypto.hash(:md5, a <> password)
c = :crypto.hash(:md5, b <> password)

iv = a <> b
key = c

# Decrypting the data. Notice that we're also specifying the padding so it's stripped.
:crypto.crypto_one_time(algo, iv, key, ciphertext, [encrypt: false, padding: :pkcs_padding])
|> IO.inspect()

```

That prints out “hello world” on my machine.

I hope that helps you get unstuck and away from Node 🙂

If I had to suggest a new approach once you’re able to migrate off your legacy Node code:

- Use a full randomly generated key, don’t generate one like Node does based on a short “password” string
- Don’t feel tempted to use `aes_256_ecb` because it’s easier. Something like `aes_256_cbc` is fine as far as I know, and there are others.
- Erlang’s built-in crypto stuff is, to my knowledge, perfectly fine to use
- We didn’t find Cloak’s abstraction particularly useful, but it’s possible you might, certainly worth reading up on it once you get there.

---

<div class="post-metadata">

**Author:** ![mindok](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mindok/32/13538_2.png) [@mindok](https://forum.elixirforum.com/u/mindok)\
**Post date:** [May 19, 2021, 4:15am UTC](https://forum.elixirforum.com/t/porting-node-js-encryption-function-to-elixir/39493/7 "2021-05-19T04:15:41Z")

</div>

This has to be answer of the year!!! Awesome work.

---

<div class="post-metadata">

**Author:** ![emeryotopalik](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/emeryotopalik/32/22978_2.png) [@emeryotopalik](https://forum.elixirforum.com/u/emeryotopalik)\
**Post date:** [May 20, 2021, 1:21am UTC](https://forum.elixirforum.com/t/porting-node-js-encryption-function-to-elixir/39493/8 "2021-05-20T01:21:35Z")

</div>

> [@mattbaker](#):
>
> Summarizing

Aren’t you supposed to be on vacation?

---

<div class="post-metadata">

**Author:** ![peoj](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/peoj/32/19305_2.png) [@peoj](https://forum.elixirforum.com/u/peoj)\
**Post date:** [May 23, 2021, 3:44pm UTC](https://forum.elixirforum.com/t/porting-node-js-encryption-function-to-elixir/39493/9 "2021-05-23T15:44:38Z")

</div>

What an incredible answer. Thanks so much for helping and teaching me a thing or two along the way.

---

<div class="post-metadata">

**Author:** ![hauleth](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/hauleth/32/18942_2.png) [@hauleth](https://forum.elixirforum.com/u/hauleth)\
**Post date:** [May 24, 2021, 8:35am UTC](https://forum.elixirforum.com/t/porting-node-js-encryption-function-to-elixir/39493/10 "2021-05-24T08:35:11Z")

</div>

> [@mattbaker](#):
>
> Something like `aes_256_cbc` is fine as far as I know

Please, do not do that. Encryption without authentication is pointless as attacker can manipulate the plaintext without any problems (without knowing the plaintext):

```elixir
iex> key = :crypto.strong_rand_bytes(32)
iex> <<a, rest::binary>> = iv = :crypto.strong_rand_bytes(16)
iex> plaintext = "Abba"
iex(46)> iv1 = Bitwise.bxor(a, 3) <> rest
iex(47)> ct = :crypto.crypto_one_time(:aes_256_cbc, key, iv, plaintext, encrypt: true, padding: :pkcs_padding)
iex(48)> :crypto.crypto_one_time(:aes_256_cbc, key, iv1, ct, encrypt: false, padding: :pkcs_padding)
"Bbba"

```

So as you can see, I can change first letter from `A` to `B` without any problems. This is **huge** problem as you no longer can trust the cipher text.

In short - do not use non-AEAD ciphers, never.

---

<div class="post-metadata">

**Author:** ![lud](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/lud/32/14382_2.png) [@lud](https://forum.elixirforum.com/u/lud)\
**Post date:** [May 24, 2021, 8:57am UTC](https://forum.elixirforum.com/t/porting-node-js-encryption-function-to-elixir/39493/11 "2021-05-24T08:57:16Z")

</div>

The more I learn crypto, the more there is to learn ☹ I know it is true for most domains but in that case it is almost overwhelming.

---

<div class="post-metadata">

**Author:** ![hauleth](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/hauleth/32/18942_2.png) [@hauleth](https://forum.elixirforum.com/u/hauleth)\
**Post date:** [May 24, 2021, 9:29am UTC](https://forum.elixirforum.com/t/porting-node-js-encryption-function-to-elixir/39493/12 "2021-05-24T09:29:59Z")

</div>

That is why if you have these thee letters anywhere in your code `AES` then you are probably vulnerable. If you want to do it right you have few options:

- If there is no additional requirements - encrypt only on transport layer, like TLS, and call it a day
- Use good token implementations, like [PASETO](https://paseto.io) ([JOSE (used by JWT) is plagued with problems](https://paragonie.com/blog/2017/03/jwt-json-web-tokens-is-bad-standard-that-everyone-should-avoid))
- Use [foolproof API from entrusted authors](https://doc.libsodium.org/secret-key_cryptography/secretbox) instead of building the protocol on your own

Doing crypto on your own is **enormously hard**.

---

<div class="post-metadata">

**Author:** ![mattbaker](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mattbaker/32/3698_2.png) [@mattbaker](https://forum.elixirforum.com/u/mattbaker)\
**Post date:** [May 24, 2021, 4:01pm UTC](https://forum.elixirforum.com/t/porting-node-js-encryption-function-to-elixir/39493/13 "2021-05-24T16:01:20Z")

</div>

This is so interesting! Thanks for sharing that about CBC, I learned something new too!

I like that you pointed to more resources in your follow up comment, since knowing what _not_ to do is only have the battle 😃

---

<div class="post-metadata">

**Author:** ![tomciopp](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/tomciopp/32/4900_2.png) [@tomciopp](https://forum.elixirforum.com/u/tomciopp)\
**Post date:** [May 24, 2021, 4:34pm UTC](https://forum.elixirforum.com/t/porting-node-js-encryption-function-to-elixir/39493/14 "2021-05-24T16:34:00Z")

</div>

Agreed mostly, if you’re going to use AES, make sure it is AES-GCM. However, more people seem to be advocating for XSalsa20-Poly1305 these days. Use libsodium or NaCl bindings if you can. Write the least amount of code to interface with existing battle tested libraries.

If you find yourself actually implementing algorithms and you’ve never done it before stop. You’re 100% likely to do something incorrectly and your system will be insecure.

---

<div class="post-metadata">

**Author:** ![lud](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/lud/32/14382_2.png) [@lud](https://forum.elixirforum.com/u/lud)\
**Post date:** [May 24, 2021, 10:50pm UTC](https://forum.elixirforum.com/t/porting-node-js-encryption-function-to-elixir/39493/15 "2021-05-24T22:50:29Z")

</div>

That Paragon website is a goldmine.

---

<div class="post-metadata">

**Author:** ![hauleth](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/hauleth/32/18942_2.png) [@hauleth](https://forum.elixirforum.com/u/hauleth)\
**Post date:** [May 25, 2021, 10:42am UTC](https://forum.elixirforum.com/t/porting-node-js-encryption-function-to-elixir/39493/16 "2021-05-25T10:42:12Z")

</div>

> [@mattbaker](#):
>
> Thanks for sharing that about CBC

Just be aware, that it applies to all unauthenticated encryption, not only CBC. Of course you can build authenticated protocol using CBC, but [that is still full of pitfalls](https://crypto.stackexchange.com/a/205/28013).

> [@tomciopp](#):
>
> However, more people seem to be advocating for XSalsa20-Poly1305 these days.

Small correction, XChaCha20 not XSalsa20. The difference is compression function that optimises better on the X86-64 CPUs, as there is reduced dependency between rows. [However XChaCha20 is still not standardised by IETF and due to that is not included in OpenSSL](https://github.com/openssl/openssl/issues/5523).

> [@tomciopp](#):
>
> Use libsodium or NaCl bindings if you can. Write the least amount of code to interface with existing battle tested libraries.

That.

> [@tomciopp](#):
>
> If you find yourself actually implementing algorithms and you’ve never done it before stop.

Actually not really. Implementing most of the cryptographic primitives (with exception to AES due to the design issues) is quite easy and straightforward. What is hard is how to implement **protocols** , as this is where most of the cryptographic problems is happening. Erlang has quite nice API for using the primitives, which is simple, the hard part is how to join primitives with other to have secure protocol.

---

<div class="post-metadata">

**Author:** ![chriscohoat](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/chriscohoat/32/8053_2.png) [@chriscohoat](https://forum.elixirforum.com/u/chriscohoat)\
**Post date:** [November 13, 2023, 12:49am UTC](https://forum.elixirforum.com/t/porting-node-js-encryption-function-to-elixir/39493/17 "2023-11-13T00:49:57Z")

</div>

I know I’m a bit late to the game here … but I spent all day trying to port this related (and reasonably-looking NodeJS call):

```elixir
const decryptedData = CryptoJS.AES.decrypt(
     ciphertext,
     passphrase
).toString(CryptoJS.enc.Utf8);

```

to Elixir. In my case I was passing in a Base64 encoded string with a `Salted` prefix.

In crypto-js there’s a function called `parse`:

[https://github.com/brix/crypto-js/blob/develop/src/cipher-core.js#L610](https://github.com/brix/crypto-js/blob/develop/src/cipher-core.js#L610)

My implementation (thanks a lot @mattbaker for your help):

```elixir
defmodule SSODecryption do
  def parse(openSSLStr) do
    {:ok, ciphertext_b64} = Base.decode64(openSSLStr)

    <<salted_indicator::binary-size(8), salt::binary-size(8), ciphertext::binary>> =
      ciphertext_b64

    case salted_indicator do
      "Salted__" ->
        {salt, ciphertext}

      _ ->
        {:error, "No salt found"}
    end
  end

  def parse_and_decrypt(openSSLStr, passphrase, algo \\ :aes_256_cbc) do
    case parse(openSSLStr) do
      {:error, reason} ->
        {:error, reason}

      {salt, ciphertext} ->
        {key, iv} = derive_key_and_iv(passphrase, salt)
        perform_decryption(ciphertext, iv, key, algo)
    end
  end

  defp derive_key_and_iv(passphrase, salt) do
    # Concatenate the passphrase and salt as the starting point for hash computations.
    passphrase_salt = passphrase <> salt

    # Compute the first hash using the passphrase and salt.
    a = :crypto.hash(:md5, passphrase_salt)

    # For subsequent hashes, concatenate the result of the previous hash with the original passphrase and salt.
    b = :crypto.hash(:md5, a <> passphrase_salt)
    c = :crypto.hash(:md5, b <> passphrase_salt)

    # The IV is derived from the first two hash outputs.
    iv = a <> b

    # The key is derived from the third hash output.
    key = c

    {key, iv}
  end

  defp perform_decryption(ciphertext, iv, key, algo) do
    try do
      case :crypto.crypto_one_time(algo, iv, key, ciphertext,
             encrypt: false,
             padding: :pkcs_padding
           ) do
        result when is_binary(result) ->
          case Jason.decode(result) do
            {:ok, decoded} -> {:ok, decoded}
            {:error, _reason} -> {:error, "Failed to decode JSON"}
          end

        _reason ->
          {:error, "Decryption failed for an unknown reason"}
      end
    rescue
      _exception ->
        {:error, "Decryption failed"}
    end
  end
end

```

I’m not positive about the error handling and it only works for the salted case right now, but it seems to let me move some NodeJS code away. Thanks again for the answers on this thread.
