且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Rijndael 256加密/解密c#和php?

更新时间:2023-02-17 16:18:22

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

If you want to use Rijndael256 in your C# application you have to set the BlockSize to 256.

RijndaelManaged rj = new RijndaelManaged();
rj.BlockSize = 256;

然后你的iv必须是256位长。

see SymmetricAlgorithm.BlockSize属性

And then your iv has to be 256 bits long as well.
see SymmetricAlgorithm.BlockSize Property

或者另一种方式:目前您的C#应用​​程序使用Rijndael128,因此您的php脚本。

Or the other way round: Currently your C# application uses Rijndael128 and so must your php script.

<?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);

打印 hello world