Home >Backend Development >PHP Tutorial >PHP custom encryption method with encryption factor
There are many PHP encryption methods now. Generally, the simple encryption method is direct MD5, but some simple MD5 passwords can also be decompiled, so it is safer for us to encrypt using a custom method now.
Now I am collecting and summarizing some PHP encryption methods and sharing them with you for your reference.
Method 1:
/** * 对用户的密码进行加密 * @param $password * @param $encrypt //传入加密串,在修改密码时做认证 * @return array/password */ function password($password, $encrypt='') { $pwd = array(); $pwd['encrypt'] = $encrypt ? $encrypt : rand(1000,9999); $pwd['password'] = md5(md5(trim($password)).$pwd['encrypt']); return $encrypt ? $pwd['password'] : $pwd; }
Method description: When we only pass in the password, the method will return an encryption factor and a newly generated encryption password; and this encryption factor and password are randomly generated and will not change every time. Same.
Method 2:
/* * rc4加密算法 * $pwd 密钥 * $data 要加密的数据 */ //$pwd密钥 $data需加密字符串 function rc4 ($pwd, $data){ $key[] =""; $box[] =""; $pwd_length = strlen($pwd); $data_length = strlen($data); for ($i = 0; $i < 256; $i++){ $key[$i] = ord($pwd[$i % $pwd_length]); $box[$i] = $i; } for ($j = $i = 0; $i < 256; $i++) { $j = ($j + $box[$i] + $key[$i]) % 256; $tmp = $box[$i]; $box[$i] = $box[$j]; $box[$j] = $tmp; } for ($a = $j = $i = 0; $i < $data_length; $i++) { $a = ($a + 1) % 256; $j = ($j + $box[$a]) % 256; $tmp = $box[$a]; $box[$a] = $box[$j]; $box[$j] = $tmp; $k = $box[(($box[$a] + $box[$j]) % 256)]; $cipher .= chr(ord($data[$i]) ^ $k); } return $cipher; }
Method 3:
/** +----------------------------------------------------- * Mcrypt 加密/解密 * @param String $date 要加密和解密的数据 * @param String $mode encode 默认为加密/decode 为解密 * @return String * @author zxing@97md.net Mon Sep 14 22:59:28 CST 2009 +----------------------------------------------------- * @example */ function ZxingCrypt($date,$mode = 'encode'){ $key = md5('zxing');//用MD5哈希生成一个密钥,注意加密和解密的密钥必须统一 if ($mode == 'decode'){ $date = base64_decode($date); } if (function_exists('mcrypt_create_iv')){ $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB); $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); } if (isset($iv) && $mode == 'encode'){ $passcrypt = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $date, MCRYPT_MODE_ECB, $iv); }elseif (isset($iv) && $mode == 'decode'){ $passcrypt = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $date, MCRYPT_MODE_ECB, $iv); } if ($mode == 'encode'){ $passcrypt = base64_encode($passcrypt); } return $passcrypt; }