/** * converts keypair to PEM-encoded string, which can be stroed in * .pem compatible files, contianing RSA private key. * * @return string PEM-encoded keypair on success, false on error * @access public */ function toPEMString() { // store RSA private key attributes into ASN.1 string $str = ''; $attr_names = $this->_get_attr_names(); $n = sizeof($attr_names); $rsa_attrs = $this->_attrs; for ($i = 0; $i < $n; $i++) { $attr = $attr_names[$i]; if (!isset($rsa_attrs[$attr])) { $this->pushError("Cannot find value for ASN.1 attribute [{$attr}]"); return false; } $tmp = $rsa_attrs[$attr]; $str .= Crypt_RSA_KeyPair::_ASN1StoreInt($tmp); } // prepend $str by ASN.1 SEQUENCE (0x10) header $str = Crypt_RSA_KeyPair::_ASN1Store($str, 0x10, true); // encode and format PEM string $str = base64_encode($str); $str = chunk_split($str, 64, "\n"); return "-----BEGIN RSA PRIVATE KEY-----\n{$str}-----END RSA PRIVATE KEY-----\n"; }