# Simple Encrypt + Decrypt?

**URL:** https://forum.crystal-lang.org/t/simple-encrypt-decrypt/2354
**Category:** Help & Support
**Created:** [July 21, 2020, 3:18am UTC](https://forum.crystal-lang.org/t/simple-encrypt-decrypt/2354 "2020-07-21T03:18:05Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![carcinocron](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/carcinocron/32/1022_2.png) [@carcinocron](https://forum.crystal-lang.org/u/carcinocron)
#### Post date: [July 21, 2020, 3:18am UTC](https://forum.crystal-lang.org/t/simple-encrypt-decrypt/2354/1 "2020-07-21T03:18:05Z")

</div>

I come from Laravel background where the following interface exists ([docs if you really want to see more](https://laravel.com/docs/7.x/encryption)):

```crystal
# example value make by `php artisan key:generate`
# ENV["APL_KEY"] = "base64:dTOymAUpqvF/2tOxnQdakYHuL66HlmkElovGwumCiPE="

struct SimpleEncrypt
  def initialize(
    @key : String = ENV["APP_KEY"], 
    @cypher = AES128)
  end

  def encrypt (data : String) : String
    # encrypts the string
  end

  def decrypt (data : String) : String
    # decrypts the string
  end
end

e = SimpleEncrypt.new()
value = "any string"
encrypted_value = e.encrypt(value)
value2 = e.decrypt(encrypted_value)
puts value2 == value
# true

```

I couldn’t find anything in the standard library or [http://crystalshards.xyz/](http://crystalshards.xyz/) for this, but I did find things suited for adjacent topics like password hashing, SSL, certificates, MD5/SHA.

I think this would make a great STD or shard, but I don’t know the primitives for doing this myself.

---

<div class="post-metadata">

### Author: ![oprypin](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/oprypin/32/859_2.png) [@oprypin](https://forum.crystal-lang.org/u/oprypin)
#### Post date: [July 21, 2020, 8:14am UTC](https://forum.crystal-lang.org/t/simple-encrypt-decrypt/2354/2 "2020-07-21T08:14:36Z")

</div>

I found [https://github.com/dscottboggs/crypher](https://github.com/dscottboggs/crypher), maybe its code would be useful

---

<div class="post-metadata">

### Author: ![naqvis](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/naqvis/32/906_2.png) [@naqvis](https://forum.crystal-lang.org/u/naqvis)
#### Post date: [July 21, 2020, 8:35am UTC](https://forum.crystal-lang.org/t/simple-encrypt-decrypt/2354/3 "2020-07-21T08:35:31Z")

</div>

[https://github.com/didactic-drunk/sodium.cr](https://github.com/didactic-drunk/sodium.cr) seems good candidate for your use-case. Especially the example [https://github.com/didactic-drunk/sodium.cr#secret-key-encryption](https://github.com/didactic-drunk/sodium.cr#secret-key-encryption)

---

<div class="post-metadata">

### Author: ![Geo-7](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/geo-7/32/816_2.png) [@Geo-7](https://forum.crystal-lang.org/u/Geo-7)
#### Post date: [July 21, 2020, 9:49am UTC](https://forum.crystal-lang.org/t/simple-encrypt-decrypt/2354/4 "2020-07-21T09:49:39Z")

</div>

You can check my simple cli app:  
[https://github.com/Geo-7/openssl\_sample](https://github.com/Geo-7/openssl_sample)

---

<div class="post-metadata">

### Author: ![nico](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/nico/32/692_2.png) [@nico](https://forum.crystal-lang.org/u/nico)
#### Post date: [July 29, 2020, 8:32am UTC](https://forum.crystal-lang.org/t/simple-encrypt-decrypt/2354/5 "2020-07-29T08:32:32Z")

</div>

Hello,

```crystal
# Encrypt *data*
  def cipher_encrypt(data) : IO::Memory
    cipher = OpenSSL::Cipher.new("aes-256-cbc")
    cipher.encrypt
    cipher.key = App.cfg.cipher_key
    cipher.iv = App.cfg.cipher_iv
    cipher.update data
    encrypted = cipher.final

    IO::Memory.new(encrypted)
  end

  # Decrypt *data*
  def cipher_decrypt(data) : IO::Memory
    cipher = OpenSSL::Cipher.new("aes-256-cbc")
    cipher.decrypt
    cipher.key = App.cfg.cipher_key
    cipher.iv = App.cfg.cipher_iv
    cipher.update data
    decrypted = cipher.final

    IO::Memory.new(decrypted)
  end

```

`App.cfg.cipher_*` should be replaced (or assigned).

These methods return `IO::Memory`. Use `to_s` to get value in `String` (`cipher_encrypt("hello").to_s`)

---

<div class="post-metadata">

### Author: ![pynixwang](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/pynixwang/32/310_2.png) [@pynixwang](https://forum.crystal-lang.org/u/pynixwang)
#### Post date: [July 30, 2020, 2:49am UTC](https://forum.crystal-lang.org/t/simple-encrypt-decrypt/2354/6 "2020-07-30T02:49:58Z")

</div>

AES encrypt

---

<div class="post-metadata">

### Author: ![nico](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/nico/32/692_2.png) [@nico](https://forum.crystal-lang.org/u/nico)
#### Post date: [November 9, 2020, 7:51am UTC](https://forum.crystal-lang.org/t/simple-encrypt-decrypt/2354/7 "2020-11-09T07:51:14Z")

</div>

Forget my example above. I made a more practical and reliable shard : [https://github.com/Nicolab/crystal-crypt](https://github.com/Nicolab/crystal-crypt).

```crystal
require "crypt"
require "crypt/crypter"
require "crypt/random"

data = "super secret data"
secret = Crypt.random_string(32)
crypter = Crypt::Crypter.new(secret)

# Data encrypted
encrypted = crypter.encrypt(data)

# Data decrypted
decrypted_bytes = crypter.decrypt(encrypted)

# Decrypted data (Bytes)
puts decrypted_bytes

# Convert Bytes to String
puts String.new(decrypted_bytes)

```

Generate a password:

```crystal
require "crypt"
require "crypt/bcrypt"

password = Crypt.create_bcrypt_password("super secret")
# => $2a$10$rI4xRiuAN2fyiKwynO6PPuorfuoM4L2PVv6hlnVJEmNLjqcibAfHq

password.verify("wrong secret") # => false
password.verify("super secret") # => true

```

---

<div class="post-metadata">

### Author: ![j8r](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/j8r/32/111_2.png) [@j8r](https://forum.crystal-lang.org/u/j8r)
#### Post date: [November 9, 2020, 4:13pm UTC](https://forum.crystal-lang.org/t/simple-encrypt-decrypt/2354/8 "2020-11-09T16:13:28Z")

</div>

A shard is not necessary for Bcrypt, it is easy enough with the stdlib.  
For OpenSSL, I can agree not so much. I hope the API will be revisited and more type safe after the 1.0.
