3

关于 SO 已经有一些有用的问题:

但是,我仍然对我的特殊情况有困难。

我尝试了各种方法,但最终得到的错误"The IV parameter must be as long as the blocksize"或文本与生成的哈希不匹配。

我对加密的了解不足以弄清楚我做错了什么。

这是php版本:

$pass = 'hello';
$salt = 'application-salt';

echo Encrypt('hello', 'application-salt');

function Encrypt($pass, $salt)
{
    $derived = PBKDF1($pass, $salt, 100, 16);
    $key = bin2hex(substr($derived, 0, 8));
    $iv = bin2hex(substr($derived, 8, 8));
    return mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $pass, MCRYPT_MODE_CBC, $iv);
}

function PBKDF1($pass, $salt, $count, $dklen)
{
    $t = $pass.$salt;
    $t = sha1($t, true);
    for($i=2; $i <= $count; $i++)
    {
        $t = sha1($t, true);
    }
    $t = substr($t,0,$dklen-1);
    return $t;
}

和 C# 版本:

Console.WriteLine(Encrypt("hello", "application-salt"));
// output: "Hk4he+qKGsO5BcL2HDtbkA=="

public static string Encrypt(string clearText, string Password)
{
    byte[] clearData = System.Text.Encoding.Unicode.GetBytes(clearText);
    PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,
        new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });

    MemoryStream ms = new MemoryStream();
    Rijndael alg = Rijndael.Create();
    alg.Key = pdb.GetBytes(32);
    alg.IV = pdb.GetBytes(16);
    CryptoStream cs = new CryptoStream(ms, alg.CreateEncryptor(), CryptoStreamMode.Write);
    cs.Write(clearData, 0, clearData.Length);
    cs.Close();
    byte[] encryptedData = ms.ToArray();

    return Convert.ToBase64String(encryptedData);
}

我希望能够在一个新的基于 php 的应用程序中验证用户登录,该应用程序将与现有 C# 应用程序相同的 MySQL 数据库进行通信。我打算加密密码并将生成的哈希与存储在数据库中的哈希进行比较以进行身份​​验证。

任何指针将不胜感激。

编辑:

我意识到在 C# 函数中,PasswordDeriveBytes正在调用并传递一个字节数组作为参数,我在 PHP 版本中没有类似的参数。我发现这源于一个Codeproject 示例,并且 ASCII 中的字节数组拼写为“Ivan Medvedev”,我认为他是示例作者。不幸的是,我无法改变这一点。

4

3 回答 3

3

下面的片段可能对正在寻找从 C# 到 PHP 的精确转换的人有所帮助

