C# Rijndael解密方法传输到PHP?

C# Rijndael解密方法传输到PHP?,c#,php,rijndael,C#,Php,Rijndael,我在互联网上找到了以下我用来解密的方法 public static string DecryptString(string cipherText, string passPhrase, string initVector) { byte[] initVectorBytes = Encoding.UTF8.GetBytes(initVector); byte[] cipherTextBytes = Convert.FromBase64String(ciphe

我在互联网上找到了以下我用来解密的方法

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);
    }
因此,这个方法得到一个加密文本、一个密码和一个IV(以16个字符长的字符串的形式)。例如: 密码:test IV:hCIoZVI5lrtCgdNo 加密消息:sequure8w3ruqvyqe6oa==

使用上述方法,我可以成功地解密消息(结果应该是“test”)

现在我尝试在PHP中使用openssl_decrypt(),以便也能够在PHP中解密加密消息。但这是行不通的。我用了这个:

$encrypted = "SeQUquUrE8W3rUqvYQe6oA==";
$password = "test";
$iv = "hCIoZVI5lrtCgdNo";
echo openssl_decrypt($encrypted, 'aes-256-cbc', $password, 0, $iv);
但这不起作用?你能帮助我吗?
谢谢大家!

我将首先研究
openssl\u decrypt
背后使用的机制。如果这与RijndaelThank你的回答完全不同,我不会感到惊讶。在一个论坛上,我读到Rijndael的256bit键基本上是AES256。由于加密方法中的密钥大小是256,我认为它是AES256。根据其他一些论坛的说法,不推荐使用的mcrypt函数被openssl_decrypt所取代,openssl_decrypt也应该对AES256进行解密——至少我是这么认为的。但我不是这方面的专家。好吧,我明白了,谢谢。然后问题仍然存在:任何关于如何将C#代码传输到PHP的想法,以便我能够解密同样在PHPPasswordDeriveBytes中使用C#程序加密的消息,都是一个密钥派生函数,而您根本没有在PHP代码中实现这一点: