Encrypt And Decrypt Using Blowfish In Java
Posted by tech on
July 15, 2009
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; } }

/rating_on.png)
(7 votes, average: 4.43 out of 5)