<?php
    class Foo {
      protected $mcrypt_cipher = MCRYPT_RIJNDAEL_128;
      protected $mcrypt_mode = MCRYPT_MODE_CBC;

      public function decrypt($key, $iv, $encrypted)
      {
        return mcrypt_decrypt($this->mcrypt_cipher, $key, base64_decode($encrypted), $this->mcrypt_mode, $iv);
      }

      public function encrypt($key, $iv, $password)
      {
        $block = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
          $padding = $block - (strlen($password) % $block);
          $password .= str_repeat(chr($padding), $padding);
        return mcrypt_encrypt($this->mcrypt_cipher, $key, $password, $this->mcrypt_mode, $iv);
      }
    }
    $foo = new Foo;
    $pass = 'p@ss';
    $salt = 's@1t';

    $key = PBKDF1($pass, $salt, 2, 32);

    $iv = "@1B2c3D4e5F6g7H8";

    $encrypted = $foo->encrypt($key,$iv,'test@123');

    $encrypted = base64_encode($encrypted);

    echo 'Encrypted: '.$encrypted.'</br>';
    echo 'Decrypted: '.$foo->decrypt($key, $iv, $encrypted);



    function PBKDF1($pass, $salt, $count, $cb)
    {
      static $base;
      static $extra;
      static $extracount= 0;
      static $hashno;
      static $state = 0;

      if ($state == 0)
      {
        $hashno = 0;
        $state = 1;

        $key = $pass . $salt;
        $base = sha1($key, true);
        for($i = 2; $i < $count; $i++)
        {
          $base = sha1($base, true);
        }
      }

      $result = "";

      if ($extracount > 0)
      {
        $rlen = strlen($extra) - $extracount;
        if ($rlen >= $cb)
        {
          $result = substr($extra, $extracount, $cb);
          if ($rlen > $cb)
          {
            $extracount += $cb;
          }
          else
          {
            $extra = null;
            $extracount = 0;
          }
          return $result;
        }
        $result = substr($extra, $rlen, $rlen);
      }

      $current = "";
      $clen = 0;
      $remain = $cb - strlen($result);
      while ($remain > $clen)
      {
        if ($hashno == 0)
        {
          $current = sha1($base, true);
        }
        else if ($hashno < 1000)
        {
          $n = sprintf("%d", $hashno);
          $tmp = $n . $base;
          $current .= sha1($tmp, true);
        }
        $hashno++;
        $clen = strlen($current);     
      }

      // $current now holds at least as many bytes as we need
      $result .= substr($current, 0, $remain);

      // Save any left over bytes for any future requests
      if ($clen > $remain)
      {
        $extra = $current;
        $extracount = $remain;
      }

      return $result; 
    }
于 2014-04-07T18:46:50.930 回答
2

我认为 PHP 版本实际上可能会在密钥和 IV 中添加 00h 值字节。它们都有一个无效的大小:每个 8 个字节。对于 AES-128,它们需要扩展到 16 个字节。在您的 C# 代码中,您使用 32 字节作为密钥,因此将使用密钥大小为 256 位的 AES。

此外,您没有在 中指定迭代次数PasswordDeriveBytes,您应该指定它,因为该类没有指定默认的迭代次数 - 根据您的评论,这将是 100,假设它是。

哦,你使用了不正确的加密方法。MCRYPT_RIJNDAEL_256 指定 Rijndael 算法使用 256 位的块大小,而不是256 位的密钥。据推测,密钥的位大小只是密钥的字节数乘以 8。

你能用这个替换你的加密功能,然后再试一次吗?

function Encrypt($pass, $salt)
{
     $derived = PBKDF1($pass, $salt, 100, 48);
     $key = bin2hex(substr($derived, 0, 32));
     $iv = bin2hex(substr($derived, 32, 16));
     return mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $pass, MCRYPT_MODE_CBC, $iv);
}

最后,请检查生成的IV和密钥是否与PHP中的匹配,然后再进行加密或解密。你确定那个 PHP PBKDF1 函数是正确的吗?

更新: 以下是有关 PasswordDeriveBytes 中 M$ PBKDF1 例程的更多信息(包括您可以尝试转换的 Java 代码):

哈,我明白你的意思了。

有趣的是,使用 .NET:调用 48 或调用 32 后跟 16 时结果不同:

.NET GetBytes(32 +16):04DD9D139DCB9DE889946D3662B319682159FF9C9B47FA15ED205C7CAF890922655D8DD89AE1CAAC60A8041FCD7E8DA4

.NET GetBytes(32) 04DD9D139DCB9DE889946D3662B319682159FF9C9B47FA15ED205C7CAF890922 后跟GetBytes(16) 89946D3662B3196860A8041FCD7E8DA4

真正的 Microsoft 代码,他们无法更改它,因为它可能会破坏该领域的应用程序。请注意,当使用 16 字节然后 8 字节或直接按 24 字节调用时,它们也会返回不同的结果。您最好升级到 PBKDF2,并将 PBKDF1 限制在标准中定义的最大 20 字节。

于 2012-01-26T00:53:26.983 回答
-1

PHP 已经在其 Mcrypt 模块中内置了此功能。

试试这个:http ://www.php.net/manual/en/book.mcrypt.php

于 2012-01-25T22:58:23.537 回答