2016-02-05 92 views
-2

我有一個關於我的代碼的問題。問題是,當我說echo $ collumB比他顯示student_city。這是在我的數據庫,但我希望它顯示解密的密碼。它只是顯示了錯誤的數據顯示錯誤的數據Decrypy PDO/PHP

(存在另一個網頁,我對密碼進行加密,但我需要解密的密碼echo'ed

<html> 
<head> 
    <title>insert data in database using PDO(php data object)</title> 
    <link rel="stylesheet" type="text/css" href="style-login.css"> 
</head> 
<body> 

    <div id="main"> 
     <h1>Login using PDO</h1> 
    <div id="login"> 
     <h2>Login</h2> 
     <hr/> 
     <form action="" method="post"> 
      <label>Email :</label> 
      <input type="email" name="stu_email" id="email" required="required" placeholder="[email protected]"/><br/><br /> 
      <label>Password :</label> 
      <input type="password" name="stu_ww" id="ww" required="required" placeholder="Please Enter Your Password"/><br/><br /> 
      <input type="submit" value=" Submit " name="submit"/><br /> 
     </form> 
    </div> 

    </div> 

    <?php 
    //require ("encrypt.php"); 
     if(isset($_POST["submit"])){ 
      $hostname='localhost'; 
      $username='root'; 
      $password=''; 
      $pdo = "college"; 
      $student_email = $_POST["stu_email"]; 
      $encrypt_key = "4ldetn43t4aed0ho10smhd1l"; 

      try { 
       $dbh = new PDO("mysql:host=$hostname;dbname=college","root","$password");     
       $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

        // Query 
        $statement = $dbh->prepare("SELECT student_email, student_city, AES_DECRYPT(student_password, '$encrypt_key') 
         AS student_password FROM students WHERE student_email = :student_email ORDER BY student_email ASC"); 

        // Assign and execute query 
        $statement->bindParam(':student_email', $student_email, PDO::PARAM_STR); 
         $statement->setFetchMode(PDO::FETCH_ASSOC); 
          $statement->execute(); 

        // Get data 
         while($row = $statement->fetch()) { 
          echo "1 ,";         
          //$columnA_value = $row['student_city']; 
          $columnB_value = $row['student_password']; 
         } 
         echo "2 ,"; 
         echo $columnB_value; 
      } 

       catch(PDOException $e) 
       { 
        echo $e->getMessage(); 
       } 

     } 
    ?> 
</body> 
</html> 
+2

看來你有沒有STO紅色的加密密碼在數據庫中,這是非常危險的。如果你的數據庫表被黑客攻擊,那麼他可以很容易地訪問你的網站。我強烈建議您通過使用「md5」算法進行加密來存儲您的密碼。請檢查[此鏈接](http://stackoverflow.com/questions/5089841/two-way-encryption-i-need-to-store-passwords-that-c​​an-be-retrieved)如何在php中加密/解密。 – PHPExpert

+0

@PHPExpert [如果用於密碼,MD5是一個非常糟糕的主意。](http://security.stackexchange.com/q/19906/45523) –

+0

您不應該加密用戶的密碼。你需要使用哈希,而不是一些強大的PBKDF2,bcrypt,scrypt和Argon2。由於散列函數是單向函數,因此您將無法「解密」散列。爲了驗證您的用戶,您可以再次通過散列函數運行密碼,以便與存儲在數據庫中的散列進行比較。查看更多:[如何安全地哈希密碼?](http://security.stackexchange.com/q/211/45523) –

回答

0
SELECT student_email, student_city, CAST(AES_DECRYPT(student_password, '$encrypt_key') AS char(50)) AS student_password FROM students WHERE student_email = :student_email ORDER BY student_email ASC; 

儘量明確其轉換爲字符串。你可以根據您的要求修改「50」。

而且你的迴音外,同時循環,因此它會只打印,如果有超過10條記錄最後一條記錄。