Rijndael 256加密/解密c#和php? [英] Rijndael 256 Encrypt/decrypt between c# and php?

查看:136
本文介绍了Rijndael 256加密/解密c#和php?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UPDATED

我已经对C#代码进行了更改,所以它使用的块大小为256,但现在你好,世界看起来像这样的 http://pastebin.com/5sXhMV11 ,我无法弄清楚我应该用rtrim()到得到最后的混乱。

I have made the changes to the C# code so it uses a block size of 256. but now the hello world looks like this http://pastebin.com/5sXhMV11 and I cant figure out what I should use with rtrim() to get ride of the mess at the end.

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

Also when you say the IV should be random, by this do you mean don't use the same IV more then once or is the way I have coded it wrong?

再次感谢

我试图使用C#中加密的PHP解密字符串。我似乎无法让PHP使用mcrypt进行解密,可以做一些帮助。我得到以下错误与PHP,所以我猜我没有正确设置IV。

I'm trying to decrypt a string with PHP that was encrypted in C#. I can't seem to get PHP to decrypt it using mcrypt and could do with some help please. I get the following error with php so I am guessing I'm not setting the IV correctly.

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

Error: The IV parameter must be as long as the blocksize

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

Both functions use the same cipher, key, IV and set to CBC mode:

加密文本来自c#= UmzUCnAzThH0nMkIuMisqg ==

key 32 long = qwertyuiopasdfghjklzxcvbnmqwerty

iv 16 long = 1234567890123456

encrypted text from 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。

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

这篇关于Rijndael 256加密/解密c#和php?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