Package: blowfish
This module implements the Blowfish cipher using only Python (3.4+).Blowfish is a block cipher that can be used for symmetric-key encryption. It
has a 8-byte block size and supports a variable-length key, from 4 to 56 bytes.
It’s fast, free and has been analyzed considerably. It was designed by Bruce
Schneier and more details about it can be found at
<https://www.schneier.com/blowfish.html>.DependenciesFeaturesInstallationdistutilspipDevelopmentTestsBugsUsageBlockElectronic Codebook Mode (ECB)Electronic Codebook Mode with Cipher Text Stealing (ECB-CTS)Cipher-Block Chaining Mode (CBC)Cipher-Block Chaining with Ciphertext Stealing (CBC-CTS)Propagating Cipher-Block Chaining Mode (PCBC)Cipher Feedback Mode (CFB)Output Feedback Mode (OFB)Counter Mode (CTR)DependenciesPython 3.4+FeaturesFast (well, as fast you can possibly go using only Python 3.4+)Efficient; generators/iterators are used liberally to reduce memory usageElectronic Codebook (ECB) modeElectronic Codebook with Ciphertext Stealing (ECB-CTS) modeCipher-Block Chaining (CBC) modeCipher-Block Chaining with Ciphertext Stealing (CBC-CTS) modePropagating Cipher-Block Chaining (PCBC) modeCipher Feedback (CFB) modeOutput Feedback (OFB) modeCounter (CTR) modeInstallationIf you just need a Blowfish cipher in your Python project, feel free to
manually copyblowfish.pyto your package directory (license permitting).distutilsTo install the module to your Python distribution, use the includeddistutilsscript:$ python setup.py installpipStable versions can be installed frompypiusingpip:$ pip install blowfishpipcan also install the latest development version directly fromgit:$ pip install 'git+https://github.com/jashandeep-sohi/python-blowfish.git'DevelopmentWant to add a mode of operation? Speed up encryption?Make your changes to a clone of the repository athttps://github.com/jashandeep-sohi/python-blowfishand send me a pull request.TestsTests are written using the Pythonunittestframework. All tests are in thetest.pyfile and can be run using:$ python -m unittest test.pyBugsAre you having problems? Please let me know athttps://github.com/jashandeep-sohi/python-blowfish/issuesUsageWarningCryptography is complex, so please don’t use this module in anythingcriticalwithout understanding what you are doing and checking the source
code to make sure it is doing what you want it to.NoteThis is just a quick overview on how to use the module. For detailed
documentation please see thedocstringsin the module.First create aCipherobject with akey.importblowfishcipher=blowfish.Cipher(b"Key must be between 4 and 56 bytes long.")By default this initializes a Blowfish cipher that will interpret bytes using
the big-endian byte order. Should the need arrise to use the little-endian byte
order, provide"little"as the second argument.cipher_little=blowfish.Cipher(b"my key",byte_order="little")BlockTo encrypt or decrypt a block of data (8 bytes), use theencrypt_blockordecrypt_blockmethods of theCipherobject.fromosimporturandomblock=urandom(8)ciphertext=cipher.encrypt_block(block)plaintext=cipher.decrypt_block(ciphertext)assertblock==plaintextAs these methods can only operate on 8 bytes of data, they’re of little
practical use. Instead, use one of the implemented modes of operation.Electronic Codebook Mode (ECB)To encrypt or decrypt data in ECB mode, useencrypt_ecbordecrypt_ecbmethods of theCipherobject. ECB mode can only operate on data that is a
multiple of the block-size in length.data=urandom(10*8)# data to encryptdata_encrypted=b"".join(cipher.encrypt_ecb(data))data_decrypted=b"".join(cipher.decrypt_ecb(data_encrypted)assertdata==data_decryptedElectronic Codebook Mode with Cipher Text Stealing (ECB-CTS)To encrypt or decrypt data in ECB-CTS mode, useencrypt_ecb_ctsordecrypt_ebc_ctsmethods of theCipherobject. ECB-CTS mode can operate
on data of any length greater than 8 bytes.data=urandom(10*8+5)# data to encryptdata_encrypted=b"".join(cipher.encrypt_ecb_cts(data))data_decrypted=b"".join(cipher.decrypt_ecb_cts(data_encrypted))assertdata==data_decryptedCipher-Block Chaining Mode (CBC)To encrypt or decrypt data in CBC mode, useencrypt_cbcordecrypt_cbcmethods of theCipherobject. CBC mode can only operate on data that is a
multiple of the block-size in length.data=urandom(10*8)# data to encryptiv=urandom(8)# initialization vectordata_encrypted=b"".join(cipher.encrypt_cbc(data,iv))data_decrypted=b"".join(cipher.decrypt_cbc(data_encrypted,iv))assertdata==data_decryptedCipher-Block Chaining with Ciphertext Stealing (CBC-CTS)To encrypt or decrypt data in CBC-CTS mode, useencrypt_cbc_ctsordecrypt_cbc_ctsmethods of theCipherobject. CBC-CTS mode can operate
on data of any length greater than 8 bytes.data=urandom(10*8+6)# data to encryptiv=urandom(8)# initialization vectordata_encrypted=b"".join(cipher.encrypt_cbc_cts(data,iv))data_decrypted=b"".join(cipher.decrypt_cbc_cts(data_encrypted,iv))assertdata==data_decryptedPropagating Cipher-Block Chaining Mode (PCBC)To encrypt or decrypt data in PCBC mode, useencrypt_pcbcordecrypt_pcbcmethods of theCipherobject. PCBC mode can only operate on data that is a
multiple of the block-size in length.data=urandom(10*8)# data to encryptiv=urandom(8)# initialization vectordata_encrypted=b"".join(cipher.encrypt_pcbc(data,iv))data_decrypted=b"".join(cipher.decrypt_pcbc(data_encrypted,iv))assertdata==data_decryptedCipher Feedback Mode (CFB)To encrypt or decrypt data in CFB mode, useencrypt_cfbordecrypt_cfbmethods of theCipherobject. CFB mode can operate on data of any length.data=urandom(10*8+7)# data to encryptiv=urandom(8)# initialization vectordata_encrypted=b"".join(cipher.encrypt_cfb(data,iv))data_decrypted=b"".join(cipher.decrypt_cfb(data_encrypted,iv))assertdata==data_decryptedOutput Feedback Mode (OFB)To encrypt or decrypt data in OFB mode, useencrypt_ofbordecrypt_ofbmethods of theCipherobject. OFB mode can operate on data of any length.data=urandom(10*8+1)# data to encryptiv=urandom(8)# initialization vectordata_encrypted=b"".join(cipher.encrypt_ofb(data,iv))data_decrypted=b"".join(cipher.decrypt_ofb(data_encrypted,iv))assertdata==data_decryptedCounter Mode (CTR)To encrypt or decrypt data in CTR mode, useencrypt_ctrordecrypt_ctrmethods of theCipherobject. CTR mode can operate on data of any length.
Although you can use anycounteryou want, a simple increment by one counter
is secure and the most popular. So for convenience sake a simple increment by
one counter is implemented by theblowfish.ctr_counterfunction. However,
you should implement your own for optimization purposes.fromoperatorimportxordata=urandom(10*8+2)# data to encrypt# increment by one countersnonce=int.from_bytes(urandom(8),"big")enc_counter=blowfish.ctr_counter(nonce,f=xor)dec_counter=blowfish.ctr_counter(nonce,f=xor)data_encrypted=b"".join(cipher.encrypt_ctr(data,enc_counter))data_decrypted=b"".join(cipher.decrypt_ctr(data_encrypted,dec_counter))assertdata==data_decrypted