Experiment 5: Write a C/JAVA program to implement the BlowFish algorithm logic.
Experiment 7: Using Java Cryptography, encrypt the text ―Hello world‖ using BlowFish. Create your own key using Java keytool.
Source code:
import java.io.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
import java.util.Base64;
public class BlowFish {
public static void main(String[] args) throws Exception {
// Generate Blowfish key
KeyGenerator keyGenerator = KeyGenerator.getInstance("Blowfish");
keyGenerator.init(128);
Key secretKey = keyGenerator.generateKey();
// Initialize cipher for encryption
Cipher cipherOut = Cipher.getInstance("Blowfish/CFB/NoPadding");
cipherOut.init(Cipher.ENCRYPT_MODE, secretKey);
// Corrected Encoder usage
Encoder encoder=new Encoder();
byte iv[] = cipherOut.getIV();
if (iv != null) {
System.out.println("Initialization Vector of the Cipher: " + Base64.getEncoder().encodeToString(iv));
}
// File handling
FileInputStream fin = new FileInputStream("inputFile.txt");
FileOutputStream fout = new FileOutputStream("outputFile.txt");
CipherOutputStream cout = new CipherOutputStream(fout, cipherOut);
// Encrypt file content
int input;
while ((input = fin.read()) != -1) {
cout.write(input);
}
// Close streams
fin.close();
cout.close();
}
}
Output:
Initialization Vector of the Cipher: dI1MXzW97oQ=
Contents of inputFile.txt: Hello World
Contents of outputFile.txt: ùJÖ˜ NåI“