大家好,我遇到了一个主要障碍,试图将使用AES256加密的API从Java示例集成到PHP,这是本文档中提供的Java版本。
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;
}
}}
从这我可以收集,我应该像这样在PHP运行的东西
openssl_encrypt($data, "aes-256-cbc", “YOURKEY”, null, "INITVECTOREXAMPLE");然后我应该将结果转换为Base64并返回它?它是否正确? ZeroBytePadding()调用和所有这些让我非常困惑。
预先感谢您的帮助! :)