laravel 4.2使用加密列进行查询

Bou*_*ady 5 php encryption cryptography laravel laravel-4

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

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'));
}
Run Code Online (Sandbox Code Playgroud)

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

Sco*_*ski 14

索引加密数据

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

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


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

弱密码学

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

$sptkey = md5('sample_encryptkey');
Run Code Online (Sandbox Code Playgroud)

如果您想在应用程序中使用一点安全性,请不要使用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
);
Run Code Online (Sandbox Code Playgroud)

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

$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
    )
);
Run Code Online (Sandbox Code Playgroud)

进一步阅读具体问题:

该怎么做(选择一个):