I have a remote system sending me data which it has encrypted via the openssl command line program, using blowfish encryption.
Specifically, the command being run is:
openssl enc -blowfish -a -salt -in original.txt -out encrypted.txt -pass pass:secret
For the input This is a test.
that produces U2FsdGVkX19bSsC3dXTOYssoOK5L3THkhXgiB7X1Trv6SaVO2TGz0g==
I'm trying to decrypt this content on the other side in Java with the following code.
// requires commons-io and commons-codec
public void testDecryption() throws Exception {
File encryptedFile = new File("encrypted.txt");
String password = "secret";
byte[] base64EncryptedBytes = FileUtils.readFileToByteArray(encryptedFile);
byte[] encryptedBytes = new Base64().decode(base64EncryptedBytes);
SecretKeySpec blowfishKey = new SecretKeySpec(password.getBytes("ASCII"), "Blowfish");
Cipher blowfishCipher = Cipher.getInstance("Blowfish/ECB/NoPadding");
blowfishCipher.init(Cipher.DECRYPT_MODE, blowfishKey);
byte[] decryptedContent = blowfishCipher.doFinal(encryptedBytes);
System.out.println(new String(decryptedContent));
}
Rather than the original message that currently produces...
êõïÖ¶M≥ O]¢∞;Z<HVÖ_’˚h‘:O›c=w◊®zÉ9˘
What am I doing wrong?
Some possible theories
- Blowfish/ECB/NoPadding is not the right cypher instance to use. I've tried every combination of mode and padding listed at http://docs.oracle.com/javase/1.4.2/docs/guide/security/jce/JCERefGuide.html#AppA except the OAEPWith[digest]And[mgf]Padding padding unsuccessfully.
- I've noticed that if I decrypt the file from the command line using
openssl enc -d -blowfish -a -in encrypted.txt
the password prompt is labeled 'bf-cbc', which suggests Blowfish/CBC rather than Blowfish/ECB, however if I use that I get ajava.security.InvalidKeyException: Parameters missing
exception, but I'm not sure what paramater I could add.
- I've noticed that if I decrypt the file from the command line using
- The password given on the command line should be transformed somehow, or
getBytes("ASCII")
is incorrect. - Some additional handling is required in the Java code for the salt.