10

更新

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

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

再次感谢!

你好,

我正在尝试使用在 C# 中加密的 PHP 解密字符串。我似乎无法让 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;
}

谢谢

4

2 回答 2

10

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

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

然后你的 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);

印刷hello world

于 2010-08-07T21:01:24.347 回答
-1

使用 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 加密。

于 2011-09-09T10:15:37.990 回答