import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; import java.util.Base64; public class Twofish implements Cryptable { private static final String ALGORITHM = "Twofish"; private SecretKey secretKey; public Twofish() throws Exception { KeyGenerator keyGen = KeyGenerator.getInstance(ALGORITHM); keyGen.init(256); // Twofish key length can be 128, 192, or 256 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); } }