Example of the Blowfish encryption and decryption technique
To better understand how Blowfish works, let's walk through an example of encrypting and decrypting a message using a 128-bit key.
Encryption
1. Input: 64-bit plaintext block: "Hello, World!"
Key: 128-bit key: "0123456789ABCDEF"
2. Split the plaintext block into two 32-bit halves:
L0 = "Hell"
R0 = "o, W"
3. Perform 16 rounds of encryption:
For i = 1 to 16:
Li = Ri-1
Ri = Li-1 ⊕ F(Ri-1, Pi)
where F is the key-dependent function, and Pi is the ith 32-bit subkey from the P-array.
4. After the 16th round, swap the halves one last time:
L17 = R16
R17 = L16
5. Output: 64-bit ciphertext block: "0x7A65B8F03C9D5A1F"
Decryption
1. Input: 64-bit ciphertext block: "0x7A65B8F03C9D5A1F"
Key: 128-bit key: "0123456789ABCDEF"
2. Split the ciphertext block into two 32-bit halves:
L17 = "0x7A65B8F0"
R17 = "0x3C9D5A1F"
3. Perform 16 rounds of decryption
For i = 16 to 1:
Ri-1 = Li
Li-1 = Ri ⊕ F(Li, Pi)
4. After the 16th round, swap the halves one last time:
L0 = R0
R0 = L0
5. Output: 64-bit plaintext block: "Hello, World!"
Advantages of Blowfish
1. Strong security: Blowfish is known for its robust security, making it suitable for encrypting sensitive data. With a variable key length of up to 448 bits, it offers a high level of protection against brute-force attacks.
2. Fast encryption and decryption: Blowfish is designed to be fast, especially when implemented in hardware. It is often faster than other popular encryption algorithms like DES and AES, making it an attractive choice for applications that require high-speed encryption.
3. Royalty-free: Blowfish is unpatented and free for anyone to use without any royalties or restrictions. This makes it accessible to developers and organizations looking for a cost-effective encryption solution.
4. Widely supported: Blowfish is supported by many cryptographic libraries and software applications, making it easy to integrate into existing systems and frameworks.
5. Flexible key size: With a variable key size ranging from 32 to 448 bits, Blowfish allows users to balance security and performance based on their specific needs. Longer keys provide higher security, while shorter keys can be used for faster encryption in less sensitive scenarios.
6. No known successful attacks: To date, there have been no successful attacks against the full 16-round version of Blowfish. While there have been some theoretical weaknesses discovered, they do not compromise the security of the algorithm when used properly.
7. Compact implementation: Blowfish has a relatively small memory footprint, making it suitable for use in embedded systems and resource-constrained environments.
Disadvantages of Blowfish
1. 64-bit block size: Blowfish uses a 64-bit block size, which is considered small by modern standards. This can make it vulnerable to birthday attacks and collision attacks in certain scenarios, especially when large amounts of data are encrypted with the same key.
2. Weak keys: Blowfish has a class of weak keys that can make the encryption more susceptible to attacks. However, these weak keys are rare and can be easily avoided by using a strong key generation method.
3. Slow key setup: The key setup phase of Blowfish is relatively slow compared to other encryption algorithms. This is because the algorithm must generate a large number of subkeys and S-boxes based on the user-provided key. While this slow key setup contributes to the security of Blowfish, it can be a drawback in applications that require frequent key changes.
4. Not suitable for authentication: Blowfish is a block cipher and does not provide built-in authentication or integrity checking. If these features are required, additional mechanisms like message authentication codes (MACs) or digital signatures must be used in conjunction with Blowfish.
5. Superseded by newer algorithms: While Blowfish is still secure and widely used, it has been largely superseded by newer encryption algorithms like AES (Advanced Encryption Standard). AES offers better security, performance, and flexibility, making it the preferred choice for many modern applications.
6. Limited block size: The 64-bit block size of Blowfish can be a limitation in some scenarios. For example, when encrypting large files or streams of data, the algorithm must break the data into 64-bit blocks, which can be less efficient than using larger block sizes.
Uses of Blowfish
1. Secure communication: Blowfish can be used to encrypt sensitive data transmitted over networks, ensuring confidentiality and protecting against eavesdropping. It is commonly used in secure communication protocols like SSH (Secure Shell) and VPN (Virtual Private Network) solutions.
2. File and disk encryption: Blowfish can be employed to encrypt files and entire disk partitions, protecting sensitive data at rest. This is particularly useful for securing personal documents, financial records, or confidential business information stored on computers or storage devices.
3. Password management: Blowfish is often used in password management systems to securely store and encrypt user passwords. By encrypting passwords with Blowfish, organizations can protect user accounts from unauthorized access and prevent password leaks in the event of a data breach.
4. Secure backup and archiving: Blowfish can be utilized to encrypt backup files and archives before storing them on local devices or transmitting them to remote servers. This ensures that sensitive data remains protected even if the backup files are compromised or intercepted.
5. E-commerce transactions: Blowfish can be used to encrypt sensitive information exchanged during e-commerce transactions, such as credit card numbers and personal details. By securing this data, online merchants can protect their customers' privacy and prevent fraud.
6. Cloud storage: Blowfish can be employed to encrypt data before uploading it to cloud storage services. This adds an extra layer of security, ensuring that data remains confidential even if the cloud provider's systems are breached or the data is accessed by unauthorized parties.
7. Embedded systems security: Blowfish's compact implementation and low memory footprint make it suitable for use in embedded systems, such as IoT devices, smart appliances, and automotive systems. It can be used to encrypt data stored on these devices or transmitted between them, enhancing the overall security of the embedded system.
8. Gaming and software protection: Blowfish can be used to encrypt game assets, software libraries, or configuration files to prevent unauthorized modification, reverse engineering, or cheating. By encrypting these resources, developers can protect their intellectual property and maintain the integrity of their software.
Java program that demonstrates Blowfish encryption:
Now, let’s discuss a Java program that shows how to encrypt and decrypt data using the Blowfish algorithm:
Java
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
public class BlowfishEncryption {
private static final String ALGORITHM = "Blowfish";
public static void main(String[] args) throws Exception {
String plaintext = "Hello, Blowfish!";
// Generate a secret key
SecretKey secretKey = generateKey();
// Encrypt the plaintext
String ciphertext = encrypt(plaintext, secretKey);
System.out.println("Encrypted: " + ciphertext);
// Decrypt the ciphertext
String decryptedText = decrypt(ciphertext, secretKey);
System.out.println("Decrypted: " + decryptedText);
}
public static SecretKey generateKey() throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);
return keyGenerator.generateKey();
}
public static String encrypt(String plaintext, SecretKey secretKey) throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encryptedBytes = cipher.doFinal(plaintext.getBytes());
return Base64.getEncoder().encodeToString(encryptedBytes);
}
public static String decrypt(String ciphertext, SecretKey secretKey) throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] ciphertextBytes = Base64.getDecoder().decode(ciphertext);
byte[] decryptedBytes = cipher.doFinal(ciphertextBytes);
return new String(decryptedBytes);
}
}

