Java - Blowfish Encryption and decryption Example

Blowfish is an encryption method developed by Bruce Schneier in 1993 as an alternative to the DES encryption method. It is significantly faster than DES and provides good encryption speed, although no effective cryptanalysis technique has been found to date. It is one of the first secure block ciphers that is not protected by any patents and is therefore freely available for anyone to use. This is a symmetric block cipher algorithm.

  • Block size: 64 bits
  • Key size: variable size from 32 to 448 bits
  • number of subsections: 18 [P-array]
  • number of rounds: 16
  • number of substitution blocks: 4 [each with 512 records of 32 bits each]develop 

Example 1: Encryption and decryption using Blowfish

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

/**
* This program demonstrates how to encrypt/decrypt
* input using the Blowfish
* Cipher with the Java Cryptography.
*/
public class BlowfishDemo {

public String encrypt(String password, String key) throws
NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException, IllegalBlockSizeException,
BadPaddingException, UnsupportedEncodingException {
byte[] KeyData = key.getBytes();
SecretKeySpec KS = new SecretKeySpec(KeyData, "Blowfish");
Cipher cipher = Cipher.getInstance("Blowfish");
cipher.init(Cipher.ENCRYPT_MODE, KS);
String encryptedtext = Base64.getEncoder().
encodeToString(cipher.doFinal(password.getBytes("UTF-8")));
return encryptedtext;

}

public String decrypt(String encryptedtext, String key)
throws NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException, IllegalBlockSizeException,
BadPaddingException {
byte[] KeyData = key.getBytes();
SecretKeySpec KS = new SecretKeySpec(KeyData, "Blowfish");
byte[] ecryptedtexttobytes = Base64.getDecoder().
decode(encryptedtext);
Cipher cipher = Cipher.getInstance("Blowfish");
cipher.init(Cipher.DECRYPT_MODE, KS);
byte[] decrypted = cipher.doFinal(ecryptedtexttobytes);
String decryptedString =
new String(decrypted, Charset.forName("UTF-8"));
return decryptedString;

}

public static void main(String[] args) throws Exception {
final String password = "Knf@123";
final String key = "knowledgefactory";
System.out.println("Password: " + password);
BlowfishDemo obj = new BlowfishDemo();
String enc_output = obj.encrypt(password, key);
System.out.println("Encrypted text: " + enc_output);
String dec_output = obj.decrypt(enc_output, key);
System.out.println("Decrypted text: " + dec_output);
}
}

Output:

Password: Knf@123
Encrypted text: 4DTHqnctCuk=
Decrypted text: Knf@123


Example 2: File Encryption and decryption using Blowfish 

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class BlowfishFileEncryptionDemo {

private static final String ALGORITHM = "Blowfish";
private static String key= "knowledgefactory";
private static final String SAMPLE_FILE_PATH =
"/home/user/Desktop/cryptotest/Sample.txt";
private static final String ENCRYPTED_FILE_PATH =
"/home/user/Desktop/cryptotest/file.encrypted";
private static final String DECRYPTED_FILE_PATH =
"/home/user/Desktop/cryptotest/decryptedfile.txt";

public static void main(String[] args) {

File sampleFile = new File(SAMPLE_FILE_PATH);
File encryptedFile = new File(ENCRYPTED_FILE_PATH);
File decryptedFile = new File(DECRYPTED_FILE_PATH);

try {
BlowfishFileEncryptionDemo.encrypt(sampleFile, encryptedFile);
BlowfishFileEncryptionDemo.decrypt(encryptedFile, decryptedFile);
} catch (Exception e) {
e.printStackTrace();
}
}

public static void encrypt(File sampleFile, File outputFile)
throws Exception {
doCrypto(Cipher.ENCRYPT_MODE, sampleFile, outputFile);

}

public static void decrypt(File sampleFile, File outputFile)
throws Exception {
doCrypto(Cipher.DECRYPT_MODE, sampleFile, outputFile);

}

private static void doCrypto(int cipherMode, File sampleFile,
File outputFile) throws Exception {

Key secretKey = new SecretKeySpec(key.getBytes(), ALGORITHM);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(cipherMode, secretKey);

InputStream inputStream = new FileInputStream(sampleFile);
byte[] inputBytes = new byte[(int) sampleFile.length()];
inputStream.read(inputBytes);

byte[] outputBytes = cipher.doFinal(inputBytes);

OutputStream outputStream = new FileOutputStream(outputFile);
outputStream.write(outputBytes);

inputStream.close();
outputStream.close();

}
}


Output:


More related topics...

Spring Boot + Angular - CRUD examples:



Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Spring Boot + Mockito simple application with 100% code coverage

Top 5 Java ORM tools - 2024

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete