Encrypt And Decrypt Using Blowfish In Java

Java Cryptography classes offer a variety of ways for you to encrypt and decrypt. One of them is using Blowfish. The methods below encrypt and decrypt Strings using a String key as its secret key.

public static String encryptBlowfish(String to_encrypt, String strkey) {
  try {
    SecretKeySpec key = new SecretKeySpec(strkey.getBytes(), "Blowfish");
     Cipher cipher = Cipher.getInstance("Blowfish");
     cipher.init(Cipher.ENCRYPT_MODE, key);
     return new String(cipher.doFinal(to_encrypt.getBytes()));
  } catch (Exception e) { return null; }
}
 
public static String decryptBlowfish(String to_decrypt, String strkey) {
  try {
     SecretKeySpec key = new SecretKeySpec(strkey.getBytes(), "Blowfish");
     Cipher cipher = Cipher.getInstance("Blowfish");
     cipher.init(Cipher.DECRYPT_MODE, key);
     byte[] decrypted = cipher.doFinal(to_decrypt.getBytes());
     return new String(decrypted);
  } catch (Exception e) { return null; }
}
Bookmark and Share

Found this post useful? Buy me a cup of coffee or help support the sponsors on the right.

Related Posts with Thumbnails

1 Star2 Stars3 Stars4 Stars5 Stars (7 votes, average: 4.43 out of 5)
Loading ... Loading ...

Leave a Reply