Class Methods
Instance Methods


Crypt::OpenSSL::RSA − RSA encoding and decoding, using the openSSL libraries


  use Crypt::OpenSSL::Random;
  use Crypt::OpenSSL::RSA;
  # not necessary if we have /dev/random:
  $rsa_pub = Crypt::OpenSSL::RSA−>new_public_key($key_string);
  $rsa_pub−>use_sslv23_padding(); # use_pkcs1_oaep_padding is the default
  $ciphertext = $rsa−>encrypt($plaintext);
  $rsa_priv = Crypt::OpenSSL::RSA−>new_private_key($key_string);
  $plaintext = $rsa−>encrypt($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",
  print "public key (in X509 format) is:\n",
  $rsa_priv−>use_md5_hash(); # use_sha1_hash is the default
  $signature = $rsa_priv−>sign($plaintext);
  print "Signed correctly\n" if ($rsa−>verify($plaintext, $signature));


Crypt::OpenSSL::RSA provides the ability to RSA encrypt strings which are somewhat shorter than the block size of a key. It also allows for decryption, signatures and signature verification.

NOTE : Many of the methods in this package can croak, so use eval, or’s try/catch mechanism to capture errors. Also, while some methods from earlier versions of this package return true on success, this (never documented) behavior is no longer the case.

Class Methods


Create a new Crypt::OpenSSL::RSA object by loading a public key in from a string containing Base64/DER−encoding of either the PKCS1 or X.509 representation of the key. The string should include the −−−−−BEGIN...−−−−− and −−−−−END...−−−−− lines.

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


Create a new Crypt::OpenSSL::RSA object by loading a private key in from an string containing the Base64/DER encoding of the PKCS1 representation of the key. The string should include the −−−−−BEGIN...−−−−− and −−−−−END...−−−−− lines. The padding is set to PKCS1_OAEP , but can be changed with use_xxx_padding.


Create a new Crypt::OpenSSL::RSA object by constructing a private/public key pair. The first (mandetory) 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.


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.


Import a random seed from Crypt::OpenSSL::Random, since the OpenSSL libraries won’t allow sharing of random structures across perl XS modules.

Instance Methods


Clean up after ourselves. In particular, erase and free the memory occupied by the RSA key structure.


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

  −−−−−END RSA PUBLIC KEY−−−−−−


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".


Return the DER-encoded PKCS1 representation of the private key.


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


Decrypt a binary "string". Croaks if the key is public only.


Encrypt a binary "string" using the private key. Croaks if the key is public only.


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


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


Check the signature on a text.


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.


Use PKCS #1 v1.5 padding. This currently is the most widely used mode of 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.


Use PKCS #1 v1.5 padding with an SSL-specific modification that denotes that the server is SSL3 capable.


Use the RFC 1321 MD5 hashing algorithm by Ron Rivest when signing and verifying messages.


Use the RFC 3174 Secure Hashing Algorithm ( FIPS 180−1) when signing and verifying messages. This is the default.

use_sha224_hash, use_sha256_hash, use_sha384_hash, use_sha512_hash

These FIPS 180−2 hash algorithms, for use when signing and verifying messages, are only available with newer openssl versions (>= 0.9.8).


Dobbertin, Bosselaers and Preneel’s RIPEMD hashing algorithm when signing and verifying messages.


Returns the size, in bytes, of the key. All encrypted text will be of this size, and depending on the padding mode used, the length of the text to be encrypted should be:

at most 42 bytes less than this size.

pkcs1_padding or sslv23_padding

at most 11 bytes less than this size.


exactly this size.


This function validates the RSA key, returning a true value if the key is valid, and a false value otherwise. Croaks if the key is public only.


Return 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, where 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 return as undef; only n and e will be defined for a public key. The Crypt::OpenSSL::Bignum module must be installed for this to work.


Return true if this is a private key, and false if it is private only.


There is a small memory leak when generating new keys of more than 512 bits.


Ian Robertson, For support, please email perl−openssl−


perl(1), Crypt::OpenSSL::Random(3), Crypt::OpenSSL::Bignum(3), rsa(3), RSA_new(3), RSA_public_encrypt(3), RSA_size(3), RSA_generate_key(3), RSA_check_key(3)


Personal Opportunity - Free software gives you access to billions of dollars of software at no cost. Use this software for your business, personal use or to develop a profitable skill. Access to source code provides access to a level of capabilities/information that companies protect though copyrights. Open source is a core component of the Internet and it is available to you. Leverage the billions of dollars in resources and capabilities to build a career, establish a business or change the world. The potential is endless for those who understand the opportunity.

Business Opportunity - Goldman Sachs, IBM and countless large corporations are leveraging open source to reduce costs, develop products and increase their bottom lines. Learn what these companies know about open source and how open source can give you the advantage.

Free Software

Free Software provides computer programs and capabilities at no cost but more importantly, it provides the freedom to run, edit, contribute to, and share the software. The importance of free software is a matter of access, not price. Software at no cost is a benefit but ownership rights to the software and source code is far more significant.

Free Office Software - The Libre Office suite provides top desktop productivity tools for free. This includes, a word processor, spreadsheet, presentation engine, drawing and flowcharting, database and math applications. Libre Office is available for Linux or Windows.

Free Books

The Free Books Library is a collection of thousands of the most popular public domain books in an online readable format. The collection includes great classical literature and more recent works where the U.S. copyright has expired. These books are yours to read and use without restrictions.

Source Code - Want to change a program or know how it works? Open Source provides the source code for its programs so that anyone can use, modify or learn how to write those programs themselves. Visit the GNU source code repositories to download the source.


Study at Harvard, Stanford or MIT - Open edX provides free online courses from Harvard, MIT, Columbia, UC Berkeley and other top Universities. Hundreds of courses for almost all major subjects and course levels. Open edx also offers some paid courses and selected certifications.

Linux Manual Pages - A man or manual page is a form of software documentation found on Linux/Unix operating systems. Topics covered include computer programs (including library and system calls), formal standards and conventions, and even abstract concepts.