HexBlowfish Algorithm

HexBlowfish Algorithm

Blowfish is an encryption algorithm that can be used as a replacement for the DES or IDEA algorithms. It is a symmetric (that is, a secret or private key) block cipher that uses a variable-length key, from 32 bits to 448 bits. It is significantly faster than DES. Blowfish is unpatented, license-free, and available for free to all uses.

In this example, Just convert input String to Hexadecimal for encrypt/decrypt using Blowfish algorithm.

Solution
private static byte[] encrypt(byte[] inpBytes,
SecretKey key, String xform) throws Exception {
Cipher cipher = Cipher.getInstance(xform);
cipher.init(Cipher.ENCRYPT_MODE, key);
return cipher.doFinal(inpBytes);
}

private static byte[] decrypt(byte[] inpBytes,
SecretKey key, String xform) throws Exception {
Cipher cipher = Cipher.getInstance(xform);
cipher.init(Cipher.DECRYPT_MODE, key);
return cipher.doFinal(inpBytes);
}

private static String tangleEncrypt(String input) {
char[] inputCharArray = input.toCharArray();
input = “”;
for(char charInput : inputCharArray) {
int ascii = (int)charInput;
input += Integer.toHexString(ascii);
}
System.out.println(“Your Input: “+input);
return input;
}

private static String tangleDecrypt(String input) {
int divide = 2;
String result = “”;
for(int i=0; i<input.length();) {
String tempInput = input.substring(i, i + divide);
result += (char)Integer.parseInt(tempInput,16);
i = i + divide;
}
return result;
}

public static void main(String[] unused) throws Exception {
String xform = “Blowfish”;
KeyGenerator kg = KeyGenerator.getInstance(“Blowfish”);
kg.init(56);
SecretKey key = kg.generateKey();

byte[] dataBytes = tangleEncrypt(“~AbdulJeilani~”).getBytes();

byte[] encBytes = encrypt(dataBytes, key, xform);
System.out.println(“Encrypted: “+new String(encBytes, “US-ASCII”));

byte[] decBytes = decrypt(encBytes, key, xform);
System.out.println(“Decrypted: “+tangleDecrypt(new String(decBytes, “US-ASCII”)));

boolean expected = java.util.Arrays.equals(dataBytes, decBytes);
}

output
Your Input: 7e416264756c4a65696c616e697e
Encrypted: A?A??d?rQ??Z,?|W?????` -??.?f ?
Decrypted: ~AbdulJeilani~