Sybren A. Stüvel | aa28c04 | 2011-07-30 23:48:00 +0200 | [diff] [blame] | 1 | Usage |
| 2 | ================================================== |
| 3 | |
Sybren A. Stüvel | a3fd61a | 2011-07-31 00:22:31 +0200 | [diff] [blame] | 4 | This section describes the usage of the Python-RSA module. |
Sybren A. Stüvel | aa28c04 | 2011-07-30 23:48:00 +0200 | [diff] [blame] | 5 | |
Sybren A. Stüvel | a3fd61a | 2011-07-31 00:22:31 +0200 | [diff] [blame] | 6 | Before you can use RSA you need keys. You will receive a private key |
| 7 | and a public key. |
| 8 | |
| 9 | .. note:: |
| 10 | |
| 11 | The private key is called *private* for a reason. Never share this |
| 12 | key with anyone. |
| 13 | |
Sybren A. Stüvel | d92b667 | 2011-07-31 17:44:44 +0200 | [diff] [blame] | 14 | The public key is used for encypting a message such that it can only |
| 15 | be read by the owner of the private key. As such it's also referred to |
| 16 | as the *encryption key*. Decrypting a message can only be done using |
| 17 | the private key, hence it's also called the *decryption key*. |
| 18 | |
| 19 | The private key is used for signing a message. With this signature and |
| 20 | the public key, the receiver can verifying that a message was signed |
| 21 | by the owner of the private key, and that the message was not modified |
| 22 | after signing. |
| 23 | |
| 24 | Generating keys |
| 25 | -------------------------------------------------- |
| 26 | |
Sybren A. Stüvel | 1f011e1 | 2011-07-31 19:20:46 +0200 | [diff] [blame] | 27 | You can use the :py:func:`rsa.newkeys` function to create a keypair: |
| 28 | |
| 29 | >>> (pubkey, privkey) = rsa.newkeys(512) |
| 30 | |
| 31 | Alternatively you can use :py:meth:`rsa.PrivateKey.load_pkcs1` and |
| 32 | :py:meth:`rsa.PublicKey.load_pkcs1` to load keys from a file: |
| 33 | |
| 34 | >>> with open('private.pem') as privatefile: |
| 35 | ... keydata = privatefile.read() |
| 36 | >>> pubkey = rsa.PrivateKey.load_pkcs1(keydata) |
Sybren A. Stüvel | d92b667 | 2011-07-31 17:44:44 +0200 | [diff] [blame] | 37 | |
| 38 | Generating a keypair may take a long time, depending on the number of |
| 39 | bits required. The number of bits determines the cryptographic |
| 40 | strength of the key, as well as the size of the message you can |
| 41 | encrypt. If you don't mind having a slightly smaller key than you |
| 42 | requested, you can pass ``accurate=False`` to speed up the key |
| 43 | generation process. |
| 44 | |
| 45 | These are some timings from my netbook (Linux 2.6, 1.6 GHz Intel Atom |
| 46 | N270 CPU, 2 GB RAM): |
| 47 | |
| 48 | +----------------+------------------+ |
| 49 | | Keysize (bits) | Time to generate | |
| 50 | +================+==================+ |
| 51 | | 32 | 0.01 sec. | |
| 52 | +----------------+------------------+ |
| 53 | | 64 | 0.03 sec. | |
| 54 | +----------------+------------------+ |
| 55 | | 96 | 0.04 sec. | |
| 56 | +----------------+------------------+ |
| 57 | | 128 | 0.08 sec. | |
| 58 | +----------------+------------------+ |
| 59 | | 256 | 0.27 sec. | |
| 60 | +----------------+------------------+ |
| 61 | | 384 | 0.93 sec. | |
| 62 | +----------------+------------------+ |
| 63 | | 512 | 1.21 sec. | |
| 64 | +----------------+------------------+ |
| 65 | | 1024 | 7.93 sec. | |
| 66 | +----------------+------------------+ |
| 67 | | 2048 | 132.97 sec. | |
| 68 | +----------------+------------------+ |
| 69 | |
Sybren A. Stüvel | a3fd61a | 2011-07-31 00:22:31 +0200 | [diff] [blame] | 70 | |
| 71 | Encryption and decryption |
| 72 | -------------------------------------------------- |
| 73 | |
Sybren A. Stüvel | d92b667 | 2011-07-31 17:44:44 +0200 | [diff] [blame] | 74 | To encrypt or decrypt a message, use :py:func:`rsa.encrypt` resp. |
| 75 | :py:func:`rsa.decrypt`. Let's say that Alice wants to send a message |
| 76 | that only Bob can read. |
| 77 | |
| 78 | #. Bob generates a keypair, and gives the public key to Alice. This is |
| 79 | done such that Alice knows for sure that the key is really Bob's |
| 80 | (for example by handing over a USB stick that contains the key). |
| 81 | |
Sybren A. Stüvel | 1f011e1 | 2011-07-31 19:20:46 +0200 | [diff] [blame] | 82 | >>> (bob_pub, bob_priv) = rsa.newkeys(512) |
| 83 | |
Sybren A. Stüvel | d92b667 | 2011-07-31 17:44:44 +0200 | [diff] [blame] | 84 | #. Alice writes a message |
| 85 | |
Sybren A. Stüvel | 1f011e1 | 2011-07-31 19:20:46 +0200 | [diff] [blame] | 86 | >>> message = 'hello Bob!' |
| 87 | |
Sybren A. Stüvel | d92b667 | 2011-07-31 17:44:44 +0200 | [diff] [blame] | 88 | #. Alice encrypts the message using Bob's public key, and sends the |
| 89 | encrypted message. |
| 90 | |
Sybren A. Stüvel | db34825 | 2011-07-31 19:22:47 +0200 | [diff] [blame] | 91 | >>> crypto = rsa.encrypt(message, bob_pub) |
Sybren A. Stüvel | 1f011e1 | 2011-07-31 19:20:46 +0200 | [diff] [blame] | 92 | |
Sybren A. Stüvel | d92b667 | 2011-07-31 17:44:44 +0200 | [diff] [blame] | 93 | #. Bob receives the message, and decrypts it with his private key. |
| 94 | |
Sybren A. Stüvel | 1f011e1 | 2011-07-31 19:20:46 +0200 | [diff] [blame] | 95 | >>> message = rsa.decrypt(crypto, bob_priv) |
| 96 | >>> print message |
| 97 | hello Bob! |
| 98 | |
Sybren A. Stüvel | d92b667 | 2011-07-31 17:44:44 +0200 | [diff] [blame] | 99 | Since Bob kept his private key *private*, Alice can be sure that he is |
Sybren A. Stüvel | 1f011e1 | 2011-07-31 19:20:46 +0200 | [diff] [blame] | 100 | the only one who can read the message. |
| 101 | |
| 102 | .. note:: |
| 103 | |
| 104 | Bob does *not* know for sure that it was Alice that sent the |
| 105 | message, since she didn't sign it. |
Sybren A. Stüvel | d92b667 | 2011-07-31 17:44:44 +0200 | [diff] [blame] | 106 | |
| 107 | |
| 108 | Low-level operations |
| 109 | ++++++++++++++++++++++++++++++ |
| 110 | |
| 111 | The core RSA algorithm operates on large integers. These operations |
| 112 | are considered low-level and are supported by the |
| 113 | :py:func:`rsa.core.encrypt_int` and :py:func:`rsa.core.decrypt_int` |
| 114 | functions. |
Sybren A. Stüvel | a3fd61a | 2011-07-31 00:22:31 +0200 | [diff] [blame] | 115 | |
| 116 | Signing and verification |
| 117 | -------------------------------------------------- |
| 118 | |
Sybren A. Stüvel | 1f011e1 | 2011-07-31 19:20:46 +0200 | [diff] [blame] | 119 | You can create a detached signature for a message using the |
| 120 | :py:func:`rsa.sign` function: |
| 121 | |
| 122 | >>> (pubkey, privkey) = rsa.newkeys(512) |
| 123 | >>> message = 'Go left at the blue tree' |
| 124 | >>> signature = rsa.sign(message, privkey, 'SHA-1') |
| 125 | |
| 126 | This hashes the message using SHA-1. Other hash methods are also |
| 127 | possible, check the :py:func:`rsa.sign` function documentation for |
| 128 | details. The hash is then signed with the private key. |
| 129 | |
| 130 | In order to verify the signature, use the :py:func:`rsa.verify` |
| 131 | function. |
| 132 | |
| 133 | >>> message = 'Go left at the blue tree' |
| 134 | >>> rsa.verify(message, signature, pubkey) |
| 135 | |
| 136 | Modify the message, and the signature is no longer valid and a |
| 137 | :py:class:`rsa.pkcs1.VerificationError` is thrown: |
| 138 | |
| 139 | >>> message = 'Go right at the blue tree' |
| 140 | >>> rsa.verify(message, signature, pubkey) |
| 141 | Traceback (most recent call last): |
| 142 | File "<stdin>", line 1, in <module> |
| 143 | File "/home/sybren/workspace/python-rsa/rsa/pkcs1.py", line 289, in verify |
| 144 | raise VerificationError('Verification failed') |
| 145 | rsa.pkcs1.VerificationError: Verification failed |
| 146 | |
| 147 | .. note:: |
| 148 | |
| 149 | Never display the stack trace of a |
| 150 | :py:class:`rsa.pkcs1.VerificationError` exception. It shows where |
| 151 | in the code the exception occurred, and thus leaks information |
| 152 | about the key. It's only a tiny bit of information, but every bit |
| 153 | makes cracking the keys easier. |
| 154 | |
Sybren A. Stüvel | a3fd61a | 2011-07-31 00:22:31 +0200 | [diff] [blame] | 155 | |
| 156 | Working with big files |
| 157 | -------------------------------------------------- |
| 158 | |
| 159 | |
Sybren A. Stüvel | aa28c04 | 2011-07-30 23:48:00 +0200 | [diff] [blame] | 160 | |