尝试使用Elixir中的公共密钥编码base64编码密码

发布于 2025-02-06 06:32:36 字数 3055 浏览 4 评论 0原文

我尝试了很多不同的方式,但是这种方式使我走了最远。我在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文