尝试使用Elixir中的公共密钥编码base64编码密码
我尝试了很多不同的方式,但是这种方式使我走了最远。我在PEM文件中有公钥。我通过使用以下:public_key的一些代码在密钥文件中读取,但是尝试对其进行编码只是返回错误。
base64_password = Base.encode64(enc_password)
rsa_public_key_path = "/Users/owen/loomis/io_backend/priv/certs/in2publickey.key"
{:ok, pub_key_string} = File.read(rsa_public_key_path)
IO.puts("got the file " <> pub_key_string)
public_key1 = "/Users/owen/loomis/io_backend/priv/certs/in2publickey.pem" |> File.read!() |> :public_key.pem_decode()
IO.puts("got the key")
public_key2 = Enum.at(public_key1, 0)
IO.puts("got key2")
{:RSAPublicKey, public_key3} = public_key2
{:ok, cipher_text} = ExPublicKey.encrypt_public(base64_password, public_key3)
输出是:
got the file -----BEGIN RSA PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDBgAoZDSn2tQiWlmAajIGmptuz
Olk7nYiIpz6IC14hdE27S+lpxG9UvNmsuogHAUagNnlnWUlNNyqBbx2ntSlihKpb
cu9SseTD5GYFCUn0KxMFqxodEWPNDg5lq5G+Pd1llvXYrMBaN8w3qssI7QvbOqOt
RjuLvzwCsNG2oCgzAwIDAQAB
-----END RSA PUBLIC KEY-----
got the key
got key2
异常返回是:
** (MatchError) no match of right hand side value: {:RSAPublicKey, <<48, 129, 159, 48, 13, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 1, 5, 0, 3, 129, 141, 0, 48, 129, 137, 2, 129, 129, 0, 193, 128, 10, 25, 13, 41, 246, 181, 8, 150, 150, 96, 26, 140, 129, 166, 166, 219, 179, ...>>, :not_encrypted}
(io_backend 0.1.0) lib/io_backend_web/controllers/AuthenticationController.ex:102: IoBackendWeb.AuthenticationController.show/2
(io_backend 0.1.0) lib/io_backend_web/controllers/AuthenticationController.ex:1: IoBackendWeb.AuthenticationController.action/2
(io_backend 0.1.0) lib/io_backend_web/controllers/AuthenticationController.ex:1: IoBackendWeb.AuthenticationController.phoenix_controller_pipeline/2
(phoenix 1.6.6) lib/phoenix/router.ex:355: Phoenix.Router.__call__/2
(io_backend 0.1.0) lib/io_backend_web/endpoint.ex:1: IoBackendWeb.Endpoint.plug_builder_call/2
(io_backend 0.1.0) lib/plug/debugger.ex:136: IoBackendWeb.Endpoint."call (overridable 3)"/2
(io_backend 0.1.0) lib/io_backend_web/endpoint.ex:1: IoBackendWeb.Endpoint.call/2
(phoenix 1.6.6) lib/phoenix/endpoint/cowboy2_handler.ex:54: Phoenix.Endpoint.Cowboy2Handler.init/4
(cowboy 2.9.0) /Users/owen/loomis/io_backend/deps/cowboy/src/cowboy_handler.erl:37: :cowboy_handler.execute/2
(cowboy 2.9.0) /Users/owen/loomis/io_backend/deps/cowboy/src/cowboy_stream_h.erl:306: :cowboy_stream_h.execute/3
(cowboy 2.9.0) /Users/owen/loomis/io_backend/deps/cowboy/src/cowboy_stream_h.erl:295: :cowboy_stream_h.request_process/3
(stdlib 3.17) proc_lib.erl:226: :proc_lib.init_p_do_apply/3
我能够到达“ get key2”点,但是它抱怨下一行,我试图剥离仅密钥而不是:rsapublickey atom 。 (我已经尝试使用public_key2进行编码,并给出了匹配错误。我什至不确定Expublickey是否会rsa。实际读取字符串的调用是对Erlang函数的调用。我需要一个相应的Erlang代码来制作加密有更好的方法?
I've tried a bunch of different ways, but this way has gotten me the farthest. I have public key in PEM file. I've managed to read in the key file by using some code from :public_key, but trying to encode it just returns errors.
base64_password = Base.encode64(enc_password)
rsa_public_key_path = "/Users/owen/loomis/io_backend/priv/certs/in2publickey.key"
{:ok, pub_key_string} = File.read(rsa_public_key_path)
IO.puts("got the file " <> pub_key_string)
public_key1 = "/Users/owen/loomis/io_backend/priv/certs/in2publickey.pem" |> File.read!() |> :public_key.pem_decode()
IO.puts("got the key")
public_key2 = Enum.at(public_key1, 0)
IO.puts("got key2")
{:RSAPublicKey, public_key3} = public_key2
{:ok, cipher_text} = ExPublicKey.encrypt_public(base64_password, public_key3)
And the output is:
got the file -----BEGIN RSA PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDBgAoZDSn2tQiWlmAajIGmptuz
Olk7nYiIpz6IC14hdE27S+lpxG9UvNmsuogHAUagNnlnWUlNNyqBbx2ntSlihKpb
cu9SseTD5GYFCUn0KxMFqxodEWPNDg5lq5G+Pd1llvXYrMBaN8w3qssI7QvbOqOt
RjuLvzwCsNG2oCgzAwIDAQAB
-----END RSA PUBLIC KEY-----
got the key
got key2
The Exception return is:
** (MatchError) no match of right hand side value: {:RSAPublicKey, <<48, 129, 159, 48, 13, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 1, 5, 0, 3, 129, 141, 0, 48, 129, 137, 2, 129, 129, 0, 193, 128, 10, 25, 13, 41, 246, 181, 8, 150, 150, 96, 26, 140, 129, 166, 166, 219, 179, ...>>, :not_encrypted}
(io_backend 0.1.0) lib/io_backend_web/controllers/AuthenticationController.ex:102: IoBackendWeb.AuthenticationController.show/2
(io_backend 0.1.0) lib/io_backend_web/controllers/AuthenticationController.ex:1: IoBackendWeb.AuthenticationController.action/2
(io_backend 0.1.0) lib/io_backend_web/controllers/AuthenticationController.ex:1: IoBackendWeb.AuthenticationController.phoenix_controller_pipeline/2
(phoenix 1.6.6) lib/phoenix/router.ex:355: Phoenix.Router.__call__/2
(io_backend 0.1.0) lib/io_backend_web/endpoint.ex:1: IoBackendWeb.Endpoint.plug_builder_call/2
(io_backend 0.1.0) lib/plug/debugger.ex:136: IoBackendWeb.Endpoint."call (overridable 3)"/2
(io_backend 0.1.0) lib/io_backend_web/endpoint.ex:1: IoBackendWeb.Endpoint.call/2
(phoenix 1.6.6) lib/phoenix/endpoint/cowboy2_handler.ex:54: Phoenix.Endpoint.Cowboy2Handler.init/4
(cowboy 2.9.0) /Users/owen/loomis/io_backend/deps/cowboy/src/cowboy_handler.erl:37: :cowboy_handler.execute/2
(cowboy 2.9.0) /Users/owen/loomis/io_backend/deps/cowboy/src/cowboy_stream_h.erl:306: :cowboy_stream_h.execute/3
(cowboy 2.9.0) /Users/owen/loomis/io_backend/deps/cowboy/src/cowboy_stream_h.erl:295: :cowboy_stream_h.request_process/3
(stdlib 3.17) proc_lib.erl:226: :proc_lib.init_p_do_apply/3
I'm able to get to the "got key2" point, but it complains about the next line, where I'm trying to strip out the just the key and not the :RSAPublicKey atom. (I've tried encoding with public_key2 and it gives a match error. I'm not even sure if ExPublicKey will RSA. The call to actually read the string is a call to an Erlang function. Do I need a corresponding Erlang code to make the encryption? Got a better way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论