【问题标题】:laravel 4.2 queries with an encrypted column带有加密列的 laravel 4.2 查询
【发布时间】:2015-12-31 16:15:57
【问题描述】:

我目前在我的控制器中有这段代码,它显示一组记录这里是我的代码

public function view()
{
    $title = "View Guardian Information";
    $vPa   = DB::table('dbo_guardianinformation')
                ->join('dbo_cities', 'dbo_guardianinformation.CityID', '=' , 'dbo_cities.CityID')
                ->select('dbo_guardianinformation.ParentAccountID','dbo_guardianinformation.FirstName','dbo_guardianinformation.LastName','dbo_guardianinformation.Roles',
                        'dbo_guardianinformation.Address','dbo_cities.CityName','dbo_guardianinformation.Status','dbo_guardianinformation.EmailAddress')
                ->get();
     //encrypt decrypt algo
    // $sptkey  = md5('sample_encryptkey');
    // $enPass  = rtrim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $sptkey, $defPass, MCRYPT_MODE_ECB)));
    // $decPass = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $sptkey, base64_decode($enPass), MCRYPT_MODE_ECB));

    return View::make('ssims.view_parentAccount',compact('title','vPa'));
}

我的问题是列dbo_guardianinformation.Address包含加密记录我目前不知道我应该将解密代码放在哪里,以便当$vPa 将被传递到它已经包含解密记录的视图时。有任何想法吗?感谢任何愿意提供帮助的人

【问题讨论】:

  • 在我看来,您已经在我认为有意义的地方注释掉了解密代码,即在您填写 $vPa 之后但在您构建视图之前。您是否尝试取消注释该代码并使其正常工作?
  • 我这样做时遇到了问题。我想了一个办法,因为dbo_guardianinformation.Address 列包含加密数据,我试图创建一个循环来解密所有没有成功的记录
  • 好的,很酷。那么,您可以在问题中添加那个代码吗?
  • 我通过使用 foreach 得到了答案 :) 感谢您的建议! :)

标签: php laravel encryption laravel-4 cryptography


【解决方案1】:

索引加密数据

如果您需要快速有效地search an encrypted column in a SQL database,您需要构建数据的盲索引(即将hash_hmac('sha256', $plaintext, $separate_key_here) 存储在附加列中)并基于此构建您的选择查询。 (链接的文章解释了安全要求。)

这使您不必执行foreach() 循环,但由于使用了 HMAC-SHA256,因此具有数据库访问权限的攻击者不太可能将明文从系统中提取出来。


也就是说,我还想谈一谈:

弱密码学

请不要使用您在问题中包含的加密代码。这是非常不安全的。 Laravel has its own encryption class;请改用它。它做了很多你包含的代码 sn-p 没有做的事情。例如:提供authenticated encryption

$sptkey = md5('sample_encryptkey');

如果您希望应用程序具有一点安全性,请永远不要使用md5($string) 来生成密钥。这只是一个坏主意:

  • md5() 返回一个 32 字符的十六进制字符串
  • 大多数加密函数都需要原始二进制字符串
  • MD5 是incredibly broken hash function
  • 要将密码转换为加密密钥,需要使用密钥派生函数,即Password-Based Key D派生F使用 SHA-256 (PBKDF2-SHA256) 的功能 #2。

例如,请考虑以下代码:

define('MY_APP_PBKDF2_ITERATIONS', 86000);
define('MY_APP_KEY_LENGTH', 32); // or 16 for AES-128
// ...
$sptkey = hash_pbkdf2(
    'sha256',
    $your_password,
    $salt, // 32 bytes from /dev/urandom
    MY_APP_PBKDF2_ITERATIONS,
    MY_APP_KEY_LENGTH,
    true
);

我在这里扩展了空格,并在下面留下了一些 inline-cmets:

$enPass = rtrim(                 // Unnecessary, base64_encode doesn't leave whitespace
    base64_encode(
        mcrypt_encrypt(
            MCRYPT_RIJNDAEL_256, // This isn't AES-256 by the way
            $sptkey,
            $defPass,
            MCRYPT_MODE_ECB      // ECB mode is the worst mode
        )
    )
);
$decPass = rtrim(               // Padding oracle attack
    mcrypt_decrypt(
        MCRYPT_RIJNDAEL_256,
        $sptkey,
        base64_decode($enPass), // No error checking
        MCRYPT_MODE_ECB
    )
);

进一步阅读具体问题:

该怎么做(选择一个):

【讨论】:

    【解决方案2】:

    João Mendes 的帮助下,我得到了一些修改和帮助,我得到了这样的代码

    public function view()
    {
        $title = "View Guardian Information";
        $vPa   = DB::table('dbo_guardianinformation')
                    ->join('dbo_cities', 'dbo_guardianinformation.CityID', '=' , 'dbo_cities.CityID')
                    ->select('dbo_guardianinformation.ParentAccountID','dbo_guardianinformation.FirstName','dbo_guardianinformation.LastName','dbo_guardianinformation.Roles',
                            'dbo_guardianinformation.Address','dbo_cities.CityName','dbo_guardianinformation.Status','dbo_guardianinformation.EmailAddress')
                    ->get();
    
        foreach ($vPa as $key => $dvPa) 
        {
            $sptkey  = md5('this is secret');
            $enAdd = $dvPa->Address;
            $decAdd = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $sptkey, base64_decode($enAdd), MCRYPT_MODE_ECB));
    
            $dvPa->Address = $decAdd;   
        }
        return View::make('ssims.view_parentAccount',compact('title','vPa'));
    }
    

    【讨论】:

      猜你喜欢
      • 2015-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-29
      相关资源
      最近更新 更多