mode constants are only in blockcipher
[python-cryptoplus.git] / src / Cipher / python_DES.py
blob004c67b20b940c4eeb77faaf24e9c9552f0cbdee
1 from blockcipher import *
2 import pyDes
4 def new(key,mode=MODE_ECB,IV=None,counter=None):
5 """Create a new cipher object
7 wrapper for pure python implementation pyDes.py
9 new(key,mode=MODE_ECB,IV=None,counter=None):
10 key = raw string containing the key
11 mode = python_DES.MODE_ECB/CBC/CFB/OFB/CTR/XTS/CMAC, default is ECB
12 -> for every mode, except ECB and CTR, it is important to construct a seperate cipher for encryption and decryption
13 IV = IV as a raw string
14 -> needed for CBC, CFB and OFB mode
15 counter = counter object (CryptoPlus.Util.util.Counter)
16 -> only needed for CTR mode
17 -> use a seperate counter object for the cipher and decipher: the counter is updated directly, not a copy
18 see CTR example further on in the docstring
21 EXAMPLE (test vectors from NESSIE):
22 -----------------------------------
23 >>> import python_DES
24 >>> from binascii import hexlify, unhexlify
25 >>> cipher = python_DES.new(unhexlify('7CA110454A1A6E57'))
26 >>> ciphertext = cipher.encrypt(unhexlify('01A1D6D039776742'))
27 >>> hexlify(ciphertext)
28 '690f5b0d9a26939b'
29 >>> plaintext = cipher.decrypt(ciphertext)
30 >>> hexlify(plaintext)
31 '01a1d6d039776742'
32 """
33 return python_DES(key,mode,IV,counter)
35 class python_DES(BlockCipher):
36 def __init__(self,key,mode,IV,counter):
37 self.cipher = pyDes.des(key)
38 self.blocksize = self.cipher.block_size
39 BlockCipher.__init__(self,key,mode,IV,counter)
41 def _test():
42 import doctest
43 doctest.testmod()
45 if __name__ == "__main__":
46 _test()