Jump to content

MYSQL Password encryption and decryption


Foser

Recommended Posts

Well the best way to encrypt a password is through md5 and sessions are quite tricky to get used to so I would look at: Sessions

 

So with the md5 you would want your password to be something like this:

 

<?php
      $password = $_POST['password'];
      $password = md5($password);
?>
<html>
<body>
<form name="password" action="<?PHP_SELF?>" method="post">
<input type="password" name="password" value="$password">
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
<?php
// Insert assuming you already connected to the database
	$query = "INSERT INTO tablename (fieldname)
	VALUES('$password')";
	mysql_query($query) or die(mysql_error());
?>

 

And then for the login have:

 

<?php

$username = $_POST['username'];
$password = $_POST['password'];
$password = md5($password);
// Check if combo is in database
$query = mysql_query("SELECT fieldname FROM tablename WHERE fieldname='$username' && fieldname='$password'");
$result = mysql_num_rows($query);

if($result > 0){
echo ("Congrats");
} else {
echo ("Invalid");
}
?>
<html>
<body>
<form name="login" action="<?PHP_SELF?>" method="post">
<input type="username" name="username" value="$username">
<input type="password" name="password" value="$password">
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>

 

This code is completly untested and probably has some faults, but try it and report back

 

Hope it helps  ;D

 

~ Chocopi

I'm looking for a slight tutorial about how to encrypt a password in mysql and decrypt it so when you want to login it's simple.

 

There is no need to decrypt again. Use a one-way hashing algorithm such as SHA-1 or MD5. Then when the user enter their password to login again they you would just encrypt the entered password and compare it with the already encrypted password stored in the database.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.