-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathpython_Blowfish.py
More file actions
83 lines (66 loc) · 3.24 KB
/
python_Blowfish.py
File metadata and controls
83 lines (66 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from .blockcipher import *
from .pyblowfish import Blowfish
def new(key,mode=MODE_ECB,IV=None,counter=None,segment_size=None):
"""Create a new cipher object
Wrapper for pure python implementation pyblowfish.py
key = raw string containing the key
mode = Blowfish.MODE_ECB/CBC/CFB/OFB/CTR/CMAC, default is ECB
IV = IV as a raw string, default is "all zero" IV
-> only needed for CBC mode
counter = counter object (CryptoPlus.Util.util.Counter)
-> only needed for CTR mode
segment_size = amount of bits to use from the keystream in each chain part
-> supported values: multiple of 8 between 8 and the blocksize
of the cipher (only per byte access possible), default is 8
-> only needed for CFB mode
EXAMPLES:
**********
IMPORTING:
-----------
>>> import codecs
>>> from CryptoPlus.Cipher import python_Blowfish
EXAMPLE: (http://www.schneier.com/code/vectors.txt)
----------
>>> cipher = python_Blowfish.new(codecs.decode('0131D9619DC1376E', 'hex'))
>>> codecs.encode(cipher.encrypt(codecs.decode('5CD54CA83DEF57DA', 'hex')), 'hex')
b'b1b8cc0b250f09a0'
>>> codecs.encode(cipher.decrypt(codecs.decode(_, 'hex')), 'hex')
b'5cd54ca83def57da'
CBC, CFB, OFB EXAMPLE: http://www.schneier.com/code/vectors.txt
----------------------
>>> key = codecs.decode('0123456789ABCDEFF0E1D2C3B4A59687', 'hex')
>>> IV = codecs.decode('FEDCBA9876543210', 'hex')
>>> plaintext = codecs.decode('37363534333231204E6F77206973207468652074696D6520', 'hex')
>>> cipher = python_Blowfish.new(key,python_Blowfish.MODE_CBC,IV)
>>> ciphertext = cipher.encrypt(plaintext)
>>> codecs.encode(ciphertext, 'hex').upper()
b'6B77B4D63006DEE605B156E27403979358DEB9E7154616D9'
>>> key = codecs.decode('0123456789ABCDEFF0E1D2C3B4A59687', 'hex')
>>> iv = codecs.decode('FEDCBA9876543210', 'hex')
>>> plaintext = codecs.decode('37363534333231204E6F77206973207468652074696D6520666F722000', 'hex')
>>> cipher = python_Blowfish.new(key,python_Blowfish.MODE_CBC,iv)
>>> ciphertext = cipher.encrypt(plaintext)
>>> codecs.encode(ciphertext, 'hex').upper()
b'6B77B4D63006DEE605B156E27403979358DEB9E7154616D9'
>>> cipher = python_Blowfish.new(key,python_Blowfish.MODE_CFB,iv,segment_size=64)
>>> ciphertext = cipher.encrypt(plaintext)
>>> codecs.encode(ciphertext, 'hex').upper()
b'E73214A2822139CAF26ECF6D2EB9E76E3DA3DE04D1517200519D57A6C3'
>>> cipher = python_Blowfish.new(key,python_Blowfish.MODE_OFB,iv)
>>> ciphertext = cipher.encrypt(plaintext)
>>> codecs.encode(ciphertext, 'hex').upper()
b'E73214A2822139CA62B343CC5B65587310DD908D0C241B2263C2CF80DA'"""
return python_Blowfish(key,mode,IV,counter,segment_size)
class python_Blowfish(BlockCipher):
key_error_message = "Key should be between 8 and 56 bytes (64 <-> 448 bits)"
def __init__(self,key,mode,IV,counter,segment_size):
cipher_module = Blowfish
self.blocksize = 8
BlockCipher.__init__(self,key,mode,IV,counter,cipher_module,segment_size)
def keylen_valid(self,key):
return 8 <= len(key) <= 56
def _test():
import doctest
doctest.testmod()
if __name__ == "__main__":
_test()