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 | |
| 27 | You can use the :py:func:`rsa.newkeys` function to create a keypair. |
| 28 | Alternatively you can use :py:func:`rsa.PrivateKey.load_pkcs1` and |
| 29 | :py:func:`rsa.PublicKey.load_pkcs1` to load keys from a file. |
| 30 | |
| 31 | Generating a keypair may take a long time, depending on the number of |
| 32 | bits required. The number of bits determines the cryptographic |
| 33 | strength of the key, as well as the size of the message you can |
| 34 | encrypt. If you don't mind having a slightly smaller key than you |
| 35 | requested, you can pass ``accurate=False`` to speed up the key |
| 36 | generation process. |
| 37 | |
| 38 | These are some timings from my netbook (Linux 2.6, 1.6 GHz Intel Atom |
| 39 | N270 CPU, 2 GB RAM): |
| 40 | |
| 41 | +----------------+------------------+ |
| 42 | | Keysize (bits) | Time to generate | |
| 43 | +================+==================+ |
| 44 | | 32 | 0.01 sec. | |
| 45 | +----------------+------------------+ |
| 46 | | 64 | 0.03 sec. | |
| 47 | +----------------+------------------+ |
| 48 | | 96 | 0.04 sec. | |
| 49 | +----------------+------------------+ |
| 50 | | 128 | 0.08 sec. | |
| 51 | +----------------+------------------+ |
| 52 | | 256 | 0.27 sec. | |
| 53 | +----------------+------------------+ |
| 54 | | 384 | 0.93 sec. | |
| 55 | +----------------+------------------+ |
| 56 | | 512 | 1.21 sec. | |
| 57 | +----------------+------------------+ |
| 58 | | 1024 | 7.93 sec. | |
| 59 | +----------------+------------------+ |
| 60 | | 2048 | 132.97 sec. | |
| 61 | +----------------+------------------+ |
| 62 | |
Sybren A. Stüvel | a3fd61a | 2011-07-31 00:22:31 +0200 | [diff] [blame] | 63 | |
| 64 | Encryption and decryption |
| 65 | -------------------------------------------------- |
| 66 | |
Sybren A. Stüvel | d92b667 | 2011-07-31 17:44:44 +0200 | [diff] [blame^] | 67 | To encrypt or decrypt a message, use :py:func:`rsa.encrypt` resp. |
| 68 | :py:func:`rsa.decrypt`. Let's say that Alice wants to send a message |
| 69 | that only Bob can read. |
| 70 | |
| 71 | #. Bob generates a keypair, and gives the public key to Alice. This is |
| 72 | done such that Alice knows for sure that the key is really Bob's |
| 73 | (for example by handing over a USB stick that contains the key). |
| 74 | |
| 75 | #. Alice writes a message |
| 76 | |
| 77 | #. Alice encrypts the message using Bob's public key, and sends the |
| 78 | encrypted message. |
| 79 | |
| 80 | #. Bob receives the message, and decrypts it with his private key. |
| 81 | |
| 82 | Since Bob kept his private key *private*, Alice can be sure that he is |
| 83 | the only one who can read the message. Bob does *not* know for sure |
| 84 | that it was Alice that sent the message, since she didn't sign it. |
| 85 | |
| 86 | |
| 87 | Low-level operations |
| 88 | ++++++++++++++++++++++++++++++ |
| 89 | |
| 90 | The core RSA algorithm operates on large integers. These operations |
| 91 | are considered low-level and are supported by the |
| 92 | :py:func:`rsa.core.encrypt_int` and :py:func:`rsa.core.decrypt_int` |
| 93 | functions. |
Sybren A. Stüvel | a3fd61a | 2011-07-31 00:22:31 +0200 | [diff] [blame] | 94 | |
| 95 | Signing and verification |
| 96 | -------------------------------------------------- |
| 97 | |
| 98 | |
| 99 | Working with big files |
| 100 | -------------------------------------------------- |
| 101 | |
| 102 | |
Sybren A. Stüvel | aa28c04 | 2011-07-30 23:48:00 +0200 | [diff] [blame] | 103 | |