在PHP中解密C#加密字符串

时间:2017-02-03 14:19:20

标签: c# php encryption

我试图用PHP解密一个字符串。

我使用此代码加密C#中的字符串:

    public string EncryptMessage(string text, string key)
    {
        byte[] plainTextBytes = Encoding.UTF8.GetBytes(text);
        RijndaelManaged aes = new RijndaelManaged();
        aes.KeySize = 256;
        aes.BlockSize = 256;
        aes.Padding = PaddingMode.Zeros;
        aes.Mode = CipherMode.CBC;

        aes.Key = Encoding.Default.GetBytes(key);
        aes.GenerateIV();

        string IV = ("specialstring" + Encoding.Default.GetString(aes.IV));

        ICryptoTransform AESEncrypt = aes.CreateEncryptor(aes.Key, aes.IV);
        byte[] buffer = plainTextBytes;

        return
    Convert.ToBase64String(Encoding.Default.GetBytes(Encoding.Default.GetString(AESEncrypt.TransformFinalBlock(buffer, 0, buffer.Length)) + IV));

    } 

我还使用此代码在PHP中解密字符串:

function decrypt($text, $pkey)
{
    $key = $pkey;
    $text = base64_decode($text);
    $IV = substr($text, strrpos($text, "specialstring") );
    $text = str_replace("specialstring".$IV, "", $text);
    $res = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_CBC, $IV), "\0");
    return $res;
}

PHP代码始终返回空字符串。我的代码有什么问题,我该如何解决?

0 个答案:

没有答案