AES(块大小128位)在CBC模式与pkcs# 5填充在PHP 8



尝试将php8应用程序的加密数据发送到sage pay或opayo.

我无法弄清楚如何在PHP8中使用pkcs# 5填充的CBC模式下执行AES(块大小128位)。

Openssl似乎只允许我们插入pkcs# 7和旧的PHP示例和方法都依赖于已弃用/已删除的函数。

$cryptFieldString = http_build_query($cryptFields);
$encodedCrypt = openssl_encrypt($cryptFieldString, 'aes-128-cbc', $key, 0, $key);
$crypt = '@'.strtoupper(bin2hex($encodedCrypt));

$encodedCrypt变量将始终返回pkcs# 7或原始数据。

Thanks in advance

pkcs# 5填充只是在bin2hex和openssl_encrypt的组合中添加的,您不需要使用任何其他函数。但是,您必须检查所支持的编码,例如

if( in_array( 'AES-128-CBC', openssl_get_cipher_methods() ) ) {
$cipher_method = 'AES-128-CBC';
}
if( in_array( 'aes-128-cbc', openssl_get_cipher_methods() ) ) {
$cipher_method = 'aes-128-cbc';
}

可以用bin2hex( openssl_encrypt( $input, $cipher_method, $securekey, OPENSSL_RAW_DATA, $securekey ) )

最新更新