Home > Backend Development > PHP Tutorial > PHP custom encryption method with encryption factor

PHP custom encryption method with encryption factor

高洛峰
Release: 2016-10-21 10:57:16
Original
1730 people have browsed it

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;
}
Copy after login

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; 
}
Copy after login

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 = &#39;encode&#39;){ 
    $key = md5(&#39;zxing&#39;);//用MD5哈希生成一个密钥,注意加密和解密的密钥必须统一 
    if ($mode == &#39;decode&#39;){ 
        $date = base64_decode($date); 
    } 
    if (function_exists(&#39;mcrypt_create_iv&#39;)){ 
        $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB); 
        $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); 
    } 
    if (isset($iv) && $mode == &#39;encode&#39;){ 
        $passcrypt = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $date, MCRYPT_MODE_ECB, $iv); 
    }elseif (isset($iv) && $mode == &#39;decode&#39;){ 
        $passcrypt = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $date, MCRYPT_MODE_ECB, $iv); 
    } 
    if ($mode == &#39;encode&#39;){ 
        $passcrypt = base64_encode($passcrypt); 
    } 
    return $passcrypt; 
}
Copy after login


Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template