A.2 Cryptographic RoutinesThis section shows implementations of some simple cryptographic algorithms mentioned in Chapter 3. Two implementations of the Extended Euclidean Algorithm, which is used to find inverses in Zp, are also shown. Be aware that these implementations are intended to illustrate the principles involved and are definitely not meant to be used in real cryptographic software. As we've seen throughout the text, writing robust security software is difficult and requires special diligence to avoid potential exploits. Unfortunately, writing robust security software involves details that obscure the underlying principles the examples are intended to illustrate, so the examples were chosen to favor simplicity over robustness. A Trivial CipherThis subsection shows a python implementation (Figure A.1) of the trivial cipher that we saw in Chapter 3. As mentioned there, this cipher is useful only as an example of what not to do. Figure A.1. An Implementation of the Trivial Cipher
An RC4 ImplementationFigure A.2 shows a python implementation of RC4 written to act as a filter. Notice that it is only barely more complicated than the trivial cipher. Figure A.2. An Implementation of RC4
Finding Inverses in ZpRecall from Chapter 3 that the RSA algorithm requires us to find an inverse for x We present two implementations of this algorithm: one using the UNIX bc utility and the other using python. Both implementations are based on Algorithm X from Section 4.5.2 of [Knuth 1998]. Our first implementation (Figure A.3) uses bc. Normally, bc is used interactively, but it can also accept input from a script. See Knuth for an explanation of this algorithm and a proof that it is correct. Figure A.3. A bc-Based Implementation of the Extended Euclidean Algorithm
Figure A.4 gives a slightly more readable python-based implementation. Figure A.4. A python-Based Implementation of the Extended Euclidean Algorithm
|