Skip to content

RSA Public-Key: (49 bit) - `public_encrypt': data too large for key size #420

@TW-D

Description

@TW-D

Hello to you, I don't think I understand the reason for the "data too large for key size" error when encrypting a message like 'abcd' (4 bytes) with a 49 bits RSA public key when the key length (n) is ~7 bytes.

require('openssl')

PUBLIC_MODULUS_N = OpenSSL::BN.new(379381098436349)
PUBLIC_EXPONENT_E = OpenSSL::BN.new(11)

PRIME_P = OpenSSL::BN.new(18464531)
PRIME_Q = OpenSSL::BN.new(20546479)

PRIVATE_EXPONENT_D = OpenSSL::BN.new(68978374440971)

rsa = OpenSSL::PKey::RSA.new()
rsa.set_key(PUBLIC_MODULUS_N, PUBLIC_EXPONENT_E, PRIVATE_EXPONENT_D)
rsa.set_factors(PRIME_P, PRIME_Q)

PUBLIC_KEY = rsa.public_key()
if (PUBLIC_KEY.public?())
    pp(PUBLIC_KEY.n().num_bytes())
    pp(PUBLIC_KEY.to_text())
    pp(PUBLIC_KEY.to_pem())
    # pp(OpenSSL::PKey::RSA.constants())
    # [:PKCS1_OAEP_PADDING, :NO_PADDING, :PKCS1_PADDING, :SSLV23_PADDING]
    pp(PUBLIC_KEY.public_encrypt('abcd')) # => ERROR: data too large for key size
end

pp('')

if (rsa.private?())
    pp(rsa.n().num_bytes())
    pp(rsa.to_text())
    pp(rsa.to_pem())
end

Thank you for your help.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions