I have a need to be able to encrypt and decrypt strings in both php and vb.net. PHP needs to be able to decrypt strings encrypted in vb.net and vb.net needs to be able to decrypt string encrypted in php.
I have working code in both environments that work within that environment, but they do not work with each other.
I've done alot of searching on this one and I believe that this is to do with the openssl implementation of AES256 and the way it salts with the IV or something along those lines.
The vb implementation is generating a random GUID for the IV and prepending it to the encrypted string before base64 encoding it.
My vb.net code is like so...
Imports System.IO
Imports System.Text
Imports System.Security.Cryptography
Public Class tbSecurity
Public Shared enckey = "00000000000000000000000000000000"
Public Shared Function AESEncryptStringToBase64(strPlainText As String) As String
Dim Algo As RijndaelManaged = RijndaelManaged.Create()
With Algo
.BlockSize = 128
.FeedbackSize = 128
.KeySize = 256
.Mode = CipherMode.CBC
.IV = Guid.NewGuid().ToByteArray()
.Key = Encoding.ASCII.GetBytes(enckey)
End With
Using Encryptor As ICryptoTransform = Algo.CreateEncryptor()
Using MemStream As New MemoryStream
Using CryptStream As New CryptoStream(MemStream, Encryptor, CryptoStreamMode.Write)
Using Writer As New StreamWriter(CryptStream)
Writer.Write(strPlainText)
End Using
AESEncryptStringToBase64 = Convert.ToBase64String(Algo.IV.Concat(MemStream.ToArray()).ToArray())
End Using
End Using
End Using
End Function
Public Shared Function AESDecryptBase64ToString(strCipherText As String) As String
Dim arrSaltAndCipherText As Byte() = Convert.FromBase64String(strCipherText)
Dim Algo As RijndaelManaged = RijndaelManaged.Create()
With Algo
.BlockSize = 128
.FeedbackSize = 128
.KeySize = 256
.Mode = CipherMode.CBC
.IV = arrSaltAndCipherText.Take(16).ToArray()
.Key = Encoding.ASCII.GetBytes(enckey)
End With
Using Decryptor As ICryptoTransform = Algo.CreateDecryptor()
Using MemStream As New MemoryStream(arrSaltAndCipherText.Skip(16).ToArray())
Using CryptStream As New CryptoStream(MemStream, Decryptor, CryptoStreamMode.Read)
Using Reader As New StreamReader(CryptStream)
AESDecryptBase64ToString = Reader.ReadToEnd()
End Using
End Using
End Using
End Using
End Function
End Class
PHP code is like so...
<?php
$inputstr = $_GET['str'];
$ky = '00000000000000000000000000000000'; // 32 * 8 = 256 bit key
$iv = '1234567890123456'; // 32 * 8 = 256 bit iv
echo "<hr>";
echo "Input Str = " . $inputstr;
echo "<hr>";
echo "Decrypted Str = " . decryptRJ256($ky,$iv,$inputstr);
function decryptRJ256($key,$iv,$string_to_decrypt)
{
$string_to_decrypt = base64_decode($string_to_decrypt);
$rtn = openssl_decrypt($string_to_decrypt, "aes-256-cbc", $key,0,$iv);
$rtn = rtrim($rtn, "\0\4");
return($rtn);
}
function encryptRJ256($key,$iv,$string_to_encrypt)
{
$rtn = openssl_decrypt($string_to_encrypt, "aes-256-cbc", $key,0,$iv);
$rtn = base64_encode($rtn);
return($rtn);
}
?>
I've come across a 'working' example of code that uses AES128 and the mcrypt extension for PHP, however I'm using PHP8 and Mcrypt is deprecated and unsupported, so I really don't want to take that route.
Is anyone able to point me in the right direction for either making strings encrypted in PHP using the openssl implementation of AES256 decryptable in vb.net or the other way round? I don't care which environment I have to amend, or even if its both.
Many thanks in advance