I have a text file (passwords.txt) that contains one column with thousands of clear text passwords. For example:

12345
abc123
and etc...

I would like to create a script that can open this file, convert each password using md5 and salt, then save the converted passwords to a new file. Can this be done in PHP?

Any help is greatly appreciated!

    This is what I have so far, but I cannot get it to work:

    function tep_encrypt_password($plain) {
    $password = '';

    for ($i=0; $i<10; $i++) {
      $password .= tep_rand();
    }

    // read statement
    $fp = fopen('passwords.txt', 'w');
    $salt = substr(md5($password), 0, 2);

    $password = md5($salt . $plain) . ':' . $salt;
    
    return $password;

    fwrite($fp, $content);
    fclose($fp);

      $passes = file('passwords.txt');
      foreach($passes as $key => $pass)
        $passes[$key] = md5($pass);
      $passes = implode("\n", $passes);
      file_put_contents('passwords.txt', $passes);

      edit

      In the exact order as laserlight said.

      +1 for me

        This ought to work:

        <?php
        //$file - path to the file
        function encrypt_password_file($file) {
        
        //If the file doesn't exist return false
        if(!file_exists($file)){
        	return false;
        	exit;
        }
        
        //Opens the file ignoring new lines in the file and does not add \n to each element
        $passwords = file($file, FILE_IGNORE_NEW_LINES + FILE_SKIP_EMPTY_LINES);
        
        //Loop through all the passwords
        foreach($passwords as $password){
        	$salt = substr(md5($password), 0, 2);
        	$encrypted_passwords[] = md5($salt . $password) . ':' . $salt;
        }
        
        //Makes a backup
        copy($file, $file . ".backup");
        
        $fp = fopen($file, 'w');
        
        //If the array could not be written
        if(!fwrite($fp, implode("\n", $encrypted_passwords))){
        	fclose($fp);
        	return false;
        }
        else{
        	fclose($fp);
        	return true;
        }
        }
        ?>

        EDIT: Looks like Kudose beat me too it, but mine backups and uses the salt as you wanted 😉

          I'm going to test this out now... Thanks for your help fellas!

            Kudose wrote:
            $passes = file('passwords.txt');
            foreach($passes as $key => $pass)
              $passes[$key] = md5($pass);
            $passes = implode("\n", $passes);
            file_put_contents('passwords.txt', $passes);

            edit

            In the exact order as laserlight said.

            +1 for me

            This worked for me, so I added a little salt to it and it seems to be working as coded. THANKS!

            <?php
            
            $passes = file('passwords3.txt');
            // add salt parameter
            $salt = substr(md5($password), 0, 2);
            
            foreach($passes as $key => $pass)
               //salt free version
               //$passes[$key] = md5($pass);
               $passes[$key] = md5($salt . $pass) . ':' . $salt;
            $passes = implode("\n", $passes);
            file_put_contents('passwords3.txt', $passes);
            
            
            ?>

            Now I have one more thing I'd like to add to it. Instead of just having a txt file of passwords (passwords.txt), what if it the txt file was setup like this:

            email address password
            me@123.com 9876
            john@doe.com pe9204
            jane@hotmail.com yyoo0022

            what would I need to add to the script so it would skip over the email address and just convert the password to md5 + salt?

              <?php
              $passes = file('passwords3.txt');
              // add salt parameter
              $salt = substr(md5($password), 0, 2);
              
              foreach($passes as $key => $pass){
                 //salt free version
                 //$passes[$key] = md5($pass);
                // separate the line by spaces
                $line = explode(" ", $pass); 
                // the password is in $line[1], the email is in $line[0]
                $line[1] = md5($salt . $line[1]) . ':' . $salt;
                $passes[$key] = implode(" ", $line);
              }
              $passes = implode("\n", $passes);
              file_put_contents('passwords3.txt', $passes);
              ?> 
                Kudose wrote:
                <?php
                $passes = file('passwords3.txt');
                // add salt parameter
                $salt = substr(md5($password), 0, 2);
                
                foreach($passes as $key => $pass){
                   //salt free version
                   //$passes[$key] = md5($pass);
                  // separate the line by spaces
                  $line = explode(" ", $pass); 
                  // the password is in $line[1], the email is in $line[0]
                  $line[1] = md5($salt . $line[1]) . ':' . $salt;
                  $passes[$key] = implode(" ", $line);
                }
                $passes = implode("\n", $passes);
                file_put_contents('passwords3.txt', $passes);
                ?> 

                Thank you kudose! This is working well.

                Just one more minor addition I'd like to make. Right now, this script is applying the same salt to all hashes. I'd like to randomly apply the salt using something like this:

                function tep_encrypt_password($plain) {
                    $password = '';
                
                for ($i=0; $i<10; $i++) {
                  $password .= tep_rand();
                }
                
                $salt = substr(md5($password), 0, 2);
                
                $password = md5($salt . $plain) . ':' . $salt;
                
                return $password;
                  }

                This is my final request. Thanks again for all your help!

                  <?php
                    function tep_encrypt_password($plain){
                      $salt = substr(md5(uniqid(rand(), true)), 0, 9);
                      $password = md5($salt.$plain).':'.$salt;
                      return $password;
                    } 
                  
                    $passes = file('passwords3.txt');
                  
                    foreach($passes as $key => $pass){
                      $line = explode(" ", $pass);
                      // the password is in $line[1], the email is in $line[0]
                      $line[1] = tep_encrypt_password($line[1]);
                      $passes[$key] = implode(" ", $line);
                    }
                    $passes = implode("\n", $passes);
                    file_put_contents('passwords3.txt', $passes);
                  
                  ?>

                    Thanks Kudose! I appreciate your help!

                      Kudose wrote:
                      <?php
                        function tep_encrypt_password($plain){
                          $salt = substr(md5(uniqid(rand(), true)), 0, 9);
                          $password = md5($salt.$plain).':'.$salt;
                          return $password;
                        } 
                      
                        $passes = file('passwords3.txt');
                      
                        foreach($passes as $key => $pass){
                          $line = explode(" ", $pass);
                          // the password is in $line[1], the email is in $line[0]
                          $line[1] = tep_encrypt_password($line[1]);
                          $passes[$key] = implode(" ", $line);
                        }
                        $passes = implode("\n", $passes);
                        file_put_contents('passwords3.txt', $passes);
                      
                      ?>

                      Looks like there is a new wrinkle. Apparently, I need to take two values and add the md5 + salt...

                      For example:

                      userid email password
                      1234567 john@doe.com 123456
                      1234568 jane@doe.com 12hgtnTT

                      I need to take the userid (field 1) and the password field (field 3) read the value, then override the password value with the new md5 + hash value.

                      The end result of the text file should look like this:

                      userid email password
                      1234567 john@doe.com e365692167c426183780c1a956e37954:98
                      1234568 jane@doe.com fcea920f7412b5da7be0cf42b8c93759:7s

                      I appreciate everyone's help!

                        Give it a shot and post your modified code. 😉

                          <?php

                          function tep_encrypt_password($plain){
                          $salt = substr(md5(uniqid(rand(), true)), 0, 2);
                          $password = md5($salt.$plain).':'.$salt;
                          return $password;
                          }

                          $passes = file('new.txt');

                          foreach($passes as $key => $pass){
                          $line = explode(" ", $pass);
                          // the password is in $line[1], the email is in $line[0]
                          $line[0] = tep_encrypt_password($line[0]);
                          $line[2] = tep_encrypt_password($line[2]);
                          //$passes[$key] = implode(" ", $line);
                          $passes[$key] = implode(" ", $line);
                          }
                          $passes = implode("\n", $passes);
                          file_put_contents('new.txt', $passes);

                          ?>

                          RESULT: a963c3665a2443f935277b7278f71ec5:8b john@doe.com
                          a57c8fd7aaf9f2ac7e954117ce7f7959:f3

                          I need to spend some more time with this. I want the hash + salt to override the password... I've got some more studying to do. If I figure this out, I will gladly post my findings 🙂

                            <?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($plain){
                                $salt = substr(md5(uniqid(rand(), true)), 0, 9);
                                $password = md5($salt.$plain).':'.$salt;
                                return $password;
                              }
                            
                              // get a file with 3 columns separated by spaces [userid email password]
                              $lines = file('passwords3.txt');
                            
                              // iterate each line
                              foreach($lines as $key => $line){
                                // put each element into a variable
                                list($userid, $email, $password) = explode(" ", $line);
                                // encrypt the password
                                $password = tep_encrypt_password($password);
                                // put the elements back into a single line
                                $lines[$key] = implode(" ", array($userid, $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);
                            ?> 

                              By the way, you were pretty close with your last post. The only problem I can see off the top of my head is that you were trying to encrypt the userid as well as the password.

                              <?php
                              
                              function tep_encrypt_password($plain){
                              $salt = substr(md5(uniqid(rand(), true)), 0, 2);
                              $password = md5($salt.$plain).':'.$salt;
                              return $password;
                              }
                              
                              $passes = file('new.txt');
                              
                              foreach($passes as $key => $pass){
                              $line = explode(" ", $pass);
                              // the password is in $line[1], the email is in $line[0]
                              //$line[0] = tep_encrypt_password($line[0]); /// don't want to do this
                              $line[2] = tep_encrypt_password($line[2]);
                              //$passes[$key] = implode(" ", $line);
                              $passes[$key] = implode(" ", $line);
                              }
                              $passes = implode("\n", $passes);
                              file_put_contents('new.txt', $passes);
                              
                              ?>
                                2 years later

                                Wow!!!!!

                                After a long hunt i finally found somewhere that is sort of in the same boat as me.

                                I don't know if you may be able to help as i am really unsure how to approach this.

                                I have a CSV file, that has Username Emails Passwords Addresses everything!!
                                And i have imported that into a MySQL database,

                                the issue i have come accross is the site itsself uses md5($pass.$salt) as its encryption,

                                So i have 2 fields in my table:
                                1 called "password"
                                the other called "saltpass"

                                Everywhere i look, i come to a standstill on what to do,
                                Is there a Query i can run on the database that would convert this for me, and populate both fields,

                                OR
                                Is there a way on doing this using something like your Method using PHP and converting my CSV to txt or any other format it may pickup and use?

                                i would really appreciate any help you may be able to offer
                                thanks

                                  Write a Reply...