Heim  >  Artikel  >  Backend-Entwicklung  >  求php加密解密算法的使用

求php加密解密算法的使用

WBOY
WBOYOriginal
2016-07-11 09:17:421057Durchsuche

加密php算法

最近项目需求,要用php将一些普通的信息通过加密算法生成一个加密串,加密串能通过解密算法(逆向)解析出先前的信息。因为没有操作过,所以想问一下这里的朋友是否有现成的加解密算法的思路,算法需要够复杂不易被破解,有具体代码更好就更好啦

回复内容:

http://blog.shiniv.com/2013/11/use-aes-encryption-algorithm-to-encrypt-data-in-php/

http://www.soso.io/article/32937.html

经常用的加密/解密的字符串函数:
/**

  • 可逆的字符串加密函数
  • @param int $txtStream 待加密的字符串内容
  • @param int $password 加密密码
  • @return string 加密后的字符串 */ public static function enCrypt($txtStream,$password){ //密锁串,不能出现重复字符,内有A-Z,a-z,0-9,/,=,+,_,

$lockstream = 'st=lDEFABCNOPyzghi_jQRST-UwxkVWXYZabcdef+IJK6/7nopqr89LMmGH012345uv';
//随机找一个数字,并从密锁串中找到一个密锁值
$lockLen = strlen($lockstream);
$lockCount = rand(0,$lockLen-1);
$randomLock = $lockstream[$lockCount];
//结合随机密锁值生成MD5后的密码
$password = md5($password.$randomLock);
//开始对字符串加密
$txtStream = base64_encode($txtStream);
$tmpStream = '';
$i=0;$j=0;$k = 0;
for ($i=0; $i $k = ($k == strlen($password)) ? 0 : $k;
$j = (strpos($lockstream,$txtStream[$i])+$lockCount+ord($password[$k]))%($lockLen);
$tmpStream .= $lockstream[$j];
$k++;
}
return $tmpStream.$randomLock;

}

header("content-type:text/html;charset=utf8");
$str = "我的名字是马云一般人我不告诉他!"; //加密内容
$key = "9545646"; //密钥 自己制定密钥
$cipher = MCRYPT_DES; //密码类型
$modes = MCRYPT_MODE_ECB; //密码模式
$iv = mcrypt_create_iv(mcrypt_get_iv_size($cipher,$modes),MCRYPT_RAND);//初始化向量
echo "加密明文:".$str."

";
$str_encrypt =base64_encode( mcrypt_encrypt($cipher,$key,$str,$modes,$iv)); //加密函数
echo "加密密文:".$str_encrypt."

";
$str_decrypt = mcrypt_decrypt($cipher,$key,base64_decode($str_encrypt),$modes,$iv); //解密函数
echo "还原:".$str_decrypt;
?>

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn