How to write code for md5 encryption and decryption in PHP?
In PHP, you can use the md5() function for MD5 encryption. MD5 is a one-way encryption algorithm, meaning it cannot be reversed. Therefore, direct decryption is not possible. Below is an example code of using MD5 for encryption and verification in PHP.
// 加密
$password = 'password123';
$encrypted = md5($password);
echo "加密后的密码:" . $encrypted . "<br>";
// 验证
$inputPassword = 'password123';
if(md5($inputPassword) == $encrypted){
echo "密码正确";
}else{
echo "密码错误";
}
Please note that MD5 is no longer recommended for password storage as it is considered to be insecure. It is recommended to use stronger hashing algorithms such as bcrypt or Argon2.