4

I have a different result between java and php method in doing AES128 with zero padding and no IV encryption.

Here PHP code :

<?php
$ptaDataString = "secretdata";
$ptaDataString = encryptData($ptaDataString);
$ptaDataString = base64_encode($ptaDataString);
function encryptData($input) {
                $ptaKey = 'secret'; 
                $iv = "\0";
                $encrypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $ptaKey, $input, MCRYPT_MODE_CBC, $iv);
                return $encrypted;
}
echo $ptaDataString;
?>

And here is java code:

public static String encrypt() throws Exception {
    try {
        String data = "secretdata";
        String key = "secret0000000000";
        String iv = "0000000000000000";

        Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
        int blockSize = cipher.getBlockSize();

        byte[] dataBytes = data.getBytes();
        int plaintextLength = dataBytes.length;
        if (plaintextLength % blockSize != 0) {
            plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
        }

        byte[] plaintext = new byte[plaintextLength];
        System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);

        SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
        IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());

        cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
        byte[] encrypted = cipher.doFinal(plaintext);

        return new sun.misc.BASE64Encoder().encode(encrypted);

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

php resulting : kjgE5p/3qrum6ghdjiVIoA==

Java resulting : zLKhVMksRRr1VHQigmPQ2Q==

Any help would be appreciated, Thanks

1
  • It is best not to use mcrypt, it has been abandonware for nearly a decade now. It has therefore been deprecated and will be removed from the core and into PECL in PHP 7.2. It does not support standard PKCS#7 (née PKCS#5) padding, only non-standard null padding that can't even be used with binary data. mcrypt has many outstanding bugs dating back to 2003. Instead consider using defuse or RNCryptor, they provide a complete solution, are being maintained and is correct. Commented Jan 25, 2017 at 16:31

1 Answer 1

3

In Java, a zero byte expressed as a string is "\0" not "0". If you correct your example as follows, the results match:

String data = "secretdata";
String key = "secret\0\0\0\0\0\0\0\0\0\0";
String iv = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";

In the case of the IV, it's probably just easier to write:

byte[] iv = new byte[<block size>];

In the final line of your code, you access sun.misc.BASE64Encoder(). Accessing anything that starts with sun.* is frowned upon, since these are internal classes. Consider instead using:

return DatatypeConverter.printBase64Binary(encrypted);
Sign up to request clarification or add additional context in comments.

Comments

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.