Rijndael 256在c#和php之间加密/解密?

arb*_*bme 9 php c# encryption mcrypt rijndael

更新

我已对C#代码进行了更改,因此它使用的块大小为256.但是现在hello世界看起来像这个http://pastebin.com/5sXhMV11而且我无法弄清楚我应该使用rtrim()来获取最后骑的烂摊子.

另外当你说IV应该是随机的时,你的意思是不要再使用相同的IV一次或者我编码错误的方式?

再次感谢!

嗨,

我正在尝试使用在C#中加密的PHP解密字符串.我似乎无法让PHP使用mcrypt解密它,并且可以提供一些帮助.我用php得到以下错误,所以我猜我没有正确设置IV.

错误:IV参数必须与块大小一样长

两个函数使用相同的密码,密钥,IV并设置为CBC模式:

加密文本来自c#= UmzUCnAzThH0nMkIuMisqg ==
key 32 long = qwertyuiopasdfghjklzxcvbnmqwerty
iv 16 long = 1234567890123456

C#

    public static string EncryptString(string message, string KeyString, string IVString)
    {
        byte[] Key = ASCIIEncoding.UTF8.GetBytes(KeyString);
        byte[] IV = ASCIIEncoding.UTF8.GetBytes(IVString);

        string encrypted = null;
        RijndaelManaged rj = new RijndaelManaged();
        rj.Key = Key;
        rj.IV = IV;
        rj.Mode = CipherMode.CBC;

        try
        {
            MemoryStream ms = new MemoryStream();

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

            ms.Close();
        }
        catch (CryptographicException e)
        {
            Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
            return null;
        }
        catch (UnauthorizedAccessException e)
        {
            Console.WriteLine("A file error occurred: {0}", e.Message);
            return null;
        }
        catch (Exception e)
        {
            Console.WriteLine("An error occurred: {0}", e.Message);
        }
        finally
        {
            rj.Clear();
        }

        return encrypted;
    }
Run Code Online (Sandbox Code Playgroud)

PHP

var $mcrypt_cipher = MCRYPT_RIJNDAEL_256;
var $mcrypt_mode = MCRYPT_MODE_CBC;

function decrypt($key, $iv, $encrypted)
{
    $encrypted = base64_decode($encrypted);

    $decrypted = rtrim(mcrypt_decrypt($this->mcrypt_cipher, $key, $encrypted, $this->mcrypt_mode, $iv), "\0");;
    return $decrypted;
}
Run Code Online (Sandbox Code Playgroud)

谢谢

Vol*_*erK 9

如果要在C#应用程序中使用Rijndael256,则必须将BlockSize设置为256.

RijndaelManaged rj = new RijndaelManaged();
rj.BlockSize = 256;
Run Code Online (Sandbox Code Playgroud)

然后你的iv也必须是256位长.
请参阅SymmetricAlgorithm.BlockSize属性


或者反过来说:目前你的C#应用​​程序使用Rijndael128,所以必须使用你的php脚本.

<?php
class Foo {
  protected $mcrypt_cipher = MCRYPT_RIJNDAEL_128;
  protected $mcrypt_mode = MCRYPT_MODE_CBC;

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



$encrypted = "UmzUCnAzThH0nMkIuMisqg==";
$key = "qwertyuiopasdfghjklzxcvbnmqwerty";
$iv = "1234567890123456";

$foo = new Foo;
echo $foo->decrypt($key, $iv, $encrypted);
Run Code Online (Sandbox Code Playgroud)

版画 hello world

  • 我知道这不是你的错,但IV应该是随机的.如果没有,它就会失败. (3认同)