Rijndael 256在C#和PHP 7.2 +

时间:2019-01-13 10:09:53

标签: c# php encryption mcrypt php-openssl

我已经在C#端实现了Rijandael 256加密,如下所示:

public static string EncryptPasswordForWordpressPlugin(string message)
{
    const string Ivstring = "W4jlzx2XaqQTtbd67813xys4133tl2S7";
    string keystring = "I7R9VdkzGQ215iCkC0940ST01Wk58Mr9";

    byte[] key = ASCIIEncoding.UTF8.GetBytes(keystring);
    byte[] iv = ASCIIEncoding.UTF8.GetBytes(Ivstring);

    string encrypted = null;
    var rj = new RijndaelManaged {BlockSize = 256};
    rj.Key = key;
    rj.IV = iv;
    rj.Mode = CipherMode.CBC;

    try
    {
        var ms = new MemoryStream();

        using (var cs = new CryptoStream(ms, rj.CreateEncryptor(key, iv), CryptoStreamMode.Write))
        {
            using (var sw = new StreamWriter(cs))
            {
                sw.Write(message);
                sw.Close();
            }
            cs.Close();
        }
        byte[] encoded = ms.ToArray();
        encrypted = Convert.ToBase64String(encoded);

        ms.Close();
    }
    catch (Exception e)
    {
        Console.WriteLine("An error occurred: {0}", e.Message);
    }
    finally
    {
        rj.Clear();
    }

    return encrypted;
}

在PHP中,我使用mcrypt实现了解密:

class Decrypt {

    protected $mcrypt_cipher = MCRYPT_RIJNDAEL_256;
    protected $mcrypt_mode = MCRYPT_MODE_CBC;
    protected $key = "I7R9VdkzGQ215iCkC0940ST01Wk58Mr9";
    protected $iv = "W4jlzx2XaqQTtbd67813xys4133tl2S7";

    public function decrypt($encrypted){
        $iv_utf = mb_convert_encoding($this->iv, 'UTF-8');
        return mcrypt_decrypt($this->mcrypt_cipher, $this->key, base64_decode($encrypted), $this->mcrypt_mode, $iv_utf);

    }
}

现在已删除PHP 7.2 mcrypt,我正在寻找一种无需更改C#端即可在PHP中实现解密的方法。

我对加密/解密不是很满意,无法找到正确的解决方案。 是否可以在PHP中使用openssl来实现这种解密,或者还有另一种方法。 谢谢。

0 个答案:

没有答案