Skip to content

Instantly share code, notes, and snippets.

@dorneanu
Created July 1, 2014 17:35
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dorneanu/b8e53b034d89f6be383c to your computer and use it in GitHub Desktop.
Save dorneanu/b8e53b034d89f6be383c to your computer and use it in GitHub Desktop.
Blowfish Decrypt/Encrypt in Java
/*
* Based on https://raw.githubusercontent.com/usefulfor/usefulfor/master/security/JBoss.java
*
* JBoss.java - Blowfish encryption/decryption tool with JBoss default password
* Daniel Martin Gomez <daniel@ngssoftware.com> - 03/Sep/2009
*
* This file may be used under the terms of the GNU General Public License
* version 2.0 as published by the Free Software Foundation:
* http://www.gnu.org/licenses/gpl-2.0.html
*/
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.math.BigInteger;
import javax.xml.bind.DatatypeConverter;
public class JBlowfish
{
final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
// Converts byte array to hex string
// From: http://stackoverflow.com/questions/9655181/convert-from-byte-array-to-hex-string-in-java
public static String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for ( int j = 0; j < bytes.length; j++ ) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}
public static void main(String[] args) throws Exception
{
if ( ( args.length != 2 ) || !( args[0].equals("-e") | args[0].equals("-d") ) )
{
System.out.println( "Usage:\n\tjava JBlowfish <-e|-d> <encrypted_password>" );
return;
}
String mode = args[0];
// Configuration
byte[] key = "secret".getBytes();
String IV = "12345678";
System.out.println("-- Settings -----------");
System.out.println("KEY:\t " + bytesToHex(key));
System.out.println("IV:\t " + bytesToHex(IV.getBytes()));
// Create new Blowfish cipher
SecretKeySpec keySpec = new SecretKeySpec(key, "Blowfish");
Cipher cipher = Cipher.getInstance("Blowfish/CBC/PKCS5Padding");
String out = null;
if ( mode.equals("-e") )
{
String secret = args[1];
cipher.init(Cipher.ENCRYPT_MODE, keySpec, new javax.crypto.spec.IvParameterSpec(IV.getBytes()));
byte[] encoding = cipher.doFinal(secret.getBytes());
System.out.println("-- Encrypted -----------");
System.out.println("Base64:\t " + DatatypeConverter.printBase64Binary(encoding));
System.out.println("HEX:\t " + bytesToHex(encoding));
}
else
{
// Decode Base64
byte[] ciphertext = DatatypeConverter.parseBase64Binary(args[1]);
// Decrypt
cipher.init(Cipher.DECRYPT_MODE, keySpec, new javax.crypto.spec.IvParameterSpec(IV.getBytes()));
byte[] message = cipher.doFinal(ciphertext);
System.out.println("-- Decrypted -----------");
System.out.println("HEX:\t " + bytesToHex(message));
System.out.println("PLAIN:\t " + new String(message));
}
}
}
@manishav94
Copy link

I have implemented encryption decryption of blowfish in Java but how will we implement blowfish attack in Java?

@s0m31-hub
Copy link

s0m31-hub commented Mar 27, 2022

I know that im pure genius because i am commenting 8 years old code, but you probable should replace else (line 68) with else if (mode.equals("-d"))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment