Encrypt, decrypt using Rails

前端 未结 3 1835
刺人心
刺人心 2021-01-30 10:37

I saw a while ago the possibility to decrypt and encrypt strings in rails without including any library, but I can\'t find the blog post.

I want to be able to encrypt an

3条回答
  •  孤街浪徒
    2021-01-30 11:20

    Rails 5 requires that the key be 32 bytes.

    Edit to Rails 4 answer that works for Rails 5:

     key = SecureRandom.random_bytes(32)
     crypt = ActiveSupport::MessageEncryptor.new(key) 
     encrypted_data = crypt.encrypt_and_sign('my confidental data')
    

    Decrypt:

     decrypted_back = crypt.decrypt_and_verify(encrypted_data)
    

提交回复
热议问题