encrypt_asymmetric
encrypt_asymmetric
Functions
| Name | Description |
|---|---|
| encrypt_asymmetric | Encrypts a message using the receiver’s public key and signs it with the |
encrypt_asymmetric
encrypt_asymmetric.encrypt_asymmetric(
message,
receiver_public_key,
sender_private_key,
)Encrypts a message using the receiver’s public key and signs it with the sender’s private key. This ensures both confidentiality (only receiver can decrypt) and authenticity (receiver can verify sender).
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| message | str | The plaintext message to encrypt. | required |
| receiver_public_key | bytes or str | The receiver’s RSA public key in PEM format (bytes) or path to key file (str). | required |
| sender_private_key | bytes or str | The sender’s RSA private key in PEM format (bytes) or path to key file (str) for signing. | required |
Returns
| Name | Type | Description |
|---|---|---|
| str | Base64-encoded JSON containing encrypted message and signature. |
Raises
| Name | Type | Description |
|---|---|---|
| ValueError | If the message is too long for RSA encryption or keys are invalid. |
Examples
>>> from imitation_game.generate_asymmetric_key import generate_asymmetric_key
>>> from imitation_game.encrypt_asymmetric import encrypt_asymmetric
>>> receiver_public, _ = generate_asymmetric_key()
>>> _, sender_private = generate_asymmetric_key()
>>> encrypted = encrypt_asymmetric(
... "Hello, world!",
... receiver_public,
... sender_private,
... )