Reputation: 67
I need to implement openSSL in my application, because we had to change the version from 5.6 to version 7.2
I would like to understand how to solve this problem.
Any doubts that arise about my environment, or how I am using the application, just ask that I will respond.
<?php
$dados = '#########################################################';
$key = '################################';
$iv = '################################';
/**
* running in version 7.2
*/
$data = openssl_decrypt($dados, 'AES-128-CBC', $key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING, $iv);
/**
* Erro:
* $ clear && ./php descriptografa-senha.php
*
* PHP Warning: openssl_decrypt(): IV passed is 32 bytes long which is longer than the 16 expected by selected cipher, truncating in ./descriptografa-senha.php on line 12
* PHP Stack trace:
* PHP 1. {main}() ./descriptografa-senha.php:0
* PHP 2. openssl_decrypt() ./descriptografa-senha.php:12
*
* Warning: openssl_decrypt(): IV passed is 32 bytes long which is longer than the 16 expected by selected cipher, truncating in ./descriptografa-senha.php on line 12
*
* Call Stack:
* 0.4121 125736 1. {main}() ./descriptografa-senha.php:0
* 0.4121 126312 2. openssl_decrypt() ./descriptografa-senha.php:12
*/
/**
* running in version 5.6
*/
$mcrypt_cipher = MCRYPT_RIJNDAEL_256;
$mcrypt_mode = MCRYPT_MODE_CBC;
$encrypted = $dados;
$iv_utf = mb_convert_encoding($iv, 'UTF-8');
$decrypted = mcrypt_decrypt($mcrypt_cipher, $key, base64_decode($encrypted), $mcrypt_mode, $iv_utf);
print_r($decrypted);
/**
* performed normally
*/
This was the code that generated the encryption, see if you can understand something and help me in the problem, the language to use to encrypt is C # and the language to decrypt and PHP 7.2
public static string Cryptography(string text, string pass = "#############################")
{
if (string.IsNullOrEmpty(pass))
return string.Empty;
var rijndaelmanaged = new RijndaelManaged();
rijndaelmanaged.KeySize = 256;
rijndaelmanaged.BlockSize = 256;
rijndaelmanaged.Padding = PaddingMode.PKCS7;
rijndaelmanaged.Key = Convert.FromBase64String(Base64Encode(pass));
rijndaelmanaged.IV = Convert.FromBase64String(Base64Encode(vetorInicializacao));
var encrypt = rijndaelmanaged.CreateEncryptor(rijndaelmanaged.Key, rijndaelmanaged.IV);
byte[] xBuff = null;
using (var ms = new MemoryStream())
{
using (var cs = new CryptoStream(ms, encrypt, CryptoStreamMode.Write))
{
byte[] xXml = Encoding.UTF8.GetBytes(text);
cs.Write(xXml, 0, xXml.Length);
}
xBuff = ms.ToArray();
}
String out = Convert.ToBase64String(xBuff);
return out;
}
Upvotes: 4
Views: 20640
Reputation: 67
The solution to this is to use the phpseclib/mcrypt_compat library
composer require phpseclib/mcrypt_compat
Upvotes: -1
Reputation: 53626
From the manual:
$iv is as in the case of $password, a String of bytes.
It looks like you have $iv
encoded as a hexadecimal string:
$iv = '################################';
You probably just need to convert it to a binary byte string:
$iv = hex2bin('################################');
Ditto for $key
.
Upvotes: 5