javaencryptionblowfish

Different results with Blowfish between java 6 and java 8


public class Main {

public static void main(String[] args) {
    String result = blowfish("123123");
    System.out.println(result);
}

public static String blowfish(String source) {
    final String BLOWFISH_KEY = "22ddba9832444234";

    try {
        Cipher cipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");

        cipher.init(
                Cipher.ENCRYPT_MODE,
                new SecretKeySpec(BLOWFISH_KEY.getBytes("UTF-8"), "Blowfish")
        );
        return new String(cipher.doFinal(source.getBytes("UTF-8")));

    } catch (Exception e) {
        e.printStackTrace(System.out);
        return null;
     }
   }
}

If I run this code with java 6 oracle and then run with java 8 openJdk gives me different outputs, Why?


Solution

  • I found the problem, thanks everybody for the help, was the UTF-8, in java 6 and 7 the implementation is different from java 8, and java 6 IBM, what helped me was implement UTF_8.class from openjdk 8 on jdk 6. For more help look here Java 8 change in UTF-8 decoding.

    https://gist.github.com/AndrewsK30/30ad3e63203f2ebcbbab66619ec7c064

    Just use new String(arrayByte,new CustomCharset())