Blowfish encryption decryption in Java

Blowfish Encryption Decryption using KeyGenerator

----------------------------------------------

import java.security.NoSuchAlgorithmException;

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

import org.apache.commons.codec.binary.Base64;

public class BlowfishCipherExample {

 public static void main(String[] args) throws Exception {

   // create a key generator based upon the Blowfish cipher
   KeyGenerator keygenerator = KeyGenerator.getInstance("Blowfish");

   // create a secret key
   SecretKey secretkey = keygenerator.generateKey();


   // create a cipher based upon Blowfish
   Cipher cipher = Cipher.getInstance("Blowfish");

   // initialise cipher to with secret key
     cipher.init(Cipher.ENCRYPT_MODE, secretkey);

   // get the text to encrypt
   String inputText = "User_ID=Test12&User_Timestamp=20121030040002";

  
   // encrypt message
    byte[] encrypted = cipher.doFinal(inputText.getBytes());

   // re-initialise the cipher to be in decrypt mode
   cipher.init(Cipher.DECRYPT_MODE, secretkey);

   // decrypt message
   byte[] decrypted = cipher.doFinal(encrypted);

   // and display the results
   System.out.println("encrypted text: " + new String(encrypted) + "\n" + "decrypted text: " + new String(decrypted));

  
 }
 
}
--------------------------------------------------------------------

Blowfish Encryption using existing secret key

import java.security.NoSuchAlgorithmException;

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

import org.apache.commons.codec.binary.Base64;

public class BlowfishCipherExample {

  public static void main(String[] args) throws Exception {

String keyStr="CEM8hHSuL4nuXDydDn8Ey5jd"; // Let's assume you have this key

    // create a key generator based upon the Blowfish cipher
    KeyGenerator keygenerator = KeyGenerator.getInstance("Blowfish");

    // create a secret key
   SecretKeySpec secretkey = new SecretKeySpec(keyStr.getBytes(), "Blowfish");


    // create a cipher based upon Blowfish
    Cipher cipher = Cipher.getInstance("Blowfish");

    // initialise cipher to with secret key
     cipher.init(Cipher.ENCRYPT_MODE, secretkey);

    // get the text to encrypt
    String inputText = "User_ID=Test12&User_Timestamp=20121030040002";

   
    // encrypt message
    byte[] encrypted = cipher.doFinal(inputText.getBytes());

    // re-initialise the cipher to be in decrypt mode
   cipher.init(Cipher.DECRYPT_MODE, secretkey);

    // decrypt message
   byte[] decrypted = cipher.doFinal(encrypted);

    // and display the results
   System.out.println("encrypted text: " + new String(encrypted) + "\n" + "decrypted text: " + new String(decrypted));

  
  }
 
}

Comments

Popular posts from this blog

Struts 2 : Warning :No configuration found for the specified action: 'Login.action' in namespace: '/'

Upload csv file at Google Cloud Storage using java

Read Images from a xlsx file using Apache POI