mode constants are only in blockcipher
[python-cryptoplus.git] / src / Cipher / Blowfish.py
blob6ffa417d165d191f55248c6bb7fa3d04c2b3601b
1 from blockcipher import *
2 import Crypto.Cipher.Blowfish
4 def new(key,mode=MODE_ECB,IV=None,counter=None):
5 """Create a new cipher object
7 Blowfish using pycrypto for algo and pycryptoplus for ciphermode
9 new(key,mode=MODE_ECB,IV=None,counter=None):
10 key = raw string containing the key
11 mode = Blowfish.MODE_ECB/CBC/CFB/OFB/CTR/XTS/CMAC, default is ECB
12 IV = IV as a raw string
13 -> only needed for CBC mode
14 counter = counter object (CryptoPlus.Util.util.Counter)
15 -> only needed for CTR mode
17 ECB EXAMPLE: http://www.schneier.com/code/vectors.txt
18 -------------
19 >>> import Blowfish
20 >>> from binascii import hexlify, unhexlify
21 >>> cipher = Blowfish.new(unhexlify('0131D9619DC1376E'))
22 >>> hexlify( cipher.encrypt(unhexlify('5CD54CA83DEF57DA')) )
23 'b1b8cc0b250f09a0'
24 >>> hexlify( cipher.decrypt(unhexlify(_)) )
25 '5cd54ca83def57da'
27 CBC, CFB, OFB EXAMPLE: http://www.schneier.com/code/vectors.txt
28 ----------------------
29 >>> from binascii import hexlify,unhexlify
30 >>> key = unhexlify('0123456789ABCDEFF0E1D2C3B4A59687')
31 >>> IV = unhexlify('FEDCBA9876543210')
32 >>> plaintext = unhexlify('37363534333231204E6F77206973207468652074696D6520')
33 >>> cipher = Blowfish.new(key,Blowfish.MODE_CBC,IV)
34 >>> ciphertext = cipher.encrypt(plaintext)
35 >>> hexlify(ciphertext).upper()
36 '6B77B4D63006DEE605B156E27403979358DEB9E7154616D9'
39 >>> key = '0123456789ABCDEFF0E1D2C3B4A59687'.decode('hex')
40 >>> iv = 'FEDCBA9876543210'.decode('hex')
41 >>> plaintext = '37363534333231204E6F77206973207468652074696D6520666F722000'.decode('hex')
43 >>> cipher = Blowfish.new(key,Blowfish.MODE_CBC,iv)
44 >>> ciphertext = cipher.encrypt(plaintext)
45 >>> hexlify(ciphertext).upper()
46 '6B77B4D63006DEE605B156E27403979358DEB9E7154616D9'
48 >>> cipher = Blowfish.new(key,Blowfish.MODE_CFB,iv)
49 >>> ciphertext = cipher.encrypt(plaintext)
50 >>> hexlify(ciphertext).upper()
51 'E73214A2822139CAF26ECF6D2EB9E76E3DA3DE04D1517200519D57A6C3'
53 >>> cipher = Blowfish.new(key,Blowfish.MODE_OFB,iv)
54 >>> ciphertext = cipher.encrypt(plaintext)
55 >>> hexlify(ciphertext).upper()
56 'E73214A2822139CA62B343CC5B65587310DD908D0C241B2263C2CF80DA'
57 """
58 return Blowfish(key,mode,IV,counter)
60 class Blowfish(BlockCipher):
61 def __init__(self,key,mode,IV,counter):
62 self.cipher = Crypto.Cipher.Blowfish.new(key)
63 self.blocksize = Crypto.Cipher.Blowfish.block_size
64 BlockCipher.__init__(self,key,mode,IV,counter)
66 def _test():
67 import doctest
68 doctest.testmod()
70 if __name__ == "__main__":
71 _test()