Jump to content

crypt fucntion


westminster86

Recommended Posts

Can someone explain to me why it keeps throwing out this exception in my script. Ive checked that the two form variables im posting have values. If i dont use crypt it works fine.

 

$username = 'smith01';
$password = crypt('black');

try
{
  if($username != $_POST['username'] || $password != $_POST['password'])
    {
      throw new Exception('The administrator username and password is not valid.');
    }

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

 

Link to comment
Share on other sites

its working now. ive justed added salt as the second argument to the crypt function. I thought u dnt need to specify it, doesnt the function create one if u dont. isnt the salt just a string on which to base the encryption on. Why doesnt it work without it? can someone explain to me

 

$adminpassword = crypt('secret', salt);

 

if($adminusername != $_POST['ausername'] || $adminpassword != crypt($_POST['apassword'], salt))

Link to comment
Share on other sites

the second variable is not required. this works fine for me:

 

<?
$adminpassword = crypt('secret');
echo "adminpassword: $adminpassword";
?>

 

your logic may be off.

 

by the way, why are we comparing the posted password to the crypt()'ed password? they will never match that way and you'll always get the exception. crypt() isn't helping anything. i'd leave it out.

Link to comment
Share on other sites

why wouldnt they match? If ive encrypted for example ,

 

$password = crypt('hello');

 

Then  if i was to post a form variable over and encrypt it, and compare it with the stored encrypted password, wouldnt that be fine?. Of course they would match, as long as the user has entered the word hello into the form.

Link to comment
Share on other sites

i guess i'm a little confused. will you be storing the correct password in the script (bad idea) or retrieving it from a database or file? my method would be to compare the crypt() of the password stored in a database with the crypt() of the user-entered password. maybe that's what we're doing with $password = crypt('hello'); at the top of the file; assuming that 'hello' will actually be pulled from a data source.

 

anywho, i found this example that might help. note the comment.

 

<?php
$password = crypt('mypassword'); // let the salt be automatically generated

/* You should pass the entire results of crypt() as the salt for comparing a
   password, to avoid problems when different hashing algorithms are used. (As
   it says above, standard DES-based password hashing uses a 2-character salt,
   but MD5-based hashing uses 12.) */
if (crypt($user_input, $password) == $password) {
   echo "Password verified!";
}
?>

Link to comment
Share on other sites


  $username = 'admin';
  $password = crypt('password123');

  $dbhandle = sqlite_popen("adatabase", 0666, $err_msg);
  if(!$dbhandle) die("Error: Could not connect to database");

  $query = "CREATE TABLE admin(username VARCHAR(50), password VARCHAR(30))";
  if(@!sqlite_query($dbhandle, $query)) { echo "Table users already exists<br />"; }

  $query = sqlite_query($dbhandle, "SELECT * FROM admin WHERE username='$username' AND password='$password'");
  echo $query;

  if (sqlite_num_rows($query)==0) {
  
  $query = "INSERT INTO admin VALUES('$username', '$password')";
  //echo $query;

  if(@!sqlite_query($dbhandle, $query)) { echo "Could not insert table row"; }
  }

Link to comment
Share on other sites

are the user names or passwords in the database encrypted? if so, you'll need to compare the user-entered password by encrypting it using the same salt that was used to encrypt the password in the database. if the password in the database is not encrypted, don't encrypt or decrypt either one before comparing them.

Link to comment
Share on other sites

well yes. at the top of the scipt ive encrypted the password, which i then store in the database. So the second time round it should compare the encrypted password in the database to the encrypted one im trying to enter. I know the logic behind it, i just dnt know how to implment it. I know its something to do with the crypt fucntion because when i dnt use it, it doesnt allow me to enter values that are already in the table.

Link to comment
Share on other sites

okay, then are you following the information from a previous post, using the same salt for each crypt? here, $user_input would be the password entered by the user.

 

<?php
$password = crypt('mypassword'); // let the salt be automatically generated

/* You should pass the entire results of crypt() as the salt for comparing a
   password, to avoid problems when different hashing algorithms are used. (As
   it says above, standard DES-based password hashing uses a 2-character salt,
   but MD5-based hashing uses 12.) */
if (crypt($user_input, $password) == $password) {
   echo "Password verified!";
}
?>

Link to comment
Share on other sites

where do i use the salt in the query? Im setting predefined values for username and password. Ive encrypted the password and then i insert both the username and encrypted password in the database. Ive checked the table and the password is encrypted. My problem is when i run this script again, it shudnt insert another row in to the table, which it does. If i dnt use the crypt fucntion it works fine. Its not comparing the encrypted password in the table to the one encrypted at the top of the script

  $username = 'smith';
  $password = crypt('hello123');

  $dbhandle = sqlite_popen("adatabase", 0666, $err_msg);
  if(!$dbhandle) die("Error: Could not connect to database");

  $query = "CREATE TABLE admin(username VARCHAR(50), password VARCHAR(30))";
  if(@!sqlite_query($dbhandle, $query)) { echo "Table users already exists<br />"; }

  $query = sqlite_query($dbhandle, "SELECT * FROM admin WHERE username='$username' AND password='$password'");
  echo $query;

  if (sqlite_num_rows($query)==0) {
  
  $query = "INSERT INTO admin VALUES('$username', '$password')";
  //echo $query;

  if(@!sqlite_query($dbhandle, $query)) { echo "Could not insert table row"; }
  }

  }

Link to comment
Share on other sites

if you're going to crypt() something and store it, you'll have to use the same salt to compare anything else crypt'd:

 

<?
$password = crypt('hello123', 'somesalt');
echo "password: $password<BR>";

$user_input = crypt('hello123', 'somesalt');
echo "user_input: $user_input<BR>";

echo "password == user_input ? ";
echo ($password == $user_input)?"TRUE":"FALSE";
?>

 

output:

password: solmAr0OP9sJ.

user_input: solmAr0OP9sJ.

password == user_input ? TRUE

 

Link to comment
Share on other sites

your telling me stuff i already know. Yet no one is answering my question that im asking. I know that i have to compare the one in the database to the one on the script. so

 

$password = crypt('hello')

 

and to compare

 

if($userpassword, $password) ==$password

 

but in this example, password isnt coming from the database is it? i want to know how to implemnt into my query

 

 

Link to comment
Share on other sites

in my previous post, the password wasn't coming from a database. the point was that if you're going to compare one crypt()'d value from anywhere with another crypt()'d value from anywhere, they have to share the same salt.

 

so i would change this:

 

$password = crypt('hello123');

 

to this:

 

$password = crypt('hello123', 'somesalt');

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.