Consistency fixes in key check in ciphers python_*
[python-cryptoplus.git] / src / CryptoPlus / Cipher / python_DES.py
blob5a704add0b3552217e3c25d584d4ad693f69f039
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 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
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)
37 class python_DES(BlockCipher):
38 def __init__(self,key,mode,IV,counter):
39 if len(key) <> 8:
40 raise ValueError("Key should be 64 bits")
41 cipher_module = pyDes.des
42 self.blocksize = 8
43 BlockCipher.__init__(self,key,mode,IV,counter,cipher_module)
45 def _test():
46 import doctest
47 doctest.testmod()
49 if __name__ == "__main__":
50 _test()