blob: 7ca4fed8187673915c89c16010d02fd8384eb772 [file] [log] [blame]
Sybren A. Stüvelaa28c042011-07-30 23:48:00 +02001Usage
2==================================================
3
Sybren A. Stüvela3fd61a2011-07-31 00:22:31 +02004This section describes the usage of the Python-RSA module.
Sybren A. Stüvelaa28c042011-07-30 23:48:00 +02005
Sybren A. Stüvela3fd61a2011-07-31 00:22:31 +02006Before you can use RSA you need keys. You will receive a private key
7and 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üveld92b6672011-07-31 17:44:44 +020014The public key is used for encypting a message such that it can only
15be read by the owner of the private key. As such it's also referred to
16as the *encryption key*. Decrypting a message can only be done using
17the private key, hence it's also called the *decryption key*.
18
19The private key is used for signing a message. With this signature and
20the public key, the receiver can verifying that a message was signed
21by the owner of the private key, and that the message was not modified
22after signing.
23
24Generating keys
25--------------------------------------------------
26
Sybren A. Stüvel1f011e12011-07-31 19:20:46 +020027You can use the :py:func:`rsa.newkeys` function to create a keypair:
28
29 >>> (pubkey, privkey) = rsa.newkeys(512)
30
31Alternatively 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üveld92b6672011-07-31 17:44:44 +020037
38Generating a keypair may take a long time, depending on the number of
39bits required. The number of bits determines the cryptographic
40strength of the key, as well as the size of the message you can
41encrypt. If you don't mind having a slightly smaller key than you
42requested, you can pass ``accurate=False`` to speed up the key
43generation process.
44
45These are some timings from my netbook (Linux 2.6, 1.6 GHz Intel Atom
46N270 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üvela3fd61a2011-07-31 00:22:31 +020070
71Encryption and decryption
72--------------------------------------------------
73
Sybren A. Stüveld92b6672011-07-31 17:44:44 +020074To 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
76that 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üvel1f011e12011-07-31 19:20:46 +020082 >>> (bob_pub, bob_priv) = rsa.newkeys(512)
83
Sybren A. Stüveld92b6672011-07-31 17:44:44 +020084#. Alice writes a message
85
Sybren A. Stüvel1f011e12011-07-31 19:20:46 +020086 >>> message = 'hello Bob!'
87
Sybren A. Stüveld92b6672011-07-31 17:44:44 +020088#. Alice encrypts the message using Bob's public key, and sends the
89 encrypted message.
90
Sybren A. Stüvel1f011e12011-07-31 19:20:46 +020091 >>> cryto = rsa.encrypt(message, bob_pub)
92
Sybren A. Stüveld92b6672011-07-31 17:44:44 +020093#. Bob receives the message, and decrypts it with his private key.
94
Sybren A. Stüvel1f011e12011-07-31 19:20:46 +020095 >>> message = rsa.decrypt(crypto, bob_priv)
96 >>> print message
97 hello Bob!
98
Sybren A. Stüveld92b6672011-07-31 17:44:44 +020099Since Bob kept his private key *private*, Alice can be sure that he is
Sybren A. Stüvel1f011e12011-07-31 19:20:46 +0200100the 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üveld92b6672011-07-31 17:44:44 +0200106
107
108Low-level operations
109++++++++++++++++++++++++++++++
110
111The core RSA algorithm operates on large integers. These operations
112are considered low-level and are supported by the
113:py:func:`rsa.core.encrypt_int` and :py:func:`rsa.core.decrypt_int`
114functions.
Sybren A. Stüvela3fd61a2011-07-31 00:22:31 +0200115
116Signing and verification
117--------------------------------------------------
118
Sybren A. Stüvel1f011e12011-07-31 19:20:46 +0200119You 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
126This hashes the message using SHA-1. Other hash methods are also
127possible, check the :py:func:`rsa.sign` function documentation for
128details. The hash is then signed with the private key.
129
130In order to verify the signature, use the :py:func:`rsa.verify`
131function.
132
133 >>> message = 'Go left at the blue tree'
134 >>> rsa.verify(message, signature, pubkey)
135
136Modify 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üvela3fd61a2011-07-31 00:22:31 +0200155
156Working with big files
157--------------------------------------------------
158
159
Sybren A. Stüvelaa28c042011-07-30 23:48:00 +0200160