使用Rails加密,解密 [英] Encrypt, decrypt using Rails

查看:139
本文介绍了使用Rails加密,解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想要在不久之前看到在rails中解密和加密字符串的可能性,而不包括任何图书馆,但我找不到博客文章。能够加密和解密字符串而不包含任何东西。
使用相同的键对于rails中的所有其他内容,例如签名的Cookie。



任何想法?

解决方案

你的意思是这个?: ActiveSupport :: MessageEncryptor 。这是重用Rails 4应用程序秘密的方法:

  crypt = ActiveSupport :: MessageEncryptor.new(Rails.application.secrets。 secret_key_base)
encrypted_data = crypt.encrypt_and_sign('我的信心数据')

加密数据可以解密:

  decryptpted_back = crypt.decrypt_and_verify(encrypted_data)
pre>

以前的Rails 3正在使用 secret_token 配置选项,加密方法是 encrypt decrypt


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 and decrypt strings without including anything. Using the same key has for the everything else in rails, signed cookies for example.

Any ideas?

解决方案

You mean this one?: ActiveSupport::MessageEncryptor. Here is the way to reuse Rails 4 application's secret:

crypt = ActiveSupport::MessageEncryptor.new(Rails.application.secrets.secret_key_base)
encrypted_data = crypt.encrypt_and_sign('my confidental data')

And encrypted data can be decrypted with:

decrypted_back = crypt.decrypt_and_verify(encrypted_data)

Previously Rails 3 was using secret_token configuration option and encryptor methods were encrypt decrypt.

这篇关于使用Rails加密,解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