def decrypt_file(fn, offset=0): key = ''.join(['\xDE', '\x72', '\xBE', '\xA0', '\xDE', '\x04', '\xBE', '\xB1', '\xDE', '\xFE', '\xBE', '\xEF', '\xDE', '\xAD', '\xBE', '\xEF']) bc = 0 pb = None from blowfish import Blowfish from binascii import b2a_hex bf = Blowfish(key) printmessage("Decrypting from offset {}".format(offset)) of = fn + ".tmp" with open(fn, 'rb') as f: f.seek(offset) with open(of, 'wb') as out: while True: b = f.read(8) if not b: break if len(b) < 8: b += '\x00' * (8 - len(b)) # pad for correct blocksize if bc > 0: db = bf.decrypt(b) if pb: db = ''.join([chr(int(b2a_hex(a), 16) ^ int(b2a_hex(b), 16)) for a, b in zip(db, pb)]) pb = db out.write(db) bc += 1 return of return None
def decrypt_file(fn, offset=0): bc = 0 pb = None bf = Blowfish(settings.BLOWFISH_KEY) log.info("Decrypting from offset {}".format(offset)) of = fn + ".tmp" with open(fn, 'rb') as f: f.seek(offset) with open(of, 'wb') as out: while True: b = f.read(8) if not b: break if len(b) < 8: b += '\x00' * (8 - len(b)) # pad for correct blocksize if bc > 0: db = bf.decrypt(b) if pb: db = ''.join([chr(int(b2a_hex(a), 16) ^ int(b2a_hex(b), 16)) for a, b in zip(db, pb)]) pb = db out.write(db) bc += 1 return of return None
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 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 decryptFile(self, crypt_f): key = self.key delimiter = self.delimiter c_file = crypt_f + '.crypt' if os.path.isfile(c_file): try: cipher = Blowfish(key) decorate(' Decrypting ' + c_file + '...', 64, '-') with open(c_file, 'rb') as f1: with open(crypt_f, 'wb') as f2: dt = f1.read().split(delimiter) tot = len(dt) - 1 for i in tqdm(range(tot)): f2.write( (base64.b64decode(cipher.decrypt(dt[i])[4:]))) f1.close() f2.close() decorate('Success', 64, '-') except Exception as e: if str(e) == 'Incorrect padding': print 'ACCESS DENIED' self.retryDecrypting(crypt_f) else: print e, 'exception caught while decrypting', crypt_f elif os.path.isfile(crypt_f): print 'encrypting keychain with a new key...' new_pass = raw_input('enter new pass --->>') if isinstance(new_pass, str): try: self.key = new_pass self.encryptFile(crypt_f) self.retryDecrypting(crypt_f) except Exception as e: print e
def decryptFile(self, crypt_f): key = self.key delimiter = self.delimiter c_file = crypt_f + '.crypt' if os.path.isfile(c_file): try: cipher = Blowfish(key) decorate(' Decrypting ' + c_file + '...', 64, '-') with open(c_file, 'rb') as f1: with open(crypt_f, 'wb') as f2: dt = f1.read().split(delimiter) tot = len(dt)-1 for i in tqdm(range(tot)): f2.write((base64.b64decode(cipher.decrypt(dt[i])[4:]))) f1.close() f2.close() decorate('Success', 64, '-') except Exception as e: if str(e) == 'Incorrect padding': print 'ACCESS DENIED' self.retryDecrypting(crypt_f) else: print e, 'exception caught while decrypting', crypt_f elif os.path.isfile(crypt_f): print 'encrypting keychain with a new key...' new_pass = raw_input('enter new pass --->>') if isinstance(new_pass, str): try: self.key = new_pass self.encryptFile(crypt_f) self.retryDecrypting(crypt_f) except Exception as e: print e
# 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))