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! :)