How to set a 24 byte length key to elixir/erlang block_encrypt/4 function using des_ede3 as mode of encryption

久未见 提交于 2020-01-06 09:52:46

问题


Am trying to send data to an external api that expects encrypted data using 3DES encryption but am having issues understanding how to pass my api key as the key field to erlangs des_ede3 cipher.

According to erlangs cipher docs des_ede3 expects 3 keys that are all 8 bytes in length. How can i pass my 24 byte api key as the key to elixir/erlang :cryptoblock_encrypt/4 function

*** how can i pass key to block_encrypt/4 ***
key = "123456789012345678901234"
data = "hello world! The world is yours"
block_size = 8
cipher = :crypto.block_encrypt(:des_ede3, [key, key, key], iv, pad(data, block_size))

How do i pass my 24 byte api key as the key to erlangs block_encrypt/4 in order for me to pass data to the external api.

Thanks


回答1:


Use binary pattern matching:

<<k1 :: binary-size(8),
  k2 :: binary-size(8),
  k3 :: binary-size(8)>> = "123456789012345678901234"
#⇒ "123456789012345678901234"

{k1, k2, k3}
#⇒ {"12345678", "90123456", "78901234"}


来源:https://stackoverflow.com/questions/57478114/how-to-set-a-24-byte-length-key-to-elixir-erlang-block-encrypt-4-function-using

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!