1

I've been doing much searching but can't find an adequate explanation for why this:

public class encryption_test {

    private static final String text_encoding = "UTF-8";

    private byte [] byte_array (String input) throws Exception {
        return input.getBytes (text_encoding);
    }// byte_array;

    private byte [] encrypt (String text) throws Exception {
        Cipher cypher = Cipher.getInstance ("AES/CBC/PKCS5Padding");
        Key key = new SecretKeySpec (byte_array ("12345678123456781234567812345678"), "AES");
        cypher.init (Cipher.ENCRYPT_MODE, key, new IvParameterSpec (byte_array ("1234567812345678")));
        return cypher.doFinal (byte_array (text));
    }// encrypt;

    public String doit () throws Exception {
        byte [] etext = encrypt ("this is a plain string.");
        return new String (Base64.getEncoder ().encode (etext));        
    }// doit;

}// encryption_test;

in Java, yields:

dAza6vYiYzJ9W/i4zPHVfvA8UUyw8Sq1g2YjuLt3EjI=

Whereas:

base64_encode (mcrypt_encrypt (MCRYPT_RIJNDAEL_128, "12345678123456781234567812345678", "This is a plain string.", MCRYPT_MODE_CBC, "1234567812345678"))

in PHP yields:

G+tdEOfQTtVCQGxW3N5uzkqN207OyfIPxS6zf2xrKKY=

According to everything I've read in the forums, they should both return the same thing. Can anyone please help me?

1
  • 2
    The strings you're encoding are different. Hint: Capital T. It works fine after you fix that. Commented Jun 11, 2015 at 21:59

1 Answer 1

2

Your test strings are different. In Java, you put "this is a plain string.". In PHP, you put "This is a plain string.". Mind the capital 'T'.

Please adjust and try again.

Sign up to request clarification or add additional context in comments.

1 Comment

Yep. That's it. I should have seen it (thus, the benefit of another set of eyes). Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.