-3

Hi everyone i'm hitting a major roadblock with trying to integrate an API that uses AES256 encryption from a Java example to PHP, this is the Java version that is present in the documentation.

package com.partner.carrier.library;

import java.io.UnsupportedEncodingException;

import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.DataLengthException;
import org.bouncycastle.crypto.InvalidCipherTextException;
import org.bouncycastle.crypto.engines.RijndaelEngine;
import org.bouncycastle.crypto.modes.CBCBlockCipher;
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
import org.bouncycastle.crypto.paddings.ZeroBytePadding;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.crypto.params.ParametersWithIV;
import org.bouncycastle.util.encoders.Base64;

import com.partner.config.ConfigData;

public class AES {
    private static byte[] _sessionKey = "YOUR-KEY".getBytes(); 
    private static byte[] _initialVector = "INITVECTOREXAMPLE".getBytes();
        private static int _keySize = 256;
        private static int _blockSize = 128;    

    public static String Encrypt(String txt)
    {
        PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new RijndaelEngine(_blockSize)), new ZeroBytePadding());
        CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(_sessionKey, 0, (_keySize / 8)), _initialVector, 0, (_blockSize / 8));
        cipher.init(true, ivAndKey);

        try {
            byte[] decoded = txt.trim().getBytes("UTF-8");
            byte[] encoded  = new byte[cipher.getOutputSize(decoded.length)];
            int len = cipher.processBytes(decoded, 0, decoded.length, encoded, 0);

            cipher.doFinal(encoded, len);
            return new String(Base64.encode(encoded));
        } 
        catch (DataLengthException | IllegalStateException | InvalidCipherTextException | UnsupportedEncodingException e) {
            return null;
        }
    }

}

From this i can gather that i should run something in PHP like this

openssl_encrypt($data, "aes-256-cbc", “YOURKEY”, null, "INITVECTOREXAMPLE");

And then i should convert the result of that to Base64 and return it? Is this correct? The ZeroBytePadding() calls and all of that are really confusing to me.

Thank you in advance for your help! :)

João Serra
  • 509
  • 9
  • 21
  • *"And then i should convert the result of that to Base64 and return it?"* - It's already Base64-encoded. Haven't you looked at what you get from that line of code? *"The ZeroBytePadding() calls and all of that are really confusing to me."* - Why? Haven't you read the documentation of `openssl_encrypt`? It contains the option `OPENSSL_ZERO_PADDING`. – Artjom B. Jun 08 '17 at 21:49
  • @ArtjomB. Sorry if the questions seems stupid, i've never done this kind of stuff before hence why i'm a bit overwhelmed, it's the first time i've looked at openssl in PHP. Things that to you are obvious to me are not. But right i've done that, after that point i would theoretically get the same output as the Java example right? – João Serra Jun 08 '17 at 22:07

1 Answers1

0

Solved the problem using this excellent class

http://aesencryption.net/#PHP-aes-encryption-example

Just tweaked it slightly and there i go, now it works fine.

$encoder = new AES($data, "PRIVATEKEYHERE", 128, "cbc", "INITIALVECTORIFANY");
$_SESSION['ENCRYPTED_DATA'] = $encoder->encrypt();

Hope this helps someone later down the line :) Thanks!

João Serra
  • 509
  • 9
  • 21