繁体   English   中英

laravel 4.2使用加密列进行查询

[英]laravel 4.2 queries with an encrypted column

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

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将传递给视图时它已经包含解密的记录。 有任何想法吗? 感谢任何愿意提供帮助的人

索引加密数据

如果您需要快速有效地搜索SQL数据库中的加密列 ,则需要构建数据的盲目索引(即在另一列中存储hash_hmac('sha256', $plaintext, $separate_key_here) )并构建您的选择基于此的查询。 (链接的文章解释了安全要求。)

这样可以避免必须执行foreach()循环,但是,由于使用了HMAC-SHA256,访问数据库的攻击者很可能无法将明文取出系统。


也就是说,还有一些我想解决的问题:

弱密码学

请不要使用您问题中包含的加密代码。 这是非常不安全的。 Laravel有自己的加密类 ; 请改用它。 它包含了你所包含的代码片段没有做的很多事情。 例如:它提供经过身份验证的加密

$sptkey = md5('sample_encryptkey');

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

  • md5()返回一个32-char十六进制字符串
  • 大多数加密函数都需要原始二进制字符串
  • MD5是一个令人难以置信的破解哈希函数
  • 要变换密码,加密密钥,您需要使用密钥导出函数,即,p assword- ASEDķEY d erivation˚F结#2 SHA256(PBKDF2-SHA256)。

例如,考虑这个代码:

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
);

我在这里扩展了空白,并在下面留下了一些内联注释:

$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
    )
);

进一步阅读具体问题:

该怎么做(选择一个):

原来我有一个小的修修补补和帮助若昂·门德斯我有这样的代码

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'));
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM