Encrypting and Decrypting Images Using BlowFish

Blowfish is a symmetric-key block cipher, designed in 1993 by Bruce Schneier and included in a large number of cipher suites and encryption products.
For more information, refer to http://en.wikipedia.org/wiki/Blowfish_(cipher)

To Encrypt and Desrypt Images , follow following steps

Encrypting and Decrypting Images Using BlowFish :
1. Create SecretKey which will be used for Encryption
SecretKey secretKey = new SecretKeySpec(password.getBytes(), "Blowfish");

2. Create Cipher Instance
Cipher cipher = Cipher.getInstance("Blowfish");

3. To Encrypt, Initialize Cipher using secretKey
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
 then read input Image and create new encrypted file using cipher.update and cipher.doFinal as shown below

4. To Decrypt, Initialzie Cipher  sugin previously used secretKey
cipher.init(Cipher.DECRYPT_MODE, secretKey);

 then read input encrypted Image and create new decrypted file using cipher.update and cipher.doFinal as shown below

Encrypting and Decrypting Images using Java and BlowFish Cipher

package com.anuj.algorithms;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

/**
 * 
 * @author ANPATEL
 *
 */
public class EncryptDecryptImages {

 KeyGenerator keyGenerator = null;
 SecretKey secretKey = null;
 Cipher cipher = null;
 String password = "Anuj";
 String password1 = "Anuj1";

 public EncryptDecryptImages() {
  try {
   //keyGenerator = KeyGenerator.getInstance("Blowfish");
   //secretKey = keyGenerator.generateKey();
   secretKey = new SecretKeySpec(password.getBytes(), "Blowfish");
   cipher = Cipher.getInstance("Blowfish");

  } catch (NoSuchPaddingException e) {
   e.printStackTrace();
  } catch (NoSuchAlgorithmException e) {
   e.printStackTrace();
  }
 }

 /**
  * 
  * @param srcFile
  * @param destFile
  */
 private void encrypt(String srcFile, String destFile) {
  File fileTobeCrypted = new File(srcFile);
  File encryptedFile = new File(destFile);
  InputStream inputStream = null;
  OutputStream outputStream = null;
  try {
   System.out.println("Encryption started..");
   
   cipher.init(Cipher.ENCRYPT_MODE, secretKey);
   inputStream = new FileInputStream(fileTobeCrypted);
   outputStream = new FileOutputStream(encryptedFile);

   byte[] buffer = new byte[1024];
   int i;
   while ((i = inputStream.read(buffer)) > 0) {
    //use update if we have to encrypt/decrypt multiple blocks
    outputStream.write(cipher.update(buffer, 0, i));
    outputStream.flush();
   }
   
   //If we use doFinal repeatedly, the encryption will work without errors. 
   //But decryption will fail throwing exception - javax.crypto.BadPaddingException
   outputStream.write(cipher.doFinal());
   inputStream.close();
   outputStream.close();
   
   System.out.println("Encryption Completed..");

  } catch (IllegalBlockSizeException e) {
   e.printStackTrace();
  } catch (BadPaddingException e) {
   e.printStackTrace();
  } catch (InvalidKeyException e) {
   e.printStackTrace();
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
  
 }

 /**
  * 
  * @param srcFile
  * @param destFile
  */
 private void decrypt(String srcFile, String destFile) {
  File encryptedFile = new File(srcFile);
  File decryptedFile = new File(destFile);
  InputStream inputStream = null;
  OutputStream outputStream = null;
  try {
   System.out.println("Decryption started...");
   
   //Descryption will fail due to we are decrypting using different key
   //secretKey = new SecretKeySpec(password1.getBytes(), "Blowfish");
   
   cipher.init(Cipher.DECRYPT_MODE, secretKey);
   inputStream = new FileInputStream(encryptedFile);
   outputStream = new FileOutputStream(decryptedFile);

   byte[] buffer = new byte[1024];
   int i;
   while ((i = inputStream.read(buffer)) > 0) {
    outputStream.write(cipher.update(buffer, 0, i));
    outputStream.flush();
   }
   outputStream.write(cipher.doFinal());
   inputStream.close();
   outputStream.close();
   System.out.println("Decryption completed...");
   
  } catch (IllegalBlockSizeException e) {
   e.printStackTrace();
  } catch (BadPaddingException e) {
   e.printStackTrace();
  } catch (InvalidKeyException e) {
   e.printStackTrace();
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
  
 }
 
 public static void main(String[] args) {
  String fileTobeEncrypted = "netbeans.jpeg";
  String encryptedFile = "encryptedFile.jpeg";
  String decryptedFile = "decryptedFile.jpeg";
  EncryptDecryptImages encryptFile = new EncryptDecryptImages();

  encryptFile.encrypt(fileTobeEncrypted, encryptedFile);
  encryptFile.decrypt(encryptedFile, decryptedFile);
 }
}

Output












Reference Links

No comments:

Post a Comment