EXP-07: Using Java Cryptography, encrypt the text “Hello world” using Blow Fish.
Create your
own key using Java key tool.
package cns;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
public class BlowfishEncryption {
public static String encryptBlowfish(String plainText, String key) throws Exception {
SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "Blowfish");
Cipher cipher = Cipher.getInstance("Blowfish");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encrypted = cipher.doFinal(plainText.getBytes());
return Base64.getEncoder().encodeToString(encrypted);
}
public static String decryptBlowfish(String encryptedText, String key) throws Exception {
SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "Blowfish");
Cipher cipher = Cipher.getInstance("Blowfish");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decrypted = cipher.doFinal(Base64.getDecoder().decode(encryptedText));
return new String(decrypted);
}
public static void main(String[] args) {
try {
String key = "451017189"; // Blowfish key must be 8+ bytes
String plainText = "Hello World";
String encryptedText = encryptBlowfish(plainText, key);
System.out.println("Blowfish Encrypted Text: " + encryptedText);
String decryptedText = decryptBlowfish(encryptedText, key);
System.out.println("Blowfish Decrypted Text: " + decryptedText);
} catch (Exception e) {
e.printStackTrace();
}
}
}
OUTPUT:
Blowfish Encrypted Text: wGa34Bdoe+23zAUyd4eWvQ==
Blowfish Decrypted Text: Hello World