【问题标题】:How to use erlang public_key:decrypt_private?如何使用 erlang public_key:decrypt_private?
【发布时间】:2017-03-17 14:28:15
【问题描述】:

我尝试使用公共 RSA 密钥加密文本并使用私有 RSA 密钥解密此文本。当我加密用户公钥时,它可以工作。

编码公钥

encrypt_public_key(PlainText, PublicKey) ->
[ RSAEntry2 ] = public_key:pem_decode(PublicKey),
PubKey = public_key:pem_entry_decode( RSAEntry2 ),
public_key:encrypt_public(PlainText, PubKey).

但是当我尝试使用私钥解密此文本时,它不起作用,我不知道为什么。

解码私钥

decrypt_private_key(CryptText,PrivateKey) ->
[ RSAEntry2 ] = public_key:pem_decode(PrivateKey),
PrivKey = public_key:pem_entry_decode( RSAEntry2 ),
Result =  public_key:decrypt_private(CryptText, PrivKey, rsa_pkcs1_padding ),
Result.

我如何编码和尝试解码

PublicKey = ems_util:open_file(?SSL_PATH ++  "/" ++ binary_to_list(<<"public_key.pem">>)),

        CryptoText = ems_util:encrypt_public_key(ResponseData2,PublicKey),
        PrivateKey = ems_util:open_file(?SSL_PATH ++  "/" ++ binary_to_list(<<"private_key.pem">>)),
      %erro in this line
       TextPlain = ems_util:decrypt_private_key(TextCrypt,PrivateKey).

错误

=ERROR REPORT==== 17-Mar-2017::10:59:29 ===
Ranch listener ems_http_server1, connection process <0.2159.0>, stream 1 had its request process <0.2160.0> exit with reason function_clause and stacktrace [{public_key,decrypt_private,[<<55,66,78,123,456,2456>>,<<55,173,2367,140,71>>,asn1_NOVALUE}],[{file,"public_key.erl"},{line,313}]},{ems_util,decrypt_private_key,2,[{file,"src/util/ems_util.erl"},{line,614}]},{ems_auth_user,do_barer_authorization,1,[{file,"src/auth/ems_auth_user.erl"},{line,51}]},{ems_dispatcher,lookup_request,1,[{file,"src/distpatcher/ems_dispatcher.erl"},{line,70}]},{ems_http_handler,init,2,[{file,"src/http/ems_http_handler.erl"},{line,22}]},{cowboy_handler,execute,2,[{file,"src/cowboy_handler.erl"},{line,39}]},{cowboy_stream_h,execute,3,[{file,"src/cowboy_stream_h.erl"},{line,172}]},{cowboy_stream_h,proc_lib_hack,3,[{file,"src/cowboy_stream_h.erl"},{line,157}]}]

我不知道发生了什么。

【问题讨论】:

  • 尝试将public_key:decrypt_private(CryptText, PrivKey, rsa_pkcs1_padding) 更改为public_key:decrypt_private(CryptText, PrivKey, [{rsa_pad, rsa_pkcs1_padding}])

标签: erlang rsa public-key-encryption private-key public-key


【解决方案1】:

我描述了我的问题的解决方案。问题是公钥和私钥生成错误。使用 openssl 生成对密钥 rsa 的正确方法是:

 openssl genrsa -out private.pem 2048
 openssl rsa -in private.pem -out public.pem -outform PEM -pubout

这些命令生成正确的密钥对,代码现在可以正常工作。

【讨论】:

    猜你喜欢
    • 2018-06-17
    • 2018-01-16
    • 1970-01-01
    • 2015-11-09
    • 2014-10-20
    • 2013-06-03
    • 2021-12-03
    • 2018-09-21
    • 2011-05-05
    相关资源
    最近更新 更多