You can also try this code with Online Java Compiler
Run Code
Output
Encrypted: BKXnwP+mURhT6H1iHXL/x1my5ZHhgUeo
Decrypted: Hello, Blowfish!
In this code:
1. We define the Blowfish algorithm as the encryption algorithm using the constant `ALGORITHM`.
2. In the `main` method, we specify the plaintext to be encrypted.
3. We generate a secret key using the `generateKey` method. This method uses the `KeyGenerator` class to generate a random secret key for the Blowfish algorithm.
4. We encrypt the plaintext using the `encrypt` method, passing the plaintext and the generated secret key as parameters. The method initializes a `Cipher` object with the Blowfish algorithm and the secret key, encrypts the plaintext bytes, and returns the encrypted data as a Base64-encoded string.
5. We decrypt the ciphertext using the `decrypt` method, passing the ciphertext and the same secret key as parameters. The method initializes a `Cipher` object with the Blowfish algorithm and the secret key, decodes the Base64-encoded ciphertext, decrypts the ciphertext bytes, and returns the decrypted plaintext as a string.
6. Finally, we print the encrypted ciphertext and the decrypted plaintext to verify that the encryption and decryption process worked correctly.
Java program that shows how to decode data:
Let’s see an another java program that shows how to decrypt data that was previously encrypted using the Blowfish algorithm:
Java
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
public class BlowfishDecryption {
private static final String ALGORITHM = "Blowfish";
public static void main(String[] args) throws Exception {
String ciphertext = "YvE8VcGWQZdDXl5CCX1fCg==";
String keyString = "MySecretKey12345";
// Create a SecretKeySpec from the key string
SecretKeySpec secretKey = new SecretKeySpec(keyString.getBytes(), ALGORITHM);
// Decrypt the ciphertext
String decryptedText = decrypt(ciphertext, secretKey);
System.out.println("Decrypted: " + decryptedText);
}
public static String decrypt(String ciphertext, SecretKeySpec secretKey) throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] ciphertextBytes = Base64.getDecoder().decode(ciphertext);
byte[] decryptedBytes = cipher.doFinal(ciphertextBytes);
return new String(decryptedBytes);
}
}

You can also try this code with Online Java Compiler
Run Code
Output
Decrypted: HelloBlowfish
In this code :
1. We define the Blowfish algorithm as the decryption algorithm using the constant `ALGORITHM`.
2. In the `main` method, we specify the ciphertext that we want to decrypt. This ciphertext is assumed to have been previously encrypted using the Blowfish algorithm.
3. We create a `SecretKeySpec` object from a key string. In this example, we use a hardcoded key string "MySecretKey12345". In a real-world scenario, the key should be kept secure and not hardcoded in the program.
4. We decrypt the ciphertext using the `decrypt` method, passing the ciphertext and the `SecretKeySpec` object as parameters. The method initializes a `Cipher` object with the Blowfish algorithm and the secret key, decodes the Base64-encoded ciphertext, decrypts the ciphertext bytes, and returns the decrypted plaintext as a string.
5. Finally, we print the decrypted plaintext to verify that the decryption process worked correctly.
Frequently Asked Questions
Is Blowfish still considered secure?
Yes, Blowfish is still considered secure when used correctly. However, newer algorithms like AES are often preferred for modern applications.
What is the maximum key size supported by Blowfish?
Blowfish supports a variable key size ranging from 32 to 448 bits, providing flexibility in balancing security and performance.
Can Blowfish be used for both encryption and decryption?
Yes, Blowfish is a symmetric-key algorithm, which means the same key is used for both encryption and decryption.
Conclusion
In this article, we have discussed the Blowfish algorithm, a symmetric key block cipher that is known for its strong security and fast performance. We explained its encryption and decryption techniques and highlighted its benefits like variable key length, and royalty-free usage. We also talked about its disadvantages, like the small block size and slower key setup compared to other algorithms. We showed the implementation of this method with the help of Java code examples. We also discussed the various uses of the Blowfish algorithm which shows why it’s such an important topic to learn and understand.
You can also check out our other blogs on Code360.