英文:
How to use Plug.Crypto.decrypt?
问题
要加密一个字符串然后解密它,你可以使用以下的Elixir代码:
加密:
enc_str = Plug.Crypto.encrypt("my_secret", "raw_string", {})
解密(尝试以下两种方式):
Plug.Crypto.decrypt("my_secret", enc_str)
Plug.Crypto.decrypt("my_secret", enc_str, {})
如果你遇到了以下错误:
** (FunctionClauseError) no function clause matching in Plug.Crypto.decrypt/2
或者
** (FunctionClauseError) no function clause matching in Plug.Crypto.decrypt/5
那么可能是由于你传递的参数不正确或不匹配所致。你可以参考以下链接来了解这些方法的详细信息以获得更多帮助:
英文:
I want to encrypt a string and the decrypt it, for encryption i did
enc_str = Plug.Crypto.encrypt("my_secret", "raw_string", {})
And for decrypt y tried this two
Plug.Crypto.decrypt("my_secret", enc_str)
Plug.Crypto.decrypt("my_secret", enc_str, {})
which returns the following errors
** (FunctionClauseError) no function clause matching in Plug.Crypto.decrypt/2
** (FunctionClauseError) no function clause matching in Plug.Crypto.decrypt/5
How can enc_str be decrypted properly?
I Only found 2 references to this methods, but i really dont understand them
https://hexdocs.pm/plug_crypto/Plug.Crypto.html#decrypt/4
https://shyr.io/blog/elixir-encrypt-data-plug-crypto
答案1
得分: 3
<!-- 语言全文: lang-elixir -->
[`encrypt/4`](https://hexdocs.pm/plug_crypto/Plug.Crypto.html#encrypt/4) 的参数如下:
1. 密钥基础,通常从 `conn.secret_key_base` 中获取(如果你使用 Phoenix,则根据配置设置)
2. 你的密钥,用作加密数据的盐
3. 要加密的数据 - 可以是任何术语 - 不需要是 `"raw_string"`
4. 在文档中指定的选项
例如,在一个插件内部:
encrypted =
Plug.Crypto.encrypt(conn.secret_key_base, "mmm salty", %{hello: "world!"})
|> IO.inspect(label: "encrypted")
decrypted =
Plug.Crypto.decrypt(conn.secret_key_base, "mmm salty", encrypted)
|> IO.inspect(label: "decrypted")
输出:
encrypted: "QTEyOEdDTQ.R83_rKqB0vyw2KJmGZmjhEuRqc-En71p--VGDaRhpzOEXHIxeg_TOE4YCvQ.bsuQLhrrKzE0LOFo.qooCxTQrJQ7S8lTRhngprcEgAODvw_IsIgGofAEC1_F3NyIcaocQVns.KqKtO6hVMPk0dxOXjX-67A"
decrypted: {:ok, %{hello: "world!"}}
---
或者,由于你使用的是 Phoenix,你可以在连接(conn)中使用 [`Phoenix.Token.encrypt/4`](https://hexdocs.pm/phoenix/Phoenix.Token.html#encrypt/4):
Phoenix.Token.encrypt(conn, "mmm salty", %{hello: "world!"})
或者在端点(endpoint)中使用:
Phoenix.Token.encrypt(YourAppWeb.Endpoint, "mmm salty", %{hello: "world!"})
英文:
<!-- language-all: lang-elixir -->
The arguments to encrypt/4
are:
- The secret key base, usually taken from
conn.secret_key_base
(which if you are using Phoenix, is set based on your config) - Your secret, which acts as salt to encrypt the data
- The data you want to encrypt - can be any term - no need to be
"raw_string"
- Opts specified in the documentation
Example, from inside a plug:
encrypted =
Plug.Crypto.encrypt(conn.secret_key_base, "mmm salty", %{hello: "world!"})
|> IO.inspect(label: "encrypted")
decrypted =
Plug.Crypto.decrypt(conn.secret_key_base, "mmm salty", encrypted)
|> IO.inspect(label: "decrypted")
Output:
encrypted: "QTEyOEdDTQ.R83_rKqB0vyw2KJmGZmjhEuRqc-En71p--VGDaRhpzOEXHIxeg_TOE4YCvQ.bsuQLhrrKzE0LOFo.qooCxTQrJQ7S8lTRhngprcEgAODvw_IsIgGofAEC1_F3NyIcaocQVns.KqKtO6hVMPk0dxOXjX-67A"
decrypted: {:ok, %{hello: "world!"}}
Or since you are using Phoenix, you can use Phoenix.Token.encrypt/4
with a conn:
Phoenix.Token.encrypt(conn, "mmm salty", %{hello: "world!"})
Or an endpoint:
Phoenix.Token.encrypt(YourAppWeb.Endpoint, "mmm salty", %{hello: "world!"})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论