blowfish


import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.util.Base64;

public class BlowfishExample {

public static void main(String[] args) throws Exception {
    String text = "Hello, Blowfish!";

    // Generate Blowfish key
    KeyGenerator keyGenerator = KeyGenerator.getInstance("Blowfish");
    keyGenerator.init(128); // Key size can be 32 to 448 bits
    SecretKey key = keyGenerator.generateKey();

    // Create and initialize cipher for encryption
    Cipher cipher = Cipher.getInstance("Blowfish");
    cipher.init(Cipher.ENCRYPT_MODE, key);
    byte[] encryptedBytes = cipher.doFinal(text.getBytes());
    String encryptedText = Base64.getEncoder().encodeToString(encryptedBytes);

    // Re-initialize cipher for decryption
    cipher.init(Cipher.DECRYPT_MODE, key);
    byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
    String decryptedText = new String(decryptedBytes);

    // Output
    System.out.println("Original Text: " + text);
    System.out.println("Encrypted Text: " + encryptedText);
    System.out.println("Decrypted Text: " + decryptedText);
}

}