18 |
19 | 
20 | |
21 |
22 |
23 | Author: | Todd Whiteman |
24 | Issue Date: | 28th April, 2010 |
25 | Version: | 2.0.1 |
26 | Compatibility: | Requires Python 2.2 or higher (or Python 3)
|
27 | |
28 | Download (Windows): | pyDes-2.0.1.zip |
29 | Download (Unix): | pyDes-2.0.1.tar.gz |
30 | |
31 | License: | MIT |
32 | Source code: | SourceForge: pyDes |
33 | Last updated: | 28th April, 2010 |
34 |
35 |
36 |
37 | About pyDES
38 |
39 | This is a pure python implementation of the DES encryption algorithm.
40 | It is in pure python to avoid portability issues, since most DES
41 | implementations are programmed in C (for performance reasons).
42 |
43 |
44 |
45 | Triple DES class is also implemented, utilising the DES base. Triple DES
46 | is either DES-EDE3 with a 24 byte key, or DES-EDE2 with a 16 byte key.
47 | See the "About triple DES" section below more info on this algorithm.
48 |
49 |
50 |
51 | The code below is not written for speed or performance, so not for those
52 | needing a fast des implementation, but rather a handy portable solution ideal
53 | for small usage. It takes my AMD2000+ machine 1 second per 2.5 kilobyte to
54 | encrypt or decrypt using the DES method. That is extremely SLOW!!
55 |
56 |
57 | About triple DES
58 |
59 | Triple DES is just running the DES algorithm 3 times over the data with the
60 | specified key. The supplied key is split up into 3 parts, each part being 8
61 | bytes long (the mandatory key size for DES).
62 |
63 |
64 |
65 | The triple DES algorithm uses the DES-EDE3 method when a 24 byte key is
66 | supplied. This means there are three DES operations in the sequence
67 | encrypt-decrypt-encrypt with the three different keys. The first key will be
68 | bytes 1 to 8, the second key bytes 9 to 16 and the third key bytes 17 to 24.
69 |
70 |
71 |
72 | If a 16 byte key is supplied instead, the triple DES method used will be
73 | DES-EDE2. This means there are three DES operations in the sequence
74 | encrypt-decrypt-encrypt, but the first and third operations use the same key.
75 | The first/third key will be bytes 1 to 8 and the second key bytes 9 to 16.
76 |
77 |
78 | Credits
79 |
80 | Thanks go to:
81 |
82 | - David Broadwell for his ideas, comments and suggestions.
83 | - Mario Wolff for finding errors in triple des CBC.
84 | - Santiago Palladino for providing the PKCS5 padding technique.
85 | - Shaya for correcting the PAD_PKCS5 triple des CBC errors.
86 | - Yoav Aner for spotting a triple des CBC IV error.
87 |
88 |
89 |
90 | Installation
91 |
92 | - Extract the files from the pyDes archive.
93 | - Run the following command: python setup.py install
94 | - To test, run: python test_pydes.py
95 |
96 |
97 | Note: On Unix, you'd run this command from a shell prompt; on Windows, you
98 | have to open a command prompt window (``DOS box'') and do it there;
99 |
100 |
101 | pyDes Usage
102 |
103 | Class initialization
104 | --------------------
105 | pyDes.des(key, [mode], [IV], [pad], [padmode])
106 | pyDes.triple_des(key, [mode], [IV], [pad], [padmode])
107 |
108 | key -> Bytes containing the encryption key. 8 bytes for DES, 16 or 24 bytes
109 | for Triple DES
110 | mode -> Optional argument for encryption type, can be either
111 | pyDes.ECB (Electronic Code Book) or pyDes.CBC (Cypher Block Chaining)
112 | IV -> Optional Initial Value bytes, must be supplied if using CBC mode.
113 | Length must be 8 bytes.
114 | pad -> Optional argument, set the pad character (PAD_NORMAL) to use during
115 | all encrypt/decrpt operations done with this instance.
116 | padmode -> Optional argument, set the padding mode (PAD_NORMAL or PAD_PKCS5)
117 | to use during all encrypt/decrpt operations done with this instance.
118 |
119 | I recommend to use PAD_PKCS5 padding, as then you never need to worry about any
120 | padding issues, as the padding can be removed unambiguously upon decrypting
121 | data that was encrypted using PAD_PKCS5 padmode.
122 |
123 | Common methods
124 | --------------
125 | encrypt(data, [pad], [padmode])
126 | decrypt(data, [pad], [padmode])
127 |
128 | data -> Bytes to be encrypted/decrypted
129 | pad -> Optional argument. Only when using padmode of PAD_NORMAL. For
130 | encryption, adds this characters to the end of the data block when
131 | data is not a multiple of 8 bytes. For decryption, will remove the
132 | trailing characters that match this pad character from the last 8
133 | bytes of the unencrypted data block.
134 | padmode -> Optional argument, set the padding mode, must be one of PAD_NORMAL
135 | or PAD_PKCS5). Defaults to PAD_NORMAL.
136 |
137 |
138 | Example
139 | -------
140 | from pyDes import *
141 |
142 | # For Python3, you'll need to use bytes, i.e.:
143 | # data = b"Please encrypt my data"
144 | # k = des(b"DESCRYPT", CBC, b"\0\0\0\0\0\0\0\0", pad=None, padmode=PAD_PKCS5)
145 |
146 | data = "Please encrypt my data"
147 | k = des("DESCRYPT", CBC, "\0\0\0\0\0\0\0\0", pad=None, padmode=PAD_PKCS5)
148 | d = k.encrypt(data)
149 | print "Encrypted: %r" % d
150 | print "Decrypted: %r" % k.decrypt(d)
151 | assert k.decrypt(d, padmode=PAD_PKCS5) == data
152 |
153 |
154 |
155 | See the module source (pyDes.py) for more examples of use.
156 | You can slo run the pyDes.py file without and arguments to see a simple test.
157 |
158 | Note: This code was not written for high-end systems needing a fast
159 | implementation, but rather a handy portable solution with small usage.
160 |
161 |
162 | Older pyDES versions
163 |
164 | Download (Windows): pyDes-2.0.0.zip
165 | Download (Unix): pyDes-2.0.0.tar.gz
166 |
167 |
168 | Download (Windows): pyDes-1.3.1.zip
169 | Download (Unix): pyDes-1.3.1.tar.gz
170 |
171 |
172 | Download (Windows): pyDes-1.3.zip
173 | Download (Unix): pyDes-1.3.tar.gz
174 |
175 |
176 | Download (Windows): pyDes-1.2.zip
177 | Download (Unix): pyDes-1.2.tar.gz
178 |
179 |
180 | Download (Windows): pyDes-1.1.zip
181 | Download (Unix): pyDes-1.1.tar.gz
182 |
183 |
184 | |
185 |
186 |