Skip to content
class method

OpenSSL::PKey::RSA.new

() -> ::OpenSSL::PKey::RSA
(::Integer key_size) -> ::OpenSSL::PKey::RSA
(::String encoded_key, ?::String pass_phrase) -> ::OpenSSL::PKey::RSA
RSA.new → rsa
RSA.new(encoded_key [, password ]) → rsa
RSA.new(encoded_key) { password } → rsa
RSA.new(size [, exponent]) → rsa

Generates or loads an RSA keypair.

If called without arguments, creates a new instance with no key components set. They can be set individually by set_key, set_factors, and set_crt_params. This form is not compatible with OpenSSL 3.0 or later.

If called with a String, tries to parse as DER or PEM encoding of an RSA key. Note that if password is not specified, but the key is encrypted with a password, OpenSSL will prompt for it. See also OpenSSL::PKey.read which can parse keys of any kind.

If called with a number, generates a new key pair. This form works as an alias of RSA.generate.

Examples:

Example
OpenSSL::PKey::RSA.new 2048
OpenSSL::PKey::RSA.new File.read 'rsa.pem'
OpenSSL::PKey::RSA.new File.read('rsa.pem'), 'my password'
See also .generate #export #params also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0