3

(前言:我是加密和安全的新手,认为这是一种有趣的学习方式)

我正在用 C# 构建一个程序,该程序使用标准 HTTP 协议与用 PHP 编写的服务器进行通信。我希望这两个程序都能够发送和接收加密数据。但是,尽管它们都使用相同类型的功能,但在如何处理加密方面似乎存在不一致之处。

这两个程序都在 CBC 模式下使用 Rjindael 128 位。

为了演示/测试,我制作了两个几乎相同的函数,每个函数都采用相同的字符串,对其进行加密,然后将结果作为 base64 字符串输出。

PHP函数:

public static function EncryptionTest () {
  echo 'Testing Encryption to base64 string...<br/>';

  $originalString = 'This is the original String! How cool is that?';

  $key = "abcdefghijklmnopqrstuvwxyz012345";
  $iv = "1234567890123456";

  $encrypted = mcrypt_encrypt (MCRYPT_RIJNDAEL_128, $key, $originalString, MCRYPT_MODE_CBC, $iv);

  echo base64_encode ($encrypted);
}

它是 C# 对应物:

public static void EncryptionTest ()
{
  System.Console.WriteLine ("Testing Encryption to base64 string...");

  string originalString = "This is the original String! How cool is that?";
  byte [] encryptedData;
  byte [] key = System.Text.ASCIIEncoding.UTF8.GetBytes ("abcdefghijklmnopqrstuvwxyz012345");
  byte [] iv = System.Text.ASCIIEncoding.UTF8.GetBytes ("1234567890123456");

  RijndaelManaged cryptor = new RijndaelManaged ();
  cryptor.Key = key;
  cryptor.IV = iv;
  cryptor.BlockSize = 128;
  cryptor.Mode = CipherMode.CBC;

  ICryptoTransform encryptor = cryptor.CreateEncryptor (cryptor.Key, cryptor.IV);

  using (MemoryStream msEncrypt = new MemoryStream ())
  {
    using (CryptoStream csEncrypt = new CryptoStream (msEncrypt, encryptor, CryptoStreamMode.Write))
    {
      using (StreamWriter swEncrypt = new StreamWriter (csEncrypt) )
      {
        swEncrypt.Write (originalString);
      }
      encryptedData = msEncrypt.ToArray ();
    }
  }

  System.Console.WriteLine (System.Convert.ToBase64String (encryptedData) );
}

现在,这是 PHP 结果:

测试对 base64 字符串的加密... yzwIdowhLj+cMOFPMuHSA80pWQ6R8yfFQlEsLx5kIzUOJdFykLjsaKfK4VfaBGRv

C# 结果:

测试对 base64 字符串的加密... yzwIdowhLj+cMOFPMuHSA80pWQ6R8yfFQlEsLx5kIzUg74mGEQf9iW+OQ68m6cpp

如您所见,这两个结果显然是加密的(好),字符数相同(好),但不同(可能不好)。

我编写了解密测试来获取这些字符串并对其进行处理。这是PHP函数:

public static function DecryptionTest () {
  $phpBase64 = 'yzwIdowhLj+cMOFPMuHSA80pWQ6R8yfFQlEsLx5kIzUOJdFykLjsaKfK4VfaBGRv';
  $csBase64 =  'yzwIdowhLj+cMOFPMuHSA80pWQ6R8yfFQlEsLx5kIzUg74mGEQf9iW+OQ68m6cpp';

  $key = "abcdefghijklmnopqrstuvwxyz012345";
  $iv = "1234567890123456";

  $phpEncrypted = base64_decode ($phpBase64);
  $phpDecrypted = mcrypt_decrypt (MCRYPT_RIJNDAEL_128, $key, $phpEncrypted, MCRYPT_MODE_CBC, $iv);

  $csEncrypted = base64_decode ($csBase64);
  $csDecrypted = mcrypt_decrypt (MCRYPT_RIJNDAEL_128, $key, $csEncrypted, MCRYPT_MODE_CBC, $iv);

  echo 'Decrypted PHP string: "' . $phpDecrypted . '"<br/>' .
       'Decrypted CS string:  "' . $csDecrypted . '"';
}

