No Result
View All Result
DevRescue
  • Home
  • Python
  • Lists
  • Movies
  • Finance
  • Opinion
  • About
  • Contact Us
  • Home
  • Python
  • Lists
  • Movies
  • Finance
  • Opinion
  • About
  • Contact Us
DevRescue
Home Blog Python

Python Blowfish Encryption Example

by Khaleel O.
January 14, 2024
in Python
Reading Time: 4 mins read
A A
python blowfish encryption example

Let’s do a Python Blowfish Encryption example. Let’s go! 🔥 🔥

Blowfish is a symmetric-key block cipher algorithm designed for fast encryption and decryption of data. It’s particularly notable for its simplicity and speed. In Python, Blowfish encryption can be implemented using the cryptography library, which provides a secure and user-friendly way to handle cryptographic operations. You can install the cryptography library using the pip install cryptography or pip3 install cryptography command at the terminal. Once you do that, you’re ready to rock!

Here’s a basic example of Blowfish encryption in Python, followed by a line-by-line explanation:

from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import padding
from os import urandom

# Key and Initialization Vector generation
key = urandom(16)  # Generate a random 16-byte key
iv = urandom(8)    # Generate a random 8-byte IV (Initialization Vector)

# Creating a Blowfish cipher object
cipher = Cipher(algorithms.Blowfish(key), modes.CBC(iv), backend=default_backend())

# Prepare data for encryption (with padding)
data = b"Secret Message"
padder = padding.PKCS7(64).padder()  # 64 bit (8 byte) padding for Blowfish
padded_data = padder.update(data) + padder.finalize()

# Encrypting data
encryptor = cipher.encryptor()
encrypted_data = encryptor.update(padded_data) + encryptor.finalize()

print(f"ENCRYPTED DATA IS: {encrypted_data}")

# Decrypting data
decryptor = cipher.decryptor()
decrypted_padded_data = decryptor.update(encrypted_data) + decryptor.finalize()

# Remove padding after decryption
unpadder = padding.PKCS7(64).unpadder()
decrypted_data = unpadder.update(decrypted_padded_data) + unpadder.finalize()

print(f"DECRYPTED DATA IS: {decrypted_data}")

#output
#ENCRYPTED DATA IS: b'\xc4\xf6\xad\xa3n\xc6\xc3W\xb9\xcb,=\xf1\xd9\xaa\xa6'
#DECRYPTED DATA IS: b'Secret Message'

Let’s explain what’s happening here:

  1. Import Required Modules:
    • Import Cipher, algorithms, modes from cryptography.hazmat.primitives.ciphers for cryptographic operations.
    • Import default_backend from cryptography.hazmat.backends for cryptographic backend support.
    • Import padding from cryptography.hazmat.primitives for data padding.
    • Import urandom from os for generating random bytes (keys and IVs).
  2. Key and IV Generation:
    • key = urandom(16) generates a random 16-byte key.
    • iv = urandom(8) generates a random 8-byte Initialization Vector.
  3. Create Blowfish Cipher Object:
    • cipher = Cipher(algorithms.Blowfish(key), modes.CBC(iv), backend=default_backend()) creates a Blowfish cipher object in Cipher Block Chaining (CBC) mode.
  4. Prepare Data for Encryption (With Padding):
    • data = b"Secret Message" sets the plaintext message.
    • padder = padding.PKCS7(64).padder() creates a padding object for 64-bit (8-byte) blocks.
    • padded_data = padder.update(data) + padder.finalize() pads the plaintext to a multiple of the block size.
  5. Encrypting Data:
    • encryptor = cipher.encryptor() creates an encryptor instance from the cipher.
    • encrypted_data = encryptor.update(padded_data) + encryptor.finalize() encrypts the padded data.
  6. Decrypting Data:
    • decryptor = cipher.decryptor() creates a decryptor instance from the cipher.
    • decrypted_padded_data = decryptor.update(encrypted_data) + decryptor.finalize() decrypts the data, still containing padding.
  7. Remove Padding After Decryption:
    • unpadder = padding.PKCS7(64).unpadder() creates an unpadder for 64-bit blocks.
    • decrypted_data = unpadder.update(decrypted_padded_data) + unpadder.finalize() removes the padding from decrypted data, yielding the original plaintext.

That does it for our Python Blowfish Encryption Example. Happy coding!👌🏻👌🏻👌🏻

Tags: blowfishencryptionpythonsecurity
Previous Post

Python Deque Methods

Next Post

Python Slice String List Tuple

Khaleel O.

Khaleel O.

I love to share, educate and help developers. I have 14+ years experience in IT. Currently transitioning from Systems Administration to DevOps. Avid reader, intellectual and dreamer. Enter Freely, Go safely, And leave something of the happiness you bring.

Related Posts

Python

Python Fibonacci Recursive Solution

by Khaleel O.
January 16, 2024
0
0

Let's do a Python Fibonacci Recursive Solution. Let's go! 🔥🔥🔥 The Fibonacci sequence is a series of numbers in which...

Read moreDetails
Python

Python Slice String List Tuple

by Khaleel O.
January 16, 2024
0
0

Let's do a Python Slice string list tuple how-to tutorial. Let's go! 🔥🔥🔥 In Python, a slice is a feature...

Read moreDetails
Python

Python Deque Methods

by Khaleel O.
January 14, 2024
0
0

In this post we'll list Python Deque Methods. Ready? Let's go! 🔥🔥🔥 A deque (double-ended queue) in Python is a...

Read moreDetails
Python

Python Ternary List Comprehension

by Khaleel O.
January 13, 2024
0
0

python ternary in list comprehension Let's investigate Python ternary list comprehension with some code examples. 🔥🔥 The ternary operator in...

Read moreDetails

DevRescue © 2021 All Rights Reserved. Privacy Policy. Cookie Policy

Manage your privacy

To provide the best experiences, we and our partners use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us and our partners to process personal data such as browsing behavior or unique IDs on this site and show (non-) personalized ads. Not consenting or withdrawing consent, may adversely affect certain features and functions.

Click below to consent to the above or make granular choices. Your choices will be applied to this site only. You can change your settings at any time, including withdrawing your consent, by using the toggles on the Cookie Policy, or by clicking on the manage consent button at the bottom of the screen.

Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Statistics

Marketing

Features
Always active

Always active
Manage options Manage services Manage {vendor_count} vendors Read more about these purposes
Manage options
{title} {title} {title}
Manage your privacy
To provide the best experiences, DevRescue.com will use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Statistics

Marketing

Features
Always active

Always active
Manage options Manage services Manage {vendor_count} vendors Read more about these purposes
Manage options
{title} {title} {title}
No Result
View All Result
  • Home
  • Python
  • Lists
  • Movies
  • Finance
  • Opinion
  • About
  • Contact Us

DevRescue © 2022 All Rights Reserved Privacy Policy