I was handed this code snippet as an example, and I'm trying to make it a little more "modern":
$key='09KzF2qMBy58ZWg137N$I4h6UwJvp!ij';
$encrypted='Chttex_vuYYEK-oKQfwYrVCZYbnNh3tMgwGuK-VOsvt7TjF5M6MIcsE6e8DynZrHuxrmtmIpiN215WygdO-hzXnmx45RXzBWdxk_MkIvNoI=';
$encrypted = urlsafe_b64decode($encrypted);
$decrypted = decrypt($encrypted, $key);
$inflated = gzinflate($decrypted);
echo 'Decrypted: '.$inflated."<br />";
function urlsafe_b64decode($data) {
return base64_decode(str_pad(strtr($data, '-_', '+/'), strlen($data) % 4, '=', STR_PAD_RIGHT));
}
function decrypt($data, $key)
{
return mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $data, MCRYPT_MODE_ECB);
}
I'm on PHP7 and trying to convert this use openssl. I take the string and run it through the existing 'urlsafe_b64decode' function, then created another 'decrypt' function:
function decrypt($string, $key)
{
return openssl_decrypt($string, 'AES-256-ECB', $key);
}
I thought it would be a fairly simply 1-to-1 conversion, but its always returning "false". Any ideas what I might be doing wrong? Thanks in advance!
edit : Since the service I'm using is a .NET shop, here's their example for .NET for encrypting, if it helps at all.
Public Shared Function Encrypt(ByVal data As Byte()) As Byte()
Dim encrypted As Byte()
Using rijAlg = New System.Security.Cryptography.AesManaged()
rijAlg.Key = Encoding.ASCII.GetBytes("encryptionkeyhere")
rijAlg.Padding = System.Security.Cryptography.PaddingMode.None
rijAlg.Mode = System.Security.Cryptography.CipherMode.ECB
Dim encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV)
Using msEncrypt = New MemoryStream()
Using csEncrypt = New System.Security.Cryptography.CryptoStream(msEncrypt, encryptor, System.Security.Cryptography.CryptoStreamMode.Write)
csEncrypt.Write(data, 0, data.Length)
End Using
encrypted = msEncrypt.ToArray()
End Using
End Using
Return encrypted
End Function
update 2 So I updated my local server to have mcrypt, and was able to decode the encrypted string from my initial code snippet. This is the desired response: "sessionid=7bf727043d85e6963e640fb541d886a7454f8091&requestid=1488925528"
After googleing and stackoverflow-ing and experimenting, I'm still unable to decrypt the string correctly using openssl. Is it even possible?