Page 1 of 1

How to convert text file contents to md5 ==SOLVED==

Posted: Thu Aug 26, 2010 8:55 am
by dave7802
I am soo confused,
my head hurts and i officially have no more ideas :banghead:

Maybe someone here can help or shine some light on it,

Its simple "In Theory"
I have a text file, or a CSV, or Excel or what ever format you like,
It has Login Account information on it that i am trying to import into a MySQL Database, "This i have done successfully"
BUT and its a BIG BUT,

The Passwords are not Encrypted, and to have them work on my Site, i need to have them MD5 and Salt "md5($pass.$salt)"
I have two tables in my Database,
1 Called "password"
1 called "saltpass"

This is what stores the 2 sets of passwords for that account,
I some how need to encrypted the passwords i have now, and then update the existing data by importing it into the database,
"Hope i am clear up to this point"

So what i have done, is a PHP script that will convert the text Password to MD5 and 5 charectors long SALT code.
And then give the output to me in a Txt file,

Here is a copy of the code i am using:

Code: Select all

<?php
  // function to accept a plain text password and return a encrypted password
  // with the salt attached to the end of the string (note: the salt is 9 characters)
  function tep_encrypt_password($pass){
    $salt = substr(md5 ($password), 0, 5);
  // $password = md5($salt.$plain).':'.$salt;
    $password = md5($pass.$salt).':'.$salt;
    return $password;
  }
  
  // get a file with 3 columns separated by spaces [userid email password]
  $lines = file('passwords.txt');
  
  // iterate each line
  foreach($lines as $key => $line){
    // put each element into a variable
    list($email, $password) = explode(",", $line);
    // encrypt the password
    $password = tep_encrypt_password($password);
    // put the elements back into a single line
    $lines[$key] = implode(" ", array($email, $password));
  }
  // put all of the lines back into a single variable
  $lines = implode("\n", $lines);
  // write the variable to file
  file_put_contents('passwords3.txt', $lines);
?> 
And it works,
Only when passwords.txt contains 1 Email address and Password,
When i start on multiple lines, it seems to fall over itself and give me invalid MD5 and Salt passwords,

This is the correct data i have been working on,

Format is: Email, Password
frankrank@live.com maitland 71906d768efd2d598de075ec6350f390:d41d8
looney@google.com allan2009 ab32a79054cf7ad7e5f288dcda05ce27:d41d8
nonenone@franks.com jameswoodhead1 f5acdb98721f1a09e947e3d047d6e931:d41d8
my text file i would be using in the script passwords.txt:
frankrank@live.com,maitland
looney@google.com,allan2009
nonenone@franks.com,jameswoodhead
But when i run the code i get:
frankrank@live.com 508650b0fef24cb3f9a8fd6ddd601085:d41d8
looney@google.com 6137a3b58e149c67196ff7b603e5f852:d41d8
nonenone@franks.com 569730a471662db85dd2d9c5479d6e6d:d41d8
So all the MD5 and Salt password are wrong?????


But when i run the same thing again but this time i have 1 line instead of 3, i get the correct information:
frankrank@live.com 71906d768efd2d598de075ec6350f390:d41d8

So where am i going wrong?
Its not a case of doing it one by one, as i have 3,000 odd customer to run this on,

Any help or advise lol

Thanks very much in advance if someone is able to help :)

Re: How to convert text file contents to md5??

Posted: Thu Aug 26, 2010 9:32 am
by shawngoldw
MAYBE this line:

Code: Select all

$salt = substr(md5 ($password), 0, 5);
needs to be

Code: Select all

$salt = substr(md5 ($pass), 0, 5);
:wink:


Why are you storing both the plain text password and the salted password? Storing the plaintext one sort of defeats the purpose of using the hash.


Shawn

Re: How to convert text file contents to md5??

Posted: Thu Aug 26, 2010 11:33 am
by dave7802
Am not storing Plane text,
I am manually updating a spreadsheet and removing the plane text passwords,
Only the Md5 and Salt will be on there,

This was the only way i knew of converting the passwords in bulk.

Code: Select all

$salt = substr(md5 ($password), 0, 5);
Still worked, Pass Or Password, still done the same job, this was only a quick Temp bit of Code,
the proper code to improve the security was to randomly generate the salt key

Code: Select all

 $salt = substr(md5(uniqid(rand(), true)), 0, 5)
But what fixed it for me was this little pain in the ass

Code: Select all

$lines = file('passwords.txt',FILE_IGNORE_NEW_LINES);

Thanks for the reply tho :D


So for anyone els who is looking for this and stumbles across this, here is a copy of the working script

Have a text document with a email address and password with a comma "," Separating them both
Run this and you will get a new document with the Email Address and Encrypted Password :D

Hope this helps

Code: Select all

<?php

  //THIS IS A ENCRYPTION SCRIPT
  //THIS SCRIPT WILL CONVERT PASSWORD PLAIN TEXT TO md5($pass.$salt)
  //FOR LATER IMPORTING INTO MYSQL DATABASE

  // function to accept a plain text password and return a encrypted password
  // with the salt attached to the end of the string (note: the salt is 9 characters)
  function tep_encrypt_password($pass){
  $salt = substr(md5(uniqid(rand(), true)), 0, 5);
    $password = md5($pass.$salt).':'.$salt;
    return $password;
  }
  
  // get a file with 3 columns separated by spaces [userid email password]
  $lines = file('passwords.txt',FILE_IGNORE_NEW_LINES);
  
  // iterate each line
  foreach($lines as $key => $line){
    // put each element into a variable
    list($email, $password) = explode(",", $line);
    // encrypt the password
    $password = tep_encrypt_password($password);
    // put the elements back into a single line
    $lines[$key] = implode(" ", array($email, $password));
  }
  // put all of the lines back into a single variable
    $lines = implode("\n", $lines);
  // write the variable to file
  file_put_contents('passwords3.txt', $lines);

?>