# Encryption in Crystal lang

**URL:** <https://forum.crystal-lang.org/t/encryption-in-crystal-lang/763>\
**Category:** Offtopic\
**Created:** [May 11, 2019, 11:17pm UTC](https://forum.crystal-lang.org/t/encryption-in-crystal-lang/763 "2019-05-11T23:17:10Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![curious2learn](https://avatars.discourse-cdn.com/v4/letter/c/5daacb/32.png) [@curious2learn](https://forum.crystal-lang.org/u/curious2learn)\
**Post date:** [May 11, 2019, 11:17pm UTC](https://forum.crystal-lang.org/t/encryption-in-crystal-lang/763/1 "2019-05-11T23:17:10Z")

</div>

How do I do encryption in Crystal? I want to encrypt say a hash type or a named tuple containing some information, and then add it as a cookie value. I need to be able to read this encrypted value back and decrypt it to original hash type. Is this possible?

Thank you so much for all the help.

---

<div class="post-metadata">

**Author:** ![sol.vin](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/sol.vin/32/73_2.png) [@sol.vin](https://forum.crystal-lang.org/u/sol.vin)\
**Post date:** [May 12, 2019, 6:47pm UTC](https://forum.crystal-lang.org/t/encryption-in-crystal-lang/763/2 "2019-05-12T18:47:09Z")

</div>

Check out `OpenSSL::Cipher` [https://crystal-lang.org/api/0.35.1/OpenSSL/Cipher.html](https://crystal-lang.org/api/0.35.1/OpenSSL/Cipher.html)

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [May 12, 2019, 6:51pm UTC](https://forum.crystal-lang.org/t/encryption-in-crystal-lang/763/3 "2019-05-12T18:51:44Z")

</div>

@sol.vin Just a heads up that link is for a quite old version of the Crystal docs.

---

<div class="post-metadata">

**Author:** ![sol.vin](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/sol.vin/32/73_2.png) [@sol.vin](https://forum.crystal-lang.org/u/sol.vin)\
**Post date:** [May 12, 2019, 8:41pm UTC](https://forum.crystal-lang.org/t/encryption-in-crystal-lang/763/4 "2019-05-12T20:41:42Z")

</div>

oops my bad, I just googled OpenSSL Cipher Crystal API and thats what came up.

---

<div class="post-metadata">

**Author:** ![sol.vin](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/sol.vin/32/73_2.png) [@sol.vin](https://forum.crystal-lang.org/u/sol.vin)\
**Post date:** [May 12, 2019, 8:46pm UTC](https://forum.crystal-lang.org/t/encryption-in-crystal-lang/763/5 "2019-05-12T20:46:57Z")

</div>

@Blacksmoke16 Wait I’m missing something here, I went through the API docs on latest and couldn’t find Cipher any more, but I know it’s still there because I recently used it in a program I was writing. It’s still there in the src.

> <https://github.com/crystal-lang/crystal/blob/master/src/openssl/cipher.cr>

---

<div class="post-metadata">

**Author:** ![konovod](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/konovod/32/229_2.png) [@konovod](https://forum.crystal-lang.org/u/konovod)\
**Post date:** [May 12, 2019, 8:51pm UTC](https://forum.crystal-lang.org/t/encryption-in-crystal-lang/763/6 "2019-05-12T20:51:27Z")

</div>

Your question consists of two problems -

1. serialize your hash to some string or binary stream, (and deserialize back)
2. encrypt it (and decrypt back)

If I would need to do it, I’ll do something like this:

```crystal
require "yaml"
require "monocypher"

def encrypt(object, secret)
  plaintext = object.to_yaml.to_slice
  ciphertext = Bytes.new(plaintext.size + Crypto::OVERHEAD_SYMMETRIC)
  Crypto.encrypt(key: secret, input: plaintext, output: ciphertext)
  cookiestring = ciphertext.hexstring
end

def decrypt(text, secret)
  ciphertext = text.hexbytes
  raise "ciphertext too short" if ciphertext.size < Crypto::OVERHEAD_SYMMETRIC
  result = Bytes.new(ciphertext.size - Crypto::OVERHEAD_SYMMETRIC)
  unless Crypto.decrypt(key: secret, input: ciphertext, output: result)
    raise "ciphertext is corrupt"
  end
  hash1 = YAML.parse(String.new(result))
end

secret = Crypto::SymmetricKey.new # this will generate a key from system random source
puts "secret key is #{secret}"

hash = {"1" => [1, 2, 3], 2 => "test"}
cookiestring = encrypt(hash, secret)
puts "encrypted message: #{cookiestring}"
puts "decrypted message: #{decrypt(cookiestring, secret)}"

```

using my [monocypher bindings](https://github.com/konovod/monocypher.cr) shard and untyped serialization to YAML (it is limited to simple types, but maybe it’s ok for a cookie).

Maybe you should better use OpenSSL or [LibSodium bindings](https://github.com/andrewhamon/cox) for encryption, and typed serialization (using e.g. `YAML::Serializable`) for serialization.

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [May 12, 2019, 8:55pm UTC](https://forum.crystal-lang.org/t/encryption-in-crystal-lang/763/7 "2019-05-12T20:55:06Z")

</div>

@sol.vin Looks like that file isnt’t required in the `docs_main` file nor the `openssl.cr` file, thus is missing from the docs. Could prob make an issue for it as it’s prob not intentional it’s not there now.

---

<div class="post-metadata">

**Author:** ![sol.vin](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/sol.vin/32/73_2.png) [@sol.vin](https://forum.crystal-lang.org/u/sol.vin)\
**Post date:** [May 13, 2019, 5:04am UTC](https://forum.crystal-lang.org/t/encryption-in-crystal-lang/763/8 "2019-05-13T05:04:21Z")

</div>

Posted - [https://github.com/crystal-lang/crystal/issues/7770](https://github.com/crystal-lang/crystal/issues/7770)

---

<div class="post-metadata">

**Author:** ![curious2learn](https://avatars.discourse-cdn.com/v4/letter/c/5daacb/32.png) [@curious2learn](https://forum.crystal-lang.org/u/curious2learn)\
**Post date:** [May 14, 2019, 2:07am UTC](https://forum.crystal-lang.org/t/encryption-in-crystal-lang/763/9 "2019-05-14T02:07:50Z")

</div>

Thanks everyone. I was aware of `OpenSSL::Cipher`, however, as has been pointed out that wasn’t available in the new documentation.

I did not know about `monocypher`. Thank you @konovod. Will look into that. I appreciate the example you gave. That would be very helpful.

Thanks.

---

<div class="post-metadata">

**Author:** ![nsuchy](https://avatars.discourse-cdn.com/v4/letter/n/c0e974/32.png) [@nsuchy](https://forum.crystal-lang.org/u/nsuchy)\
**Post date:** [May 24, 2019, 4:59pm UTC](https://forum.crystal-lang.org/t/encryption-in-crystal-lang/763/10 "2019-05-24T16:59:55Z")

</div>

> [@curious2learn](#):
>
> I want to encrypt say a hash type or a named tuple containing some information, and then add it as a cookie value. I need to be able to read this encrypted value back and decrypt it to original hash type.

What type of information is stored in the cookie? It may be a good idea to store a session reference and then have the actual user data only on the server to minimize the amount of places its temporarily stored.

---

<div class="post-metadata">

**Author:** ![curious2learn](https://avatars.discourse-cdn.com/v4/letter/c/5daacb/32.png) [@curious2learn](https://forum.crystal-lang.org/u/curious2learn)\
**Post date:** [May 25, 2019, 9:37pm UTC](https://forum.crystal-lang.org/t/encryption-in-crystal-lang/763/11 "2019-05-25T21:37:48Z")

</div>

I was thinking of storing email and a session id (nothing sensitive, such as a password). But thought of encrypting it. But probably that is not necessary. As you said, I am just using the session id now. Probably, there is no need to encrypt that.

Thanks.

---

<div class="post-metadata">

**Author:** ![nsuchy](https://avatars.discourse-cdn.com/v4/letter/n/c0e974/32.png) [@nsuchy](https://forum.crystal-lang.org/u/nsuchy)\
**Post date:** [May 26, 2019, 7:41pm UTC](https://forum.crystal-lang.org/t/encryption-in-crystal-lang/763/12 "2019-05-26T19:41:48Z")

</div>

HTTPS to and from your web server should be enough. Just make sure session IDs are unpredictable.
