restructered padding
[python-cryptoplus.git] / src / CryptoPlus / Cipher / python_Blowfish.py
blob25b5cf30212d327818ed290a38b27aadf4e37885
1 from blockcipher import *
2 from pyblowfish import Blowfish
4 def new(key,mode=MODE_ECB,IV=None,counter=None):
5 """Create a new cipher object
7 Wrapper for pure python implementation pyblowfish.py
9 key = raw string containing the key
10 mode = Blowfish.MODE_ECB/CBC/CFB/OFB/CTR/CMAC, default is ECB
11 IV = IV as a raw string
12 -> only needed for CBC mode
13 counter = counter object (CryptoPlus.Util.util.Counter)
14 -> only needed for CTR mode
16 EXAMPLES:
17 **********
18 IMPORTING:
19 -----------
20 >>> from CryptoPlus.Cipher import python_Blowfish
22 EXAMPLE: (http://www.schneier.com/code/vectors.txt)
23 ----------
25 >>> cipher = python_Blowfish.new(('0131D9619DC1376E').decode('hex'))
26 >>> ( cipher.encrypt(('5CD54CA83DEF57DA').decode('hex')) ).encode('hex')
27 'b1b8cc0b250f09a0'
28 >>> ( cipher.decrypt((_).decode('hex')) ).encode('hex')
29 '5cd54ca83def57da'
31 CBC, CFB, OFB EXAMPLE: http://www.schneier.com/code/vectors.txt
32 ----------------------
33 >>> key = ('0123456789ABCDEFF0E1D2C3B4A59687').decode('hex')
34 >>> IV = ('FEDCBA9876543210').decode('hex')
35 >>> plaintext = ('37363534333231204E6F77206973207468652074696D6520').decode('hex')
36 >>> cipher = python_Blowfish.new(key,python_Blowfish.MODE_CBC,IV)
37 >>> ciphertext = cipher.encrypt(plaintext)
38 >>> (ciphertext).encode('hex').upper()
39 '6B77B4D63006DEE605B156E27403979358DEB9E7154616D9'
42 >>> key = '0123456789ABCDEFF0E1D2C3B4A59687'.decode('hex')
43 >>> iv = 'FEDCBA9876543210'.decode('hex')
44 >>> plaintext = '37363534333231204E6F77206973207468652074696D6520666F722000'.decode('hex')
46 >>> cipher = python_Blowfish.new(key,python_Blowfish.MODE_CBC,iv)
47 >>> ciphertext = cipher.encrypt(plaintext)
48 >>> (ciphertext).encode('hex').upper()
49 '6B77B4D63006DEE605B156E27403979358DEB9E7154616D9'
51 >>> cipher = python_Blowfish.new(key,python_Blowfish.MODE_CFB,iv)
52 >>> ciphertext = cipher.encrypt(plaintext)
53 >>> (ciphertext).encode('hex').upper()
54 'E73214A2822139CAF26ECF6D2EB9E76E3DA3DE04D1517200519D57A6C3'
56 >>> cipher = python_Blowfish.new(key,python_Blowfish.MODE_OFB,iv)
57 >>> ciphertext = cipher.encrypt(plaintext)
58 >>> (ciphertext).encode('hex').upper()
59 'E73214A2822139CA62B343CC5B65587310DD908D0C241B2263C2CF80DA'"""
60 return python_Blowfish(key,mode,IV,counter)
62 class python_Blowfish(BlockCipher):
63 key_error_message = "Key should be between 8 and 56 bytes (64 <-> 448 bits)"
65 def __init__(self,key,mode,IV,counter):
66 cipher_module = Blowfish
67 self.blocksize = 8
68 BlockCipher.__init__(self,key,mode,IV,counter,cipher_module)
70 def keylen_valid(self,key):
71 return 8 <= len(key) <= 56
73 def _test():
74 import doctest
75 doctest.testmod()
77 if __name__ == "__main__":
78 _test()