Symmetric Key Generation Using Blowfish Algorithm

Description

BlowfishKey.java generates the sysmetric key using Blowfish algorithm. Key size assigned here is 128 bits. It works for key size of 256 and 448 bits also.

  • Filename: BlowfishKey.java

import javax.swing.*;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Random ;
class BlowfishKey {
byte[] skey = new byte[1000];
String skeyString;
static byte[] raw;

String inputMessage,encryptedData,decryptedMessage;

public BlowfishKey() {
generateSymmetricKey();
}
void generateSymmetricKey() {
try {
Random r = new Random();
int num = r.nextInt(10000);
String knum = String.valueOf(num);
byte[] knumb = knum.getBytes();
skey=getRawKey(knumb);
skeyString = new String(skey);
System.out.println("Blowfish Symmetric key = "+skeyString);
}
catch(Exception e) {
System.out.println(e);
}
}
private static byte[] getRawKey(byte[] seed) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("Blowfish");
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
sr.setSeed(seed);
kgen.init(128, sr); // 128, 256 and 448 bits may be available
SecretKey skey = kgen.generateKey();
raw = skey.getEncoded();
return raw;
}

public static void main(String args[]) {
BlowfishKey bf = new BlowfishKey();
}
}