default "segment size" now set in blockcipher.py instead of wrappers
[python-cryptoplus.git] / src / CryptoPlus / Cipher / python_DES.py
blobbe5b3273988bd75c3caea98105de98d34003e543
1 from blockcipher import *
2 import pyDes
4 def new(key,mode=MODE_ECB,IV=None,counter=None,segment_size=None):
5 """Create a new cipher object
7 wrapper for pure python implementation pyDes.py
9 key = raw string containing the key
10 mode = python_DES.MODE_ECB/CBC/CFB/OFB/CTR/CMAC, default is ECB
11 -> for every mode, except ECB and CTR, it is important to construct a seperate cipher for encryption and decryption
12 IV = IV as a raw string, default is "all zero" IV
13 -> needed for CBC, CFB and OFB mode
14 counter = counter object (CryptoPlus.Util.util.Counter)
15 -> only needed for CTR mode
16 -> use a seperate counter object for the cipher and decipher: the counter is updated directly, not a copy
17 see CTR example further on in the docstring
19 EXAMPLES:
20 **********
21 IMPORTING:
22 -----------
23 >>> from CryptoPlus.Cipher import python_DES
25 EXAMPLE (test vectors from NESSIE):
26 -----------------------------------
27 >>> cipher = python_DES.new(('7CA110454A1A6E57').decode('hex'))
28 >>> ciphertext = cipher.encrypt(('01A1D6D039776742').decode('hex'))
29 >>> (ciphertext).encode('hex')
30 '690f5b0d9a26939b'
31 >>> plaintext = cipher.decrypt(ciphertext)
32 >>> (plaintext).encode('hex')
33 '01a1d6d039776742'
34 """
35 return python_DES(key,mode,IV,counter,segment_size)
37 class python_DES(BlockCipher):
38 key_error_message = ("Key should be 64 bits")
40 def __init__(self,key,mode,IV,counter,segment_size):
41 cipher_module = pyDes.des
42 self.blocksize = 8
43 BlockCipher.__init__(self,key,mode,IV,counter,cipher_module,segment_size)
45 def keylen_valid(self,key):
46 return len(key) == 8
48 def _test():
49 import doctest
50 doctest.testmod()
52 if __name__ == "__main__":
53 _test()