Here is my php for openssl_encrypt:
$text = "does this work";
$key = "1234567890123456789012345678901234567890123456789012345678901234";
$method = "AES-256-CBC";
$cipher_length = openssl_cipher_iv_length($method); //16
$iv_new = openssl_random_pseudo_bytes($cipher_length);
$bin2hex = bin2hex($iv_new); //"a64d63874ba9ee8f5a5028cb40ab70a4"
$openssl = openssl_encrypt($text, $method, $key, 0, $iv_new);
$encrypted = $bin2hex . $openssl;
Value of $encrypted is:
"a64d63874ba9ee8f5a5028cb40ab70a4yFFQwLuOHeWouyfp0dyrnw==" //encrypted in DecryptPHP
Here is my php for openssl_decrypt:
$iv_strlen = 32;
preg_match("/^(.{" . $iv_strlen . "})(.+)$/", $encrypted, $regs);
list(, $_iv, $_crypted_string) = $regs;
$_hex2bin = hex2bin($_iv);
$ctype_xdigit = ctype_xdigit($_iv);
$remainder = strlen($_iv) % 2 == 0;
$new_text = openssl_decrypt($_crypted_string, $method, $key, 0, $_hex2bin);
$new_text value is:
"does this work"
So this works fine but how can I take this string:
"a64d63874ba9ee8f5a5028cb40ab70a4yFFQwLuOHeWouyfp0dyrnw=="
and convert it to "does this work" in C#? I've tried so many things and keep coming up with bad data. I even tried this with no luck. Any help would be greatly appreciated.
Decrypt string in C# that was encrypted with PHP openssl_encrypt
Here is my c# code:
string DecryptPHP()
{
//var _key = System.Text.Encoding.UTF8.GetBytes(key);
//var _iv = System.Text.Encoding.UTF8.GetBytes(iv);
var key = key = "1234567890123456789012345678901234567890123456789012345678901234";
var _key = FromHex(key);
var iv = "a64d63874ba9ee8f5a5028cb40ab70a4";
var _iv = FromHex(iv);
//pad key out to 32 bytes (256bits) if its too short
if (_key.Length < 32)
{
var paddedkey = new byte[32];
Buffer.BlockCopy(_key, 0, paddedkey, 0, key.Length);
_key = paddedkey;
}
//get the encrypted data and decrypt
var data_encrypted_string = "yFFQwLuOHeWouyfp0dyrnw==";
byte[] encryptedBytes = Convert.FromBase64String(data_encrypted_string);
return DecryptStringFromBytesAes(encryptedBytes, _key, _iv);
}
byte[] FromHex(string hex)
{
hex = hex.Replace("-", "");
byte[] raw = new byte[hex.Length / 2];
for (int i = 0; i < raw.Length; i++)
{
raw[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16);
}
return raw;
}
static string DecryptStringFromBytesAes(byte[] cipherText, byte[] key, byte[] iv)
{
// Check arguments.
if (cipherText == null || cipherText.Length <= 0)
throw new ArgumentNullException("cipherText");
if (key == null || key.Length <= 0)
throw new ArgumentNullException("key");
if (iv == null || iv.Length <= 0)
throw new ArgumentNullException("iv");
// Declare the RijndaelManaged object
// used to decrypt the data.
RijndaelManaged aesAlg = null;
// Declare the string used to hold
// the decrypted text.
string plaintext;
// Create a RijndaelManaged object
// with the specified key and IV.
aesAlg = new RijndaelManaged { Mode = CipherMode.CBC, Padding = PaddingMode.None, KeySize = 256, BlockSize = 128, Key = key, IV = iv };
// Create a decrytor to perform the stream transform.
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
// Create the streams used for decryption.
using (MemoryStream msDecrypt = new MemoryStream(cipherText))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
// Read the decrypted bytes from the decrypting stream
// and place them in a string.
plaintext = srDecrypt.ReadToEnd();
srDecrypt.Close();
}
}
}
return plaintext;
}