@classmethod<EOL><INDENT>def encrypt(cls, private_key, password, kdf=None, iterations=None):<DEDENT>
if isinstance(private_key, keys.PrivateKey):<EOL><INDENT>key_bytes = private_key.to_bytes()<EOL><DEDENT>else:<EOL><INDENT>key_bytes = HexBytes(private_key)<EOL><DEDENT>if kdf is None:<EOL><INDENT>kdf = cls.default_kdf<EOL><DEDENT>password_bytes = text_if_str(to_bytes, password)<EOL>assert len(key_bytes) == <NUM_LIT:32><EOL>return create_keyfile_json(key_bytes, password_bytes, kdf=kdf, iterations=iterations)<EOL>
Creates a dictionary with an encrypted version of your private key.
To import this keyfile into Ethereum clients like geth and parity:
encode this dictionary with :func:`json.dumps` and save it to disk where your
client keeps key files.

:param private_key: The raw private key
:type private_key: hex str, bytes, int or :class:`eth_keys.datatypes.PrivateKey`
:param str password: The password which you will need to unlock the account in your client
:param str kdf: The key derivation function to use when encrypting your private key
:param int iterations: The work factor for the key derivation function
:returns: The data to use in your encrypted file
:rtype: dict

.. code-block:: python

    >>> import getpass
    >>> encrypted = Account.encrypt(
        0xb25c7db31feed9122727bf0939dc769a96564b2de4c4726d035b36ecf1e5b364,
        getpass.getpass()
    )

    {
        'address': '5ce9454909639d2d17a3f753ce7d93fa0b9ab12e',
        'crypto': {
            'cipher': 'aes-128-ctr',
            'cipherparams': {
                'iv': '0b7845a5c3597d3d378bde9b7c7319b7'
            },
            'ciphertext': 'a494f1feb3c854e99c1ff01e6aaa17d43c0752009073503b908457dc8de5d2a5',  # noqa: E501
            'kdf': 'scrypt',
            'kdfparams': {
                'dklen': 32,
                'n': 262144,
                'p': 8,
                'r': 1,
                'salt': '13c4a48123affaa29189e9097726c698'
            },
            'mac': 'f4cfb027eb0af9bd7a320b4374a3fa7bef02cfbafe0ec5d1fd7ad129401de0b1'
        },
        'id': 'a60e0578-0e5b-4a75-b991-d55ec6451a6f',
        'version': 3
    }

     >>> with open('my-keyfile', 'w') as f:
         f.write(json.dumps(encrypted))