如何使用Plug.Crypto.decrypt?

huangapple go评论58阅读模式
英文:

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:

  1. The secret key base, usually taken from conn.secret_key_base (which if you are using Phoenix, is set based on your config)
  2. Your secret, which acts as salt to encrypt the data
  3. The data you want to encrypt - can be any term - no need to be &quot;raw_string&quot;
  4. Opts specified in the documentation

Example, from inside a plug:

encrypted =
  Plug.Crypto.encrypt(conn.secret_key_base, &quot;mmm salty&quot;, %{hello: &quot;world!&quot;})
  |&gt; IO.inspect(label: &quot;encrypted&quot;)

decrypted =
  Plug.Crypto.decrypt(conn.secret_key_base, &quot;mmm salty&quot;, encrypted)
  |&gt; IO.inspect(label: &quot;decrypted&quot;)

Output:

encrypted: &quot;QTEyOEdDTQ.R83_rKqB0vyw2KJmGZmjhEuRqc-En71p--VGDaRhpzOEXHIxeg_TOE4YCvQ.bsuQLhrrKzE0LOFo.qooCxTQrJQ7S8lTRhngprcEgAODvw_IsIgGofAEC1_F3NyIcaocQVns.KqKtO6hVMPk0dxOXjX-67A&quot;
decrypted: {:ok, %{hello: &quot;world!&quot;}}

Or since you are using Phoenix, you can use Phoenix.Token.encrypt/4 with a conn:

Phoenix.Token.encrypt(conn, &quot;mmm salty&quot;, %{hello: &quot;world!&quot;})

Or an endpoint:

Phoenix.Token.encrypt(YourAppWeb.Endpoint, &quot;mmm salty&quot;, %{hello: &quot;world!&quot;})

huangapple
  • 本文由 发表于 2023年2月18日 21:30:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75493675.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定