| 1 |
PKCS#1 v1.5 encryption (RSA) |
| 2 |
============================ |
| 3 |
|
| 4 |
.. warning:: |
| 5 |
Use :doc:`oaep` instead. This module is provided only for legacy purposes. |
| 6 |
|
| 7 |
See RFC8017__ or the `original RSA Labs specification`__ . |
| 8 |
|
| 9 |
This scheme is more properly called ``RSAES-PKCS1-v1_5``. |
| 10 |
|
| 11 |
As an example, a sender may encrypt a secret AES key in this way:: |
| 12 |
|
| 13 |
>>> from Crypto.Cipher import PKCS1_v1_5 |
| 14 |
>>> from Crypto.PublicKey import RSA |
| 15 |
>>> from Crypto.Random import get_random_bytes |
| 16 |
>>> |
| 17 |
>>> aes_key = get_random_bytes(16) |
| 18 |
>>> |
| 19 |
>>> rsa_key = RSA.importKey(open('pubkey.der').read()) |
| 20 |
>>> cipher = PKCS1_v1_5.new(rsa_key) |
| 21 |
>>> ciphertext = cipher.encrypt(aes_key) |
| 22 |
|
| 23 |
At the receiver side, decryption can be done using the private part of |
| 24 |
the RSA key:: |
| 25 |
|
| 26 |
>>> from Crypto.Random import get_random_bytes |
| 27 |
>>> |
| 28 |
>>> rsa_key = RSA.importKey(open('privkey.der').read()) |
| 29 |
>>> |
| 30 |
>>> sentinel = get_random_bytes(16) |
| 31 |
>>> |
| 32 |
>>> cipher = PKCS1_v1_5.new(rsa_key) |
| 33 |
>>> aes_key = cipher.decrypt(ciphertext, sentinel, expected_pt_len=16) |
| 34 |
>>> |
| 35 |
>>> # The AES key is the random sentinel in case of error |
| 36 |
|
| 37 |
.. __: https://tools.ietf.org/html/rfc8017 |
| 38 |
.. __: http://www.rsa.com/rsalabs/node.asp?id=2125. |
| 39 |
|
| 40 |
.. automodule:: Crypto.Cipher.PKCS1_v1_5 |
| 41 |
:members: |