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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
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; } } |