Java算法转php,将Java中的AES256加密算法转换为PHP

大家好,我遇到了一个主要障碍,试图将使用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()调用和所有这些让我非常困惑。

预先感谢您的帮助! :)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值