Improve English
[python-cryptoplus.git] / src / CryptoPlus / Cipher / python_DES.py
blobea6313388934e685754ec5a7ebf3da1b98dda53d
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
18 segment_size = amount of bits to use from the keystream in each chain part
19 -> supported values: multiple of 8 between 8 and the blocksize
20 of the cipher (only per byte access possible), default is 8
21 -> only needed for CFB mode
23 EXAMPLES:
24 **********
25 IMPORTING:
26 -----------
27 >>> from CryptoPlus.Cipher import python_DES
29 EXAMPLE (test vectors from NESSIE):
30 -----------------------------------
31 >>> cipher = python_DES.new(('7CA110454A1A6E57').decode('hex'))
32 >>> ciphertext = cipher.encrypt(('01A1D6D039776742').decode('hex'))
33 >>> (ciphertext).encode('hex')
34 '690f5b0d9a26939b'
35 >>> plaintext = cipher.decrypt(ciphertext)
36 >>> (plaintext).encode('hex')
37 '01a1d6d039776742'
38 """
39 return python_DES(key,mode,IV,counter,segment_size)
41 class python_DES(BlockCipher):
42 key_error_message = ("Key should be 64 bits")
44 def __init__(self,key,mode,IV,counter,segment_size):
45 cipher_module = pyDes.des
46 self.blocksize = 8
47 BlockCipher.__init__(self,key,mode,IV,counter,cipher_module,segment_size)
49 def keylen_valid(self,key):
50 return len(key) == 8
52 def _test():
53 import doctest
54 doctest.testmod()
56 if __name__ == "__main__":
57 _test()