Note that there are some explanatory texts on larger screens.

plurals
  1. POEncrypt safe password in the mysql database using encryption function
    primarykey
    data
    text
    <p>I want to encrypt password to prevent SQL injection and other attacks and I used an encryption function in PHP, but I don't know how to use it to encrypt passwords. </p> <p>I want each password in the user table to be encrypted so i can connect to the database and use a query but it doesn't work.</p> <p>Can anyone help me please?</p> <h1>ecnription.php</h1> <pre><code>&lt;?php require_once('include/connect.php'); if(isset($_SESSION['user_id'])) { $id= $_SESSION['user_id']; } $sql = mysql_query("SELECT password FROM user WHERE user_id= '$id'")or die(mysql_error()); while($row = mysql_fetch_array($sql)) { $enc_pass= $row['password']; } error_reporting(0); class Encryption { const CYPHER = MCRYPT_RIJNDAEL_256; const MODE = MCRYPT_MODE_CBC; const KEY = 'somesecretphrase'; public function encrypt($plaintext) { $td = mcrypt_module_open(self::CYPHER, '', self::MODE, ''); $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND); mcrypt_generic_init($td, self::KEY, $iv); $crypttext = mcrypt_generic($td, $plaintext); mcrypt_generic_deinit($td); return base64_encode($iv.$crypttext); } public function decrypt($crypttext) { $crypttext = base64_decode($crypttext); $plaintext = ''; $td = mcrypt_module_open(self::CYPHER, '', self::MODE, ''); $ivsize = mcrypt_enc_get_iv_size($td); $iv = substr($crypttext, 0, $ivsize); $crypttext = substr($crypttext, $ivsize); if ($iv) { mcrypt_generic_init($td, self::KEY, $iv); $plaintext = mdecrypt_generic($td, $crypttext); } return trim($plaintext); } } $encrypted_string = Encryption::encrypt($enc_pass); $decrypted_string = Encryption::decrypt($encrypted_string); echo $encrypted_string . "&lt;br&gt;" . PHP_EOL; var_dump($id); echo $decrypted_string . "&lt;br&gt;" . PHP_EOL; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload