编程语言
首页 > 编程语言> > Rijndael 256在c#和php之间加密/解密?

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

作者:互联网

更新

我已对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;
    }

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;
}

谢谢

解决方法:

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

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

然后你的iv也必须是256位长.
SymmetricAlgorithm.BlockSize Property

或者反过来说:目前你的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);

打印你好世界

标签:rijndael,mcrypt,php,encryption,c-2
来源: https://codeday.me/bug/20190923/1813723.html