Encrypt Using Blowflish And Java
Posted by tech on
May 18, 2009
Blowfish is a symmetric block cipher that can be used as a drop-in replacement for DES or IDEA. It takes a variable-length key, from 32 bits to 448 bits, making it an ideal way for encryption purposes. Java supports Blowflish encryption in its API. Here is a method that will encrypt using Blowfish. Part of the code converts the string to be encrypted to byte[] array so this method uses a secondary method called convertBinary2Hexadecimal() which you can find here.
public static String encrypt(String key, String plain) { Security.insertProviderAt(new org.bouncycastle.jce.provider.BouncyCastleProvider(), 3); byte[] plainText = plain.getBytes(); String cipher = null; try { SecretKeySpec blowfishKey = new SecretKeySpec(key.getBytes(), "Blowfish"); Cipher blowfishCipher = Cipher.getInstance("Blowfish", "BC"); blowfishCipher.init(Cipher.ENCRYPT_MODE, (Key)blowfishKey); byte[] cipherText = blowfishCipher.doFinal(plainText); cipher = convertBinary2Hexadecimal(cipherText); } catch (Exception e) { e.printStackTrace(); } return cipher; }






