I found the following method on the Internet which I use to decrypt.
public static string DecryptString(string cipherText, string passPhrase, string initVector)
{
byte[] initVectorBytes = Encoding.UTF8.GetBytes(initVector);
byte[] cipherTextBytes = Convert.FromBase64String(cipherText);
PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, null);
byte[] keyBytes = password.GetBytes(keysize / 8);
RijndaelManaged symmetricKey = new RijndaelManaged();
symmetricKey.Mode = CipherMode.CBC;
symmetricKey.Padding = PaddingMode.PKCS7;
ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes);
MemoryStream memoryStream = new MemoryStream(cipherTextBytes);
CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
byte[] plainTextBytes = new byte[cipherTextBytes.Length];
int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
memoryStream.Close();
cryptoStream.Close();
return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
}
So, this method gets an encrypted text, a password and an IV (in form of a 16 characters long string). For example: Password: test IV: hCIoZVI5lrtCgdNo Encrypted Message: SeQUquUrE8W3rUqvYQe6oA==
With the above mentioned method I can successfully decrypt the message (the result should be "test").
Now I tried to use openssl_decrypt() in PHP in order to also be able to decrypt encrypted messages in PHP. But this does not work. I used this:
$encrypted = "SeQUquUrE8W3rUqvYQe6oA==";
$password = "test";
$iv = "hCIoZVI5lrtCgdNo";
echo openssl_decrypt($encrypted, 'aes-256-cbc', $password, 0, $iv);
But this does not work? Can you help me? Thank you!