encrypt: this takes a password (a string) and returns the encrypted form of the
ID: 3680445 • Letter: E
Question
encrypt: this takes a password (a string) and returns the encrypted form of the password. Note: there is no decrypt function (there is no need to decrypt passwords). We will use the following VERY simple encryption algorithm (a Caesar Cipher):
For every character in the input string, add 10 to the ascii value of the character. The encrypted character’s ascii value must stay in the range of printable, non- whitespace characters: 33 to 126.
This can be enforced using this formula: ascii value of encrypted char = ((ascii value of ch - 33) + 10) % 94 + 33
Explanation / Answer
Hi below i have written a sample code for to Encrypt Passwords in the Database for your reference,
So, let's start. First of all, you need to add a new account to your database. The following code allows to do it.
<?php
define("DB_SERVER", "localhost");
define("DB_USER", "your_name");
define("DB_PASS", "your_pass");
define("DB_NAME", "your_db");
define("TBL_USERS", "users_table_name");
$connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS) or die(mysql_error());
mysql_select_db(DB_NAME, $connection) or die(mysql_error());
...
function addNewUser($username, $password){
global $connection;
$password = md5($password);
$q = "INSERT INTO ".TBL_USERS." VALUES ('$username', '$password')";
return mysql_query($q, $connection);
}
?>
Now, when a new user completes the registration form, his password will be encrypted automatically.
After that we should write code that validates a given username/password pair.
<?php
function checkUserPass($username, $password){
global $connection;
$username = str_replace("'","''",$username)
$password = md5($password);
// Verify that user is in database
$q = "SELECT password FROM ".TBL_USERS." WHERE username = '$username'";
$result = mysql_query($q, $connection);
if(!$result || (mysql_numrows($result) < 1)){
return 1; //Indicates username failure
}
// Retrieve password from result
$dbarray = mysql_fetch_array($result);
// Validate that password is correct
if($password == $dbarray['password']){
return 0; //Success! Username and password confirmed
}
else{
return 1; //Indicates password failure
}
}
?>
And what if you already have users' database ready and want to start using encrypted passwords? To do it, you need to write encypt.php script with the following code and run it in your browser.
<?php
define("DB_SERVER", "localhost");
define("DB_USER", "your_name");
define("DB_PASS", "your_pass");
define("DB_NAME", "your_db");
define("TBL_USERS", "users_table_name");
define("FLD_USER", "username_field_name");
define("FLD_PASS", "password_field_name");
set_magic_quotes_runtime(0);
$connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS) or die(mysql_error());
mysql_select_db(DB_NAME, $connection) or die(mysql_error());
$q = "SELECT ".FLD_PASS.",".FLD_USER." FROM ".TBL_USERS."";
$result = mysql_query($q, $connection);
$total=0;
$enc=0;
$doencrypt=false;
if (@$_REQUEST["do"]=="encrypt")
$doencrypt=true;
while($data = mysql_fetch_array($result))
{
if ($doencrypt)
{
$total++;
if (!encrypted($data[0]))
{
$q="UPDATE ".TBL_USERS." SET ".FLD_PASS."='".md5($data[0])."' where ".FLD_USER."='".
str_replace("'","''",$data[1])."'";
mysql_query($q, $connection);
}
$enc++;
}
else
{
$total++;
if (encrypted($data[0]))
$enc++;
}
}
function encrypted($str)
{
if (strlen($str)!=32)
return false;
for($i=0;$i<32;$i++)
if ((ord($str[$i])<ord('0') || ord($str[$i])>ord('9')) && (ord($str[$i])<ord('a') || ord($str[$i])>ord('f')))
return false;
return true;
}
?>
<html>
<head><title>Encrypt passwords</title></head>
<body>
Total passwords in the table - <?php echo $total; ?><br>
<?php if($enc==$total && $total>0) { ?>
All passwords are encrypted.
<?php } else if($total>0) { ?>
Unencrypted - <?php echo $total-$enc; ?><br><br>
Click "GO" to encrypt <?php echo $total-$enc; ?> passwords.<br>
WARNING! There will be no way to decipher the passwords.<br>
<input type=button value="GO">
<?php } ?>
</body>
</html>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.