和 C# 版本:

public static void DecryptionTest ()
{
  System.Console.WriteLine ("Testing Decryption for PHP and CS generated base64 strings!");

  string phpBase64 = "yzwIdowhLj+cMOFPMuHSA80pWQ6R8yfFQlEsLx5kIzUOJdFykLjsaKfK4VfaBGRv";
  string csBase64 = "yzwIdowhLj+cMOFPMuHSA80pWQ6R8yfFQlEsLx5kIzUg74mGEQf9iW+OQ68m6cpp";

  byte [] phpEncrypted = System.Convert.FromBase64String (phpBase64);
  byte [] csEncrypted = System.Convert.FromBase64String (csBase64);

  string phpDecrypted;
  string csDecrypted;

  byte [] encryptedData;
  byte [] key = System.Text.ASCIIEncoding.UTF8.GetBytes ("abcdefghijklmnopqrstuvwxyz012345");
  byte [] iv = System.Text.ASCIIEncoding.UTF8.GetBytes ("1234567890123456");

  RijndaelManaged cryptor = new RijndaelManaged ();
  cryptor.Key = key;
  cryptor.IV = iv;
  cryptor.BlockSize = 128;
  cryptor.Mode = CipherMode.CBC;

  ICryptoTransform decryptor = cryptor.CreateDecryptor (cryptor.Key, cryptor.IV);

  using (MemoryStream msDecrypt = new MemoryStream (csEncrypted))
  {
    using (CryptoStream csDecrypt = new CryptoStream (msDecrypt, decryptor, CryptoStreamMode.Read))
    {
      using (StreamReader swDecrypt = new StreamReader (csDecrypt) )
      {
        csDecrypted = swDecrypt.ReadToEnd ();
      }
    }
  }
  System.Console.WriteLine ("Decrypted CS string: \"" + csDecrypted + "\"");

  using (MemoryStream msDecrypt = new MemoryStream (phpEncrypted))
  {
    using (CryptoStream csDecrypt = new CryptoStream (msDecrypt, decryptor, CryptoStreamMode.Read))
    {
      using (StreamReader swDecrypt = new StreamReader (csDecrypt) )
      {
        phpDecrypted = swDecrypt.ReadToEnd ();
      }
    }
  }

  System.Console.WriteLine ("Decrypted PHP string: \"" + phpDecrypted + "\"");      
}

PHP结果:

解密的 PHP 字符串:“这是原始字符串!这有多酷?”

解密的 CS 字符串:“这是原始字符串!这有多酷?”

C# 结果:

解密的 CS 字符串:“这是原始字符串!这有多酷?”

CryptographicException:错误的 PKCS7 填充。Mono.Security.Cryptography.SymmetricTransform.ThrowBadPaddingException (PaddingMode padding, Int32 length, Int32 position) [0x0005c] in > /Applications/buildAgent/work/84669f285f6a667f/mcs/class/corlib/Mono.Security.Cryptography/ 中的长度无效 0.对称变换.cs:363

因此,基本上,PHP 代码可以成功解密这两个 base64 字符串,但 C# 代码只能解密由其自己的解密器创建的 base64 字符串。

其中很多是我在互联网上找到的代码,并根据我的需要进行了修改。就像我说的,我是密码学的新手,但我在这里已经走得很远了。我可以整天进行理论和测试,但它开始占用我的日程安排,所以我正在寻找其他人的见解,以了解它为什么不起作用。谢谢!

4

1 回答 1

4

您需要使用PHPSecLib,因为该PHP实现与C++/C#. 它有不同的填充大小(这里不是加密专家,但这是我经过几天的测试后发现的)。因此,使用中的本机实现,phpseclib它将起作用。

前段时间自己碰到了这个问题。但是使用C++/CryptoAPIPHP.

于 2013-06-28T18:59:49.530 回答