【问题标题】:Rijndael 256 Encrypt/decrypt between c# and php?Rijndael 256 c# 和 php 之间的加密/解密?
【发布时间】:2011-03-26 19:22:33
【问题描述】:

更新

我已经对 C# 代码进行了更改,因此它使用了 256 的块大小。但是现在 hello world 看起来像这样 http://pastebin.com/5sXhMV11 我不知道应该使用 rtrim() 来获得最后一团糟。

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

再次感谢!

嗨,

我正在尝试使用 PHP 解密在 C# 中加密的字符串。我似乎无法让 PHP 使用 mcrypt 对其进行解密,请提供一些帮助。我收到以下 php 错误,所以我猜我没有正确设置 IV。

错误:IV参数必须和blocksize一样长

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

来自 c# 的加密文本 = UmzUCnAzThH0nMkIuMisqg==
密钥 32 长 = qwertyuiopasdfghjklzxcvbnmqwerty
iv 16 长 = 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;
}

谢谢

【问题讨论】:

  • IV 真的应该是随机的。如果没有,它就违背了拥有一个的目的。
  • 具有 256 位块的 Rijndael 是非标准的。

标签: c# php encryption mcrypt rijndael


【解决方案1】:

如果您想在您的 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);

打印hello world

【讨论】:

【解决方案2】:

使用 PHP 加密;

/Generate public key for encrytion
$path = "keys/";

    $crt = openssl_x509_read(file_get_contents($path."cert.crt"));
    $publickey = openssl_get_publickey($crt);

    //Encrypt using public key
    openssl_public_encrypt($source, $crypted, $publickey);

    //openssl_private_encrypt($source, $crypted, $privkey);
    echo base64_encode($crypted);

使用 C# 解密

    X509Certificate2 x509cert = new X509Certificate2(pKeyFilename);
    RSACryptoServiceProvider.UseMachineKeyStore = false;
    RSACryptoServiceProvider crypt = (RSACryptoServiceProvider)x509cert.PrivateKey;                

    byte[] decrypted = crypt.Decrypt(Convert.FromBase64String(data), false);
    return ASCIIEncoding.UTF8.GetString(decrypted);

其中 pKeyFilename 是使用证书文件 cert.crt 创建的个人信息交换文件。此示例使用 AES-256 加密。

【讨论】:

    猜你喜欢
    • 2012-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-14
    • 1970-01-01
    • 1970-01-01
    • 2011-03-31
    • 2012-05-02
    相关资源
    最近更新 更多