RSA(3)

Crypt::OpenSSL::RSA πŸ’–

okay bestie, so this is the manual page for RSA(3) β€” it lives under User Contributed Perl Documentation, and it's literally the whole guide to doing RSA encryption, decryption, signing and verifying with the OpenSSL libraries through Perl. grab a snack, we're doing this πŸ“


✨ NAME ✨

Crypt::OpenSSL::RSA β€” RSA encoding and decoding, using the openSSL libraries. periodt.


🌸 SYNOPSIS 🌸

here's the vibe, in code:

  use Crypt::OpenSSL::Random;
  use Crypt::OpenSSL::RSA;

  # not necessary if we have /dev/random:
  Crypt::OpenSSL::Random::random_seed($good_entropy);
  Crypt::OpenSSL::RSA->import_random_seed();
  $rsa_pub = Crypt::OpenSSL::RSA->new_public_key($key_string);
  $ciphertext = $rsa->encrypt($plaintext);

  $rsa_priv = Crypt::OpenSSL::RSA->new_private_key($key_string);
  $plaintext = $rsa->decrypt($ciphertext);

  $rsa = Crypt::OpenSSL::RSA->generate_key(1024); # or
  $rsa = Crypt::OpenSSL::RSA->generate_key(1024, $prime);

  print "private key is:\n", $rsa->get_private_key_string();
  print "public key (in PKCS1 format) is:\n",
        $rsa->get_public_key_string();
  print "public key (in X509 format) is:\n",
        $rsa->get_public_key_x509_string();

  $rsa_priv->use_md5_hash(); # insecure. use_sha256_hash or use_sha1_hash are the default
  $signature = $rsa_priv->sign($plaintext);
  print "Signed correctly\n" if ($rsa->verify($plaintext, $signature));

🚨 SECURITY 🚨

okay so listen, this part is serious, no jokes:

Version 0.35 disabled PKCS#1 v1.5 padding entirely, to mitigate the Marvin attack. BUT β€” and this is the plot twist β€” the Marvin attack only affects PKCS#1 v1.5 decryption (padding oracle), not signatures. so Version 0.38 re-enables use_pkcs1_padding() for use with sign() and verify(), while keeping it disabled for encrypt() and decrypt().

bestie please internalize this: use PKCS1_OAEP for encryption, and either PKCS1_PSS or PKCS1 for signing. πŸ’…

πŸ’œ DESCRIPTION πŸ’œ

Crypt::OpenSSL::RSA gives you the ability to RSA encrypt strings that are, like, somewhat shorter than the block size of a key. it also does decryption, signatures and signature verification. iconic behavior honestly.

NOTE: Many of the methods in this package can croak, so use eval, or Error.pm's try/catch mechanism to capture errors. Also β€” and this is important β€” while some methods from earlier versions of this package returned true on success, this (never documented) behavior is no longer the case. it's giving "we don't do that anymore" πŸ’€

πŸ‘‘ Class Methods πŸ‘‘

🌟 new_public_key

Create a new Crypt::OpenSSL::RSA object by loading a public key in from a string containing either PEM or DER encoding of the PKCS#1 or X.509 representation of the key.

For PEM keys, the string should include the -----BEGIN...----- and -----END...----- lines. Both BEGIN RSA PUBLIC KEY (PKCS#1) and BEGIN PUBLIC KEY (X.509/SubjectPublicKeyInfo) formats are supported.

DER-encoded keys (raw binary ASN.1) are also accepted, and the format (PKCS#1 vs X.509) is auto-detected. she's smart like that ✨

The padding is set to PKCS1_OAEP, but can be changed with the use_xxx_padding methods.

Note: PKCS1_OAEP can only be used for encryption. Call use_pkcs1_pss_padding or use_pkcs1_padding prior to signing operations. don't skip this step bestie 😭

🌟 new_private_key

Create a new Crypt::OpenSSL::RSA object by loading a private key in from a string containing either PEM or DER encoding of the key.

For PEM keys, the string should include the -----BEGIN...----- and -----END...----- lines. The padding is set to PKCS1_OAEP, but can be changed with use_xxx_padding.

DER-encoded keys (raw binary ASN.1) are also accepted.

An optional parameter can be passed for passphrase-protected private keys:

  • passphrase β€” The passphrase which protects the private key. For PEM keys, this decrypts traditional encrypted PEM (DEK-Info header) and encrypted PKCS#8 PEM (BEGIN ENCRYPTED PRIVATE KEY). For DER keys, this decrypts encrypted PKCS#8 DER (EncryptedPrivateKeyInfo ASN.1 structure).

🌟 generate_key

Create a new Crypt::OpenSSL::RSA object by constructing a private/public key pair. The first (mandatory) argument is the key size, while the second optional argument specifies the public exponent (the default public exponent is 65537). The padding is set to PKCS1_OAEP, but can be changed with use_xxx_padding methods.

🌟 new_key_from_parameters

Given Crypt::OpenSSL::Bignum objects for n, e, and optionally d, p, and q β€” where p and q are the prime factors of n, e is the public exponent and d is the private exponent β€” create a new Crypt::OpenSSL::RSA object using these values.

If p and q are provided and d is undef, d is computed. Note that while p and q are not necessary for a private key, their presence will speed up computation. glow up via math, we love to see it ⭐

An optional check => 1 parameter can be passed after the key components to validate the key immediately after construction:

  my $rsa = Crypt::OpenSSL::RSA->new_key_from_parameters(
      $n, $e, $d, $p, $q, check => 1
  );

When enabled, check_key() is called on the resulting key. If the key parameters are inconsistent (e.g. wrong CRT values, mismatched n/e/d/p/q), the constructor will croak instead of returning an object that fails at first use. The check is only performed on private keys; public-only keys (n and e only) are returned without validation.

🌟 import_random_seed

Import a random seed from Crypt::OpenSSL::Random, since the OpenSSL libraries won't allow sharing of random structures across perl XS modules. it's giving "we don't share our toys" 🧚


πŸ’« Instance Methods πŸ’«

πŸ¦‹ DESTROY

Clean up after ourselves. In particular, erase and free the memory occupied by the RSA key structure. she's tidy, she's respectful, she closes her tabs 🌸

πŸ¦‹ get_public_key_string

Return the Base64/DER-encoded PKCS1 representation of the public key. This string has header and footer lines:

  -----BEGIN RSA PUBLIC KEY------
  -----END RSA PUBLIC KEY------

πŸ¦‹ get_public_key_pkcs1_string

Alias for get_public_key_string. Returns the same PKCS#1 RSAPublicKey PEM format (BEGIN RSA PUBLIC KEY). Provided for naming symmetry with the import method new_public_key (which auto-detects PKCS#1 vs. X.509) and with get_public_key_x509_string. twin energy πŸ’…

πŸ¦‹ get_public_key_x509_string

Return the Base64/DER-encoded representation of the "subject public key", suitable for use in X509 certificates. This string has header and footer lines:

  -----BEGIN PUBLIC KEY------
  -----END PUBLIC KEY------

and is the format that is produced by running openssl rsa -pubout.

πŸ¦‹ get_private_key_string

Return the Base64/DER-encoded PKCS1 representation of the private key. This string has header and footer lines:

  -----BEGIN RSA PRIVATE KEY------
  -----END RSA PRIVATE KEY------

2 optional parameters can be passed for passphrase protected private key string:

  • passphrase β€” The passphrase which protects the private key.
  • cipher name β€” The cipher algorithm used to protect the private key. Default to 'des3'.

πŸ¦‹ get_private_key_pkcs8_string

Return the Base64/DER-encoded PKCS#8 representation of the private key. This string has header and footer lines:

  -----BEGIN PRIVATE KEY-----
  -----END PRIVATE KEY-----

This is the format produced by openssl pkey -outform PEM, and is the private-key counterpart of get_public_key_x509_string.

Accepts the same optional passphrase and cipher-name parameters as get_private_key_string.

πŸ¦‹ encrypt

Encrypt a binary string using the public (portion of the) key.

πŸ¦‹ decrypt

Decrypt a binary string. Croaks if the key is public only. πŸ™ˆ

πŸ¦‹ private_encrypt

Encrypt a binary string using the private key. Croaks if the key is public only. On OpenSSL 3.x, only use_no_padding and use_pkcs1_padding are supported; OAEP and PSS will croak.

πŸ¦‹ public_decrypt

Decrypt a binary string using the public (portion of the) key. On OpenSSL 3.x, only use_no_padding and use_pkcs1_padding are supported; OAEP and PSS will croak.

πŸ¦‹ sign

Sign a string using the secret (portion of the) key. ✍️

πŸ¦‹ verify

Check the signature on a text. the moment of truth bestie πŸ”₯


πŸŽ€ Padding Methods πŸŽ€

so like, quick rundown of the whole padding situation:

use_pkcs1_padding can be used for signature operations (sign() and verify()). PKCS#1 v1.5 encryption is disabled due to the Marvin attack. use_pkcs1_pss_padding is the recommended replacement for signatures. use_pkcs1_oaep_padding is used for encryption operations.

On OpenSSL 3.x, the appropriate padding is set for each operation unless use_no_padding or use_pkcs1_padding is called before the operation.


  • use_no_padding β€” Use raw RSA encryption. This mode should only be used to implement cryptographically sound padding modes in the application code. Encrypting user data directly with RSA is insecure. do not do this unless you really know what you're doing, bestie, i'm begging 😭
  • use_pkcs1_padding β€” Use PKCS #1 v1.5 padding for signature operations only. PKCS#1 v1.5 signatures (RSASSA-PKCS1-v1.5) are secure and widely required by protocols such as JWT RS256, ACME (RFC 8555), and SAML.
Note: PKCS#1 v1.5 encryption is disabled because it is vulnerable to the Marvin Attack <https://github.com/tomato42/marvin-toolkit/blob/master/README.md> (a timing side-channel on decryption padding validation). Calling encrypt() or decrypt() with this padding will croak. Use use_pkcs1_oaep_padding() for encryption.
  • use_pkcs1_oaep_padding β€” Use EME-OAEP padding as defined in PKCS #1 v2.0 with SHA-1, MGF1 and an empty encoding parameter. This mode of padding is recommended for all new applications. It is the default mode used by Crypt::OpenSSL::RSA but is only valid for encryption/decryption. she's the default for a reason, bestie ✨
  • use_pkcs1_pss_padding β€” Use RSA-PSS padding as defined in PKCS#1 v2.1. In general, RSA-PSS should be used as a replacement for RSA-PKCS#1 v1.5. The module specifies the message digest being requested and the appropriate mgf1 setting and salt length for the digest.
Note: RSA-PSS cannot be used for encryption/decryption and results in a fatal error. Call use_pkcs1_oaep_padding for encryption operations.
  • use_sslv23_padding β€” Use PKCS #1 v1.5 padding with an SSL-specific modification that denotes that the server is SSL3 capable.
Not available on OpenSSL 3.x or later. Calling this method will croak with a descriptive error message suggesting alternatives. Use use_pkcs1_oaep_padding() for encryption or use_pkcs1_pss_padding() for signatures. rip to a legend πŸ’€

🧬 Hash/Digest Methods 🧬

okay bestie, this is the lil crew of methods that tell your RSA object which hashing algorithm to use when it signs and verifies stuff. pick your fighter πŸ’…

  • use_md5_hash β€” signs and verifies messages with the RFC 1321 MD5 hashing algorithm, the one Ron Rivest made. lowkey a problem tho: this one is considered insecure. 🚩
  • use_sha1_hash β€” signs and verifies with the RFC 3174 Secure Hashing Algorithm (FIPS 180-1). this is the default one when use_sha256_hash isn't available. ✨
  • use_sha224_hash, use_sha256_hash, use_sha384_hash, use_sha512_hash β€” the FIPS 180-2 hash algorithms, for signing and verifying messages. these only show up if you've got newer openssl versions, so >= 0.9.8. and use_sha256_hash is the default hash mode when it's available. πŸ‘‘
  • use_ripemd160_hash β€” the RIPEMD hashing algorithm by Dobbertin, Bosselaers and Preneel, for signing and verifying messages. πŸ¦‹
  • use_whirlpool_hash β€” the ISO/IEC 10118-3:2004 WHIRLPOOL hashing algorithm by Vincent Rijmen und Paulo S. L. M. Barreto, for signing and verifying messages. 🌊
  • size β€” returns the size of the key, in bytes. literally every bit of encrypted text is gonna be this size, and depending on which padding mode you're in, the length of the text you're encrypting should be:
    • pkcs1_oaep_padding β€” at most 42 bytes less than this size.
    • pkcs1_padding or sslv23_padding β€” at most 11 bytes less than this size.
    • no_padding β€” exactly this size. no wiggle room, no notes. πŸ“
  • check_key β€” validates the RSA key. returns a true value if the key is valid, a false value if it's not. > heads up bestie: it croaks if the key is public only. πŸ’€
  • get_key_parameters β€” hands you back Crypt::OpenSSL::Bignum objects representing the values of n, e, d, p, q, d mod (p-1), d mod (q-1), and 1/q mod p. quick reminder: p and q are the prime factors of n, e is the public exponent and d is the private exponent. some of these values may come back as undef, and for a public key only n and e will be defined. also the Crypt::OpenSSL::Bignum module has to be installed for this to work at all, fr fr. 🫢
  • is_private β€” returns true if this is a private key, and false if it's public only. simple as that, iconic. ✨

πŸ‘‘ AUTHOR πŸ‘‘

Ian Robertson, iroberts@cpan.org. for support, please email perl-openssl-users@lists.sourceforge.net. (yes bestie, actual email, go say hi if you're stuck πŸ’Œ)


πŸŽ€ ACKNOWLEDGEMENTS πŸŽ€

(the source page just has the heading sitting there on its own, no content β€” so we're leaving it exactly as we found it, no cap. it's giving mysterious.)


πŸ“œ LICENSE πŸ“œ

Copyright (c) 2001-2011 Ian Robertson. Crypt::OpenSSL::RSA is free software; you may redistribute it and/or modify it under the same terms as Perl itself. πŸ’–


🌈 SEE ALSO 🌈


and that's a wrap on RSA(3) bestie!! πŸŽ‰ this whole thing shipped as part of perl v5.42.2, dated 2026-04-24, filed under the name RSA(3). go forth and encrypt responsibly, ily πŸ«Άβœ¨πŸ’œ

πŸ“¦ where this page comes from