def rsa_encrypt(data, pem, b64_encode=True):
from cryptography.hazmat.backends import default_backend<EOL>from cryptography.hazmat.primitives import serialization<EOL>from cryptography.hazmat.primitives import hashes<EOL>from cryptography.hazmat.primitives.asymmetric import padding<EOL>encoded_data = to_binary(data)<EOL>pem = to_binary(pem)<EOL>public_key = serialization.load_pem_public_key(pem, backend=default_backend())<EOL>encrypted_data = public_key.encrypt(<EOL>encoded_data,<EOL>padding=padding.OAEP(<EOL>mgf=padding.MGF1(hashes.SHA1()),<EOL>algorithm=hashes.SHA1(),<EOL>label=None,<EOL>)<EOL>)<EOL>if b64_encode:<EOL><INDENT>encrypted_data = base64.b64encode(encrypted_data).decode('<STR_LIT:utf-8>')<EOL><DEDENT>return encrypted_data<EOL>
rsa 加密
:param data: 待加密字符串/binary
:param pem: RSA public key 内容/binary
:param b64_encode: 是否对输出进行 base64 encode
:return: 如果 b64_encode=True 的话，返回加密并 base64 处理后的 string；否则返回加密后的 binary