ntrepid8 / ex_crypto

Wrapper around the Erlang crypto module for Elixir.
MIT License
144 stars 48 forks source link

Cannot encrypt a long text using RSA private key #44

Closed kuroda closed 2 years ago

kuroda commented 2 years ago

I found a possible bug on ExPublicKey.encrypt_private/3.

iex> System.otp_release()
"24"
iex> short_text = "Hello"
"Hello"
iex> long_text = String.duplicate("a", 10000)
"aaa..."
iex> rsa_priv_key = ExPublicKey.load!("/path/to/private_key.pem")
#ExPublicKey.RSAPrivateKey<...>
iex> ExPublicKey.encrypt_private(short_text, rsa_priv_key)
{:ok,
 "hlnaQvo5Onskl1dlI95RSoBAZlUDVMfHXmM5J3nuB2D7er02AivkOz2l9POaH8KgN4KbVFzbnla4-i8YUmWrOQ=="}
iex> ExPublicKey.encrypt_private(long_text, rsa_priv_key)
{:error, %ErlangError{original: :encrypt_failed}, []}

In fact, an error occurs even if the text is not so long. For example, replacing 10000 with 100 causes the same problem.

However, if I replace 10000 with 50, the error does not occur.

kuroda commented 2 years ago

FYI, I use OTP 24.0.6:

$ erl -eval '{ok, Version} = file:read_file(filename:join([code:root_dir(), "releases", erlang:system_info(otp_release), "OTP_VERSION"])), io:fwrite(Version), halt().' -noshell
24.0.6
kuroda commented 2 years ago

According to an answer on the Information security:

the maximum size of data which can be encrypted with RSA is 245 bytes.

This is not a bug, although the error message could be improved.