def save_private_key_to_pem(key: RSA, output_stream: BinaryIO, password: str, protection: str = 'PBKDF2WithHMAC-SHA1AndAES256-CBC') -> None: """Save private RSA key :param key: RSA key :param output_stream: Output stream :param password: Password :param protection: Protection algorithm """ encrypted_key = key.export_key(passphrase=password, pkcs=8, protection=protection) output_stream.write(encrypted_key)