import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; import java.util.Base64; public class Blowfish implements Cryptable { private static final String ALGORITHM = "Blowfish"; private SecretKey secretKey; public Blowfish() throws Exception { KeyGenerator keyGen = KeyGenerator.getInstance(ALGORITHM); keyGen.init(128); // Blowfish key length is 128 bits this.secretKey = keyGen.generateKey(); } @Override public String encrypt(String data) throws Exception { Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, secretKey); byte[] encryptedData = cipher.doFinal(data.getBytes()); return Base64.getEncoder().encodeToString(encryptedData); } @Override public String decrypt(String cryptedData) throws Exception { Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, secretKey); byte[] decryptedData = cipher.doFinal(Base64.getDecoder().decode(cryptedData)); return new String(decryptedData); } }