我試圖做一個登錄系統,加密和解密數據庫中的密碼(爲我的項目)。我可以使用aes_encrypt加密密碼並將它們存儲在數據庫中。但是,當我稍後解密它們以找到用於登錄的匹配密碼時,它們不起作用。這就像aes_decrypt被跳過,沒有運行,因爲我有帳戶存儲在數據庫中的明文密碼,我可以登錄他們,但對於加密密碼帳戶他們不工作。我使用xampp和phpmyadmin作爲數據庫。mysql aes_encrypt和aes_decrypt
Signup file
<?php
if(isset($_POST['signup']))
{
mysql_connect("localhost","root","");
mysql_select_db("faceback");
$Email=$_POST['email'];
$que1=mysql_query("select * from users where Email='$Email'");
$count1=mysql_num_rows($que1);
if($count1>0)
{
echo "<script>
alert('There is an existing account associated with this email.');
</script>";
}
else
{
$Name=$_POST['first_name'].' '.$_POST['last_name'];
$Password=$_POST['password'];
$Gender=$_POST['sex'];
$Birthday_Date=$_POST['day'].'-'.$_POST['month'].'-'.$_POST['year'];
$FB_Join_Date=$_POST['fb_join_time'];
$day=intval($_POST['day']);
$month=intval($_POST['month']);
$year=intval($_POST['year']);
if(checkdate($month,$day,$year))
{
$que2=mysql_query("insert into
users(Name,Email,Password,Gender,Birthday_Date,FB_Join_Date)
values('$Name','$Email',AES_ENCRYPT('$Password','897sdn9j98u98jk'),
'$Gender','$Birthday_Date','$FB_Join_Date')");
session_start();
$_SESSION['tempfbuser']=$Email;
}
Login file
<?php
if(isset($_POST['Login']))
{
mysql_connect("localhost","root","");
mysql_select_db("faceback");
$user=$_POST['username'];
$pass=$_POST['password'];
$que1=mysql_query("select Email,AES_DECRYPT(Password,'897sdn9j98u98jk') from
users where Email='$user' and Password='$pass'");
$count1=mysql_num_rows($que1);
if($count1>0)
{
session_start();
$_SESSION['tempfbuser']=$user;
}
您的代碼易受SQL注入攻擊。您應該使用[mysqli](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php)或[PDO](http://php.net/manual/en/pdo.prepared- statement.php)按照[本文]中描述的準備語句(http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)。 –
自從PHP v5.5起,不再推薦'mysql_ *'函數,並且從v7.0開始已被刪除。他們不應該用於新的代碼,應該換成[mysqli](http://php.net/manual/en/book.mysqli.php)或[PDO](http://php.net/manual /en/book.pdo.php)儘可能等效。 –
不要使用MySQL的AES加密密碼。相反,使用PHP的['password_hash()'](http://php.net/manual/en/function.password-hash.php)和['password_verify()'](http://php.net/manual/ en/function.password-verify.php)函數。 –