class TestRunningTime(unittest.TestCase): def setUp(self): self.n = 1 def test_ntimes(self): zero_key = bytearray(b'\x00' * Blowfish.keySize()) self.cipher = Blowfish(zero_key) for i in range(self.n): self.test_once() def test_once(self): zero_block = bytearray(b'\x00' * Blowfish.blockSize()) self.cipher.encrypt(zero_block)
class TestVectors(unittest.TestCase): def setUp(self): pass def test_all(self): for key, cleartext, ciphertext in [ ('0000000000000000', '0000000000000000', '4EF997456198DD78'), ('FFFFFFFFFFFFFFFF', 'FFFFFFFFFFFFFFFF', '51866FD5B85ECB8A'), ('3000000000000000', '1000000000000001', '7D856F9A613063F2'), ('1111111111111111', '1111111111111111', '2466DD878B963C9D'), ('0123456789ABCDEF', '1111111111111111', '61F9C3802281B096'), ('1111111111111111', '0123456789ABCDEF', '7D0CC630AFDA1EC7'), ('0000000000000000', '0000000000000000', '4EF997456198DD78'), ('FEDCBA9876543210', '0123456789ABCDEF', '0ACEAB0FC6A0A28D'), ('7CA110454A1A6E57', '01A1D6D039776742', '59C68245EB05282B'), ('0131D9619DC1376E', '5CD54CA83DEF57DA', 'B1B8CC0B250F09A0'), ('07A1133E4A0B2686', '0248D43806F67172', '1730E5778BEA1DA4'), ('3849674C2602319E', '51454B582DDF440A', 'A25E7856CF2651EB'), ('04B915BA43FEB5B6', '42FD443059577FA2', '353882B109CE8F1A'), ('0113B970FD34F2CE', '059B5E0851CF143A', '48F4D0884C379918'), ('0170F175468FB5E6', '0756D8E0774761D2', '432193B78951FC98'), ('43297FAD38E373FE', '762514B829BF486A', '13F04154D69D1AE5'), ('07A7137045DA2A16', '3BDD119049372802', '2EEDDA93FFD39C79'), ('04689104C2FD3B2F', '26955F6835AF609A', 'D887E0393C2DA6E3'), ('37D06BB516CB7546', '164D5E404F275232', '5F99D04F5B163969'), ('1F08260D1AC2465E', '6B056E18759F5CCA', '4A057A3B24D3977B'), ('584023641ABA6176', '004BD6EF09176062', '452031C1E4FADA8E'), ('025816164629B007', '480D39006EE762F2', '7555AE39F59B87BD'), ('49793EBC79B3258F', '437540C8698F3CFA', '53C55F9CB49FC019'), ('4FB05E1515AB73A7', '072D43A077075292', '7A8E7BFA937E89A3'), ('49E95D6D4CA229BF', '02FE55778117F12A', 'CF9C5D7A4986ADB5'), ('018310DC409B26D6', '1D9D5C5018F728C2', 'D1ABB290658BC778'), ('1C587F1C13924FEF', '305532286D6F295A', '55CB3774D13EF201'), ('0101010101010101', '0123456789ABCDEF', 'FA34EC4847B268B2'), ('1F1F1F1F0E0E0E0E', '0123456789ABCDEF', 'A790795108EA3CAE'), ('E0FEE0FEF1FEF1FE', '0123456789ABCDEF', 'C39E072D9FAC631D'), ('0000000000000000', 'FFFFFFFFFFFFFFFF', '014933E0CDAFF6E4'), ('FFFFFFFFFFFFFFFF', '0000000000000000', 'F21E9A77B71C49BC'), ('0123456789ABCDEF', '0000000000000000', '245946885754369A'), ('FEDCBA9876543210', 'FFFFFFFFFFFFFFFF', '6B5C5A9C5D9E0A5A')]: key = bytes.fromhex(key) cleartext = bytes.fromhex(cleartext) ciphertext = bytes.fromhex(ciphertext) self.test_vector(key, cleartext) # in-place encrypt print(hex(struct.unpack('>Q',cleartext)[0])) self.assertEqual(cleartext, ciphertext) def test_vector(self, key, cleartext): self.cipher = Blowfish(key) self.cipher.encrypt(cleartext)
def encryptFile(self, input_f): try: key = self.key delimiter = self.delimiter size = int(self.GetSize(input_f)) crypt_f = input_f + '.crypt' cipher = Blowfish(key) print '' decorate(' Encrypting ' + input_f + '...', 64, '-') with open(input_f, 'rb') as f1: with open(crypt_f, 'wb') as f2: for i in tqdm(range(size)): t = f1.read(1) u = cipher.encrypt(str( base64.b64encode(t) * 2)) + delimiter f2.write(u) f1.close() f2.close() self.cleanUp(input_f) except Exception as e: print e, 'exception caught while encrypting', input_f finally: decorate('Success', 64, '-')
def encryptFile(self, input_f): try: key = self.key delimiter = self.delimiter size=int(self.GetSize(input_f)) crypt_f=input_f+'.crypt' cipher = Blowfish(key) print '' decorate(' Encrypting ' + input_f + '...', 64, '-') with open(input_f,'rb') as f1: with open(crypt_f,'wb') as f2: for i in tqdm(range(size)): t= f1.read(1) u = cipher.encrypt(str(base64.b64encode(t)*2))+delimiter f2.write(u) f1.close() f2.close() self.cleanUp(input_f) except Exception as e: print e, 'exception caught while encrypting', input_f finally: decorate('Success', 64, '-')
class Crypto: def __init__(self, key): self.key = key self.cipher = Blowfish(self.key) def encrypt(self, data): return "".join([self.cipher.encrypt(pad(data[i:i + 8], 8)).encode('hex') for i in xrange(0, len(data), 8)]) def decrypt(self, data): return "".join([self.cipher.decrypt(pad(data[i:i + 16].decode('hex'), 8)) for i in xrange(0, len(data), 16)]).rstrip('\x08')
class BCipher: prefix = "CRYP=" def __init__(self, key=None): if not key: key = getattr(settings, "CIPHER_KEY", settings.SECRET_KEY) if len(key) < 8: raise Exception("Key length must be greater than 8") self.__cipher = Blowfish(key) def encrypt(self, text): padtext = self.__pad_text(text) res = [] for n in range(0, len(padtext), 8): part = padtext[n : n + 8] res.append(self.__cipher.encrypt(part)) ciphertext = "".join(res) return self.prefix + base64.b64encode(ciphertext) def decrypt(self, b64text): if not b64text.startswith(self.prefix): return b64text enctext = b64text[len(self.prefix) :] try: ciphertext = base64.b64decode(enctext) except TypeError: # text is not encrypted return enctext res = [] for n in range(0, len(ciphertext), 8): part = ciphertext[n : n + 8] res.append(self.__cipher.decrypt(part)) cleartext = "".join(res) return self.__depad_text(cleartext) # Blowfish cipher needs 8 byte blocks to work with def __pad_text(self, text): pad_bytes = 8 - (len(text) % 8) # try to deal with unicode strings asc_text = str(text) for i in range(pad_bytes - 1): asc_text += chr(randrange(0, 256)) # final padding byte; % by 8 to get the number of padding bytes bflag = randrange(6, 248) bflag -= bflag % 8 - pad_bytes asc_text += chr(bflag) return asc_text def __depad_text(self, text): pad_bytes = ord(text[-1]) % 8 if not pad_bytes: pad_bytes = 8 return text[:-pad_bytes]
def main(): pt = "Hi There!!!" print(pt) blowfish = Blowfish("dsadasdasda", pt) ct = blowfish.encrypt(pt) print(ct) StegEncrypt("tiger.png", ct, "enc_tiger.png") ct = StegDecrypt("enc_tiger.png") pt = blowfish.decrypt(ct) print(pt)
def main(mode, key, infile, outfile): cipher = Blowfish(bytearray.fromhex(key)) text = infile.read() size = Blowfish.blockSize() for i in range(0,len(text),size): block = bytearray(text[i:(i+size)]) if len(block)<size: for _ in range(size-len(block)): block.append(0) if mode==MODE_ENCRYPT: cipher.encrypt(block) elif mode==MODE_DECRYPT: cipher.decrypt(block) outfile.write(block) outfile.flush()
def encrypt(args, eurikey = None): import blowfish.Blowfish pkey = 'JE87z39322aiscpqpzx94KJ29SN400mndhqn7198zfgQQAZMKLP6478A' if eurikey: pkey = eurikey cipher = Blowfish(pkey) # create the URI al = [] for (k,v) in args.itervalues(): al.append( k + "=" + v ) arg_string = string.join(al,"&") return cipher.encrypt(arg_string)
def main(mode, key, infile, outfile): cipher = Blowfish(bytearray.fromhex(key)) text = infile.read() size = Blowfish.blockSize() for i in range(0, len(text), size): block = bytearray(text[i:(i + size)]) if len(block) < size: for _ in range(size - len(block)): block.append(0) if mode == MODE_ENCRYPT: cipher.encrypt(block) elif mode == MODE_DECRYPT: cipher.decrypt(block) outfile.write(block) outfile.flush()
# Usage of blowfish: # plaintext = "The quick brown fox jumped over the lazy dog." # blowfish = new Blowfish("key", plaintext) # # cyphertext = blowfish.encrypt(plaintext) # print(cyphertext) # plaintext = blowfish.decrypt(cyphertext) # print(plaintext) from blowfish import Blowfish pt = "Nedim" print('The plaintext is: {}'.format(pt)) blowfish = Blowfish("dsadasdasda", pt) ct = blowfish.encrypt(pt) print('The ciphertext is: {}'.format(ct)) pt = blowfish.decrypt(ct) print('The decrypted ciphertext is: {}'.format(pt))
def f_function(half_block, subkey): bf = Blowfish(subkey) result = bf.encrypt(half_block) return result