Php 带有加密列的laravel 4.2查询
我目前在我的控制器中有这个代码,它显示一组记录。这里是我的代码Php 带有加密列的laravel 4.2查询,php,laravel,encryption,laravel-4,cryptography,Php,Laravel,Encryption,Laravel 4,Cryptography,我目前在我的控制器中有这个代码,它显示一组记录。这里是我的代码 public function view() { $title = "View Guardian Information"; $vPa = DB::table('dbo_guardianinformation') ->join('dbo_cities', 'dbo_guardianinformation.CityID', '=' , 'dbo_cities.CityID')
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将传递给视图时,它已经包含了解密的记录。有什么想法吗?多亏了那些愿意帮忙的人我才发现,在他们的帮助下,我有了这样的代码
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'));
}
为加密数据编制索引
如果您需要快速高效地构建数据的盲索引,即将hash_hmac'sha256'、$plaintext、$separate_key_存储在一个附加列中,并在此基础上构造select查询。链接文章解释了安全要求
这使您不必执行foreach循环,但由于使用了HMAC-SHA256,因此访问数据库的攻击者不太可能从系统中挑出明文
也就是说,我还想提出一些其他问题:
弱加密
请不要使用问题中包含的加密代码。这很不安全;请改用那个。它做了很多您包含的代码片段没有做的事情。例如:它提供
如果您想在应用程序中增加一点安全性,请不要使用md5$string生成密钥。这只是一个坏主意:
md5返回一个32字符的十六进制字符串
大多数加密函数都需要原始二进制字符串
MD5是一个
要将密码转换为加密密钥,需要使用密钥派生函数,即基于密码的密钥派生函数2和SHA-256 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
)
);
关于具体问题的进一步解读:
选择一种替代方法:
使用,因为您已经在使用Laravel。
强烈推荐使用
使用
当点击1.0.0时,切换到它对新手来说基本上是免费的
在我看来,您已经注释掉了解密代码,在我认为有意义的地方,也就是说,在您填写$vPa之后但在您构建视图之前。你有没有试着取消对代码的注释并使其正常工作?我做这件事时遇到了问题。我在想一种方法,因为dbo_guardianinformation.Address列包含加密数据,所以我试图循环解密所有记录,但没有成功OK,酷。那么,你能把这个代码添加到问题中吗?我用foreach得到了答案:谢谢你的建议!:
$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
)
);