我目前在我的控制器中有这个代码,这里显示一组记录是我的代码
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
传递给视图时它已经包含解密记录。有任何想法吗?感谢任何愿意提供帮助的人
答案 0 :(得分:14)
如果您需要快速有效地search an encrypted column in a SQL database,则需要构建数据的盲目索引(即在另一列中存储hash_hmac('sha256', $plaintext, $separate_key_here)
)并根据该结构构建您的选择查询。 (链接的文章解释了安全要求。)
这使您不必进行foreach()
循环,但是,由于使用了HMAC-SHA256,因此访问数据库的攻击者很可能无法取消明文。系统。
那就是说,我还想谈谈其他事情:
请不要使用您问题中包含的加密代码。这是非常不安全的。 Laravel has its own encryption class;请改用它。它包含了你所包含的代码片段没有做的很多事情。例如:它提供authenticated encryption。
$sptkey = md5('sample_encryptkey');
如果您在应用程序中需要一点安全性,请不要使用md5($string)
生成密钥。这只是一个坏主意:
md5()
返回32个字符的十六进制字符串例如,考虑使用此代码:
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
)
);
进一步阅读具体问题:
该怎么做(选择一个):
答案 1 :(得分:1)
我在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'));
}