Click here to Skip to main content
15,899,009 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to encrypt password, email, username and other information after the user logins on the site, encrypt them in the cookies. I new to php

This my php for password encryption and decryption.

I m a newbie to php. If you have better way than mine please tell me where to improve.

PHP
function encryptionKey($password) {
    $password = strtolower($password);
    return array(hash("sha256", $password));
	}
	function encrypt($enpassword) {
		
			return  trim( base64_encode( mcrypt_encrypt( MCRYPT_RIJNDAEL_256, substr($enpassword[0],0,32),$enpassword, MCRYPT_MODE_CBC, substr($enpassword[1],0,32) )));
    }
	function decrypt($depassword) {
            return mcrypt_decrypt(MCRYPT_RIJNDAEL_256, substr($depassword[0],0,32), base64_decode($depassword),MCRYPT_MODE_CBC, substr($depassword[1],0,32)
        );
	} 	
	
public function hashPassword($password) {
	$saltpassword = $password . "5138hyh8g0ghg3g5h"; 		
	$encpassword = hash('sha256', $saltpassword);
		return $encpassword;
}
Posted
Comments
ZurdoDev 15-Dec-15 7:23am    
No, don't put the password into a cookie. Password should only be used to validate the user.

I don't know php but in .Net once you validate a user it's common to set a Session variable, for example, the user's name and then you can use that throughout the code.
Sergey Alexandrovich Kryukov 15-Dec-15 12:11pm    
The whole idea to encrypt password is wrong. A password should never be stored anywhere at all, it is unsafe and absolutely not needed for authentication. Nobody should be able to know a password except the original owner.
—SA

1 solution

Please see my comment to the question. Here is a hint for you on using the passwords: https://en.wikipedia.org/wiki/Cryptographic_hash_function[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900