{
    "mode": "perldoc",
    "parameter": "Crypt::OpenSSL::RSA",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Crypt%3A%3AOpenSSL%3A%3ARSA/json",
    "generated": "2026-06-12T19:03:40Z",
    "synopsis": "use Crypt::OpenSSL::Random;\nuse Crypt::OpenSSL::RSA;\n# not necessary if we have /dev/random:\nCrypt::OpenSSL::Random::randomseed($goodentropy);\nCrypt::OpenSSL::RSA->importrandomseed();\n$rsapub = Crypt::OpenSSL::RSA->newpublickey($keystring);\n$rsapub->usesslv23padding(); # usepkcs1oaeppadding is the default\n$ciphertext = $rsa->encrypt($plaintext);\n$rsapriv = Crypt::OpenSSL::RSA->newprivatekey($keystring);\n$plaintext = $rsa->encrypt($ciphertext);\n$rsa = Crypt::OpenSSL::RSA->generatekey(1024); # or\n$rsa = Crypt::OpenSSL::RSA->generatekey(1024, $prime);\nprint \"private key is:\\n\", $rsa->getprivatekeystring();\nprint \"public key (in PKCS1 format) is:\\n\",\n$rsa->getpublickeystring();\nprint \"public key (in X509 format) is:\\n\",\n$rsa->getpublickeyx509string();\n$rsapriv->usemd5hash(); # insecure. usesha256hash or usesha1hash are the default\n$signature = $rsapriv->sign($plaintext);\nprint \"Signed correctly\\n\" if ($rsa->verify($plaintext, $signature));",
    "sections": {
        "NAME": {
            "content": "Crypt::OpenSSL::RSA - RSA encoding and decoding, using the openSSL libraries\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use Crypt::OpenSSL::Random;\nuse Crypt::OpenSSL::RSA;\n\n# not necessary if we have /dev/random:\nCrypt::OpenSSL::Random::randomseed($goodentropy);\nCrypt::OpenSSL::RSA->importrandomseed();\n$rsapub = Crypt::OpenSSL::RSA->newpublickey($keystring);\n$rsapub->usesslv23padding(); # usepkcs1oaeppadding is the default\n$ciphertext = $rsa->encrypt($plaintext);\n\n$rsapriv = Crypt::OpenSSL::RSA->newprivatekey($keystring);\n$plaintext = $rsa->encrypt($ciphertext);\n\n$rsa = Crypt::OpenSSL::RSA->generatekey(1024); # or\n$rsa = Crypt::OpenSSL::RSA->generatekey(1024, $prime);\n\nprint \"private key is:\\n\", $rsa->getprivatekeystring();\nprint \"public key (in PKCS1 format) is:\\n\",\n$rsa->getpublickeystring();\nprint \"public key (in X509 format) is:\\n\",\n$rsa->getpublickeyx509string();\n\n$rsapriv->usemd5hash(); # insecure. usesha256hash or usesha1hash are the default\n$signature = $rsapriv->sign($plaintext);\nprint \"Signed correctly\\n\" if ($rsa->verify($plaintext, $signature));\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "\"Crypt::OpenSSL::RSA\" provides the ability to RSA encrypt strings which are somewhat shorter\nthan the block size of a key. It also allows for decryption, signatures and signature\nverification.\n\n*NOTE*: Many of the methods in this package can croak, so use \"eval\", or Error.pm's try/catch\nmechanism to capture errors. Also, while some methods from earlier versions of this package\nreturn true on success, this (never documented) behavior is no longer the case.\n",
            "subsections": []
        },
        "Class Methods": {
            "content": "newpublickey\nCreate a new \"Crypt::OpenSSL::RSA\" object by loading a public key in from a string\ncontaining Base64/DER-encoding of either the PKCS1 or X.509 representation of the key. The\nstring should include the \"-----BEGIN...-----\" and \"-----END...-----\" lines.\n\nThe padding is set to PKCS1OAEP, but can be changed with the \"usexxxpadding\" methods.\n\nnewprivatekey\nCreate a new \"Crypt::OpenSSL::RSA\" object by loading a private key in from an string\ncontaining the Base64/DER encoding of the PKCS1 representation of the key. The string should\ninclude the \"-----BEGIN...-----\" and \"-----END...-----\" lines. The padding is set to\nPKCS1OAEP, but can be changed with \"usexxxpadding\".\n\ngeneratekey\nCreate a new \"Crypt::OpenSSL::RSA\" object by constructing a private/public key pair. The\nfirst (mandatory) argument is the key size, while the second optional argument specifies the\npublic exponent (the default public exponent is 65537). The padding is set to \"PKCS1OAEP\",\nbut can be changed with usexxxpadding methods.\n\nnewkeyfromparameters\nGiven Crypt::OpenSSL::Bignum objects for n, e, and optionally d, p, and q, where p and q are\nthe prime factors of n, e is the public exponent and d is the private exponent, create a new\nCrypt::OpenSSL::RSA object using these values. If p and q are provided and d is undef, d is\ncomputed. Note that while p and q are not necessary for a private key, their presence will\nspeed up computation.\n\nimportrandomseed\nImport a random seed from Crypt::OpenSSL::Random, since the OpenSSL libraries won't allow\nsharing of random structures across perl XS modules.\n",
            "subsections": []
        },
        "Instance Methods": {
            "content": "DESTROY\nClean up after ourselves. In particular, erase and free the memory occupied by the RSA key\nstructure.\n\ngetpublickeystring\nReturn the Base64/DER-encoded PKCS1 representation of the public key. This string has header\nand footer lines:\n\n-----BEGIN RSA PUBLIC KEY------\n-----END RSA PUBLIC KEY------\n\ngetpublickeyx509string\nReturn the Base64/DER-encoded representation of the \"subject public key\", suitable for use\nin X509 certificates. This string has header and footer lines:\n\n-----BEGIN PUBLIC KEY------\n-----END PUBLIC KEY------\n\nand is the format that is produced by running \"openssl rsa -pubout\".\n\ngetprivatekeystring\nReturn the Base64/DER-encoded PKCS1 representation of the private key. This string has\nheader and footer lines:\n\n-----BEGIN RSA PRIVATE KEY------\n-----END RSA PRIVATE KEY------\n\nencrypt\nEncrypt a binary \"string\" using the public (portion of the) key.\n\ndecrypt\nDecrypt a binary \"string\". Croaks if the key is public only.\n\nprivateencrypt\nEncrypt a binary \"string\" using the private key. Croaks if the key is public only.\n\npublicdecrypt\nDecrypt a binary \"string\" using the public (portion of the) key.\n\nsign\nSign a string using the secret (portion of the) key.\n\nverify\nCheck the signature on a text.\n\nusenopadding\nUse raw RSA encryption. This mode should only be used to implement cryptographically sound\npadding modes in the application code. Encrypting user data directly with RSA is insecure.\n\nusepkcs1padding\nUse PKCS #1 v1.5 padding. This currently is the most widely used mode of padding.\n\nusepkcs1oaeppadding\nUse \"EME-OAEP\" padding as defined in PKCS #1 v2.0 with SHA-1, MGF1 and an empty encoding\nparameter. This mode of padding is recommended for all new applications. It is the default\nmode used by \"Crypt::OpenSSL::RSA\".\n\nusesslv23padding\nUse \"PKCS #1 v1.5\" padding with an SSL-specific modification that denotes that the server is\nSSL3 capable.\n\nusemd5hash\nUse the RFC 1321 MD5 hashing algorithm by Ron Rivest when signing and verifying messages.\n\nNote that this is considered insecure.\n\nusesha1hash\nUse the RFC 3174 Secure Hashing Algorithm (FIPS 180-1) when signing and verifying messages.\nThis is the default, when usesha256hash is not available.\n\nusesha224hash, usesha256hash, usesha384hash, usesha512hash\nThese FIPS 180-2 hash algorithms, for use when signing and verifying messages, are only\navailable with newer openssl versions (>= 0.9.8).\n\nusesha256hash is the default hash mode when available.\n\nuseripemd160hash\nDobbertin, Bosselaers and Preneel's RIPEMD hashing algorithm when signing and verifying\nmessages.\n\nusewhirlpoolhash\nVincent Rijmen und Paulo S. L. M. Barreto ISO/IEC 10118-3:2004 WHIRLPOOL hashing algorithm\nwhen signing and verifying messages.\n\nsize\nReturns the size, in bytes, of the key. All encrypted text will be of this size, and\ndepending on the padding mode used, the length of the text to be encrypted should be:\n\npkcs1oaeppadding\nat most 42 bytes less than this size.\n\npkcs1padding or sslv23padding\nat most 11 bytes less than this size.\n\nnopadding\nexactly this size.\n\ncheckkey\nThis function validates the RSA key, returning a true value if the key is valid, and a false\nvalue otherwise. Croaks if the key is public only.\n\ngetkeyparameters\nReturn \"Crypt::OpenSSL::Bignum\" objects representing the values of \"n\", \"e\", \"d\", \"p\", \"q\",\n\"d mod (p-1)\", \"d mod (q-1)\", and \"1/q mod p\", where \"p\" and \"q\" are the prime factors of\n\"n\", \"e\" is the public exponent and \"d\" is the private exponent. Some of these values may\nreturn as \"undef\"; only \"n\" and \"e\" will be defined for a public key. The\n\"Crypt::OpenSSL::Bignum\" module must be installed for this to work.\n\nisprivate\nReturn true if this is a private key, and false if it is private only.\n",
            "subsections": []
        },
        "BUGS": {
            "content": "There is a small memory leak when generating new keys of more than 512 bits.\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "Ian Robertson, \"iroberts@cpan.org\". For support, please email\n\"perl-openssl-users@lists.sourceforge.net\".\n",
            "subsections": []
        },
        "ACKNOWLEDGEMENTS": {
            "content": "",
            "subsections": []
        },
        "LICENSE": {
            "content": "Copyright (c) 2001-2011 Ian Robertson. Crypt::OpenSSL::RSA is free software; you may\nredistribute it and/or modify it under the same terms as Perl itself.\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "",
            "subsections": [
                {
                    "name": "perl",
                    "content": "<http://man.he.net/?topic=RSAnew&section=3>, RSApublicencrypt(3)\n<http://man.he.net/?topic=RSApublicencrypt&section=3>, RSAsize(3)\n<http://man.he.net/?topic=RSAsize&section=3>, RSAgeneratekey(3)\n<http://man.he.net/?topic=RSAgeneratekey&section=3>, RSAcheckkey(3)\n<http://man.he.net/?topic=RSAcheckkey&section=3>\n"
                }
            ]
        }
    },
    "summary": "Crypt::OpenSSL::RSA - RSA encoding and decoding, using the openSSL libraries",
    "flags": [],
    "examples": [],
    "see_also": [
        {
            "name": "RSApublicencrypt",
            "section": "3",
            "url": "https://www.chedong.com/phpMan.php/man/RSApublicencrypt/3/json"
        },
        {
            "name": "RSAsize",
            "section": "3",
            "url": "https://www.chedong.com/phpMan.php/man/RSAsize/3/json"
        },
        {
            "name": "RSAgeneratekey",
            "section": "3",
            "url": "https://www.chedong.com/phpMan.php/man/RSAgeneratekey/3/json"
        },
        {
            "name": "RSAcheckkey",
            "section": "3",
            "url": "https://www.chedong.com/phpMan.php/man/RSAcheckkey/3/json"
        }
    ]
}