def _encrypt(certificate_or_public_key, data, padding):
if not isinstance(certificate_or_public_key, (Certificate, PublicKey)):<EOL><INDENT>raise TypeError(pretty_message(<EOL>'''<STR_LIT>''',<EOL>type_name(certificate_or_public_key)<EOL>))<EOL><DEDENT>if not isinstance(data, byte_cls):<EOL><INDENT>raise TypeError(pretty_message(<EOL>'''<STR_LIT>''',<EOL>type_name(data)<EOL>))<EOL><DEDENT>rsa = None<EOL>try:<EOL><INDENT>buffer_size = libcrypto.EVP_PKEY_size(certificate_or_public_key.evp_pkey)<EOL>buffer = buffer_from_bytes(buffer_size)<EOL>rsa = libcrypto.EVP_PKEY_get1_RSA(certificate_or_public_key.evp_pkey)<EOL>res = libcrypto.RSA_public_encrypt(len(data), data, buffer, rsa, padding)<EOL>handle_openssl_error(res)<EOL>return bytes_from_buffer(buffer, res)<EOL><DEDENT>finally:<EOL><INDENT>if rsa:<EOL><INDENT>libcrypto.RSA_free(rsa)<EOL><DEDENT><DEDENT>
Encrypts plaintext using an RSA public key or certificate

:param certificate_or_public_key:
    A PublicKey, Certificate or PrivateKey object

:param data:
    The byte string to encrypt

:param padding:
    The padding mode to use

:raises:
    ValueError - when any of the parameters contain an invalid value
    TypeError - when any of the parameters are of the wrong type
    OSError - when an error is returned by the OS crypto library

:return:
    A byte string of the encrypted data