php加密函数

来源:互联网 发布:网络管理软件分类 编辑:程序博客网 时间:2024/05/09 08:09
单项加密比较容易。最简单的就是md5。还有mcrypt加密函数库。base64不能算做加密,只能算是转码。因为别人可以直接用base64_decode得到原文。mcrypt有双向加密揭秘的函数。但是,mcrypt扩展并不是每个服务器都会有的。DISCUZ自带了一个双向加密函数,推荐使用:<?php#加密$code = authcode("everalan", 'ENCODE');#解密$username = authcode($code, 'DECODE');function authcode($string, $operation, $key = '') {  $auth_key = !empty($key) ? $key : '你自己定义的key'; $key = md5($auth_key); $key_length = strlen($key); $string = $operation == 'DECODE' ? base64_decode($string) : substr(md5($string.$key), 0, 8).$string; $string_length = strlen($string); $rndkey = $box = array(); $result = ''; for($i = 0; $i <= 255; $i++) {  $rndkey[$i] = ord($key[$i % $key_length]);  $box[$i] = $i; } for($j = $i = 0; $i < 256; $i++) {  $j = ($j + $box[$i] + $rndkey[$i]) % 256;  $tmp = $box[$i];  $box[$i] = $box[$j];  $box[$j] = $tmp; } for($a = $j = $i = 0; $i < $string_length; $i++) {  $a = ($a + 1) % 256;  $j = ($j + $box[$a]) % 256;  $tmp = $box[$a];  $box[$a] = $box[$j];  $box[$j] = $tmp;  $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256])); } if($operation == 'DECODE') {  if(substr($result, 0, 8) == substr(md5(substr($result, 8).$key), 0, 8)) {   return substr($result, 8);  } else {   return '';  } } else {  return str_replace('=', '', base64_encode($result)); }} 可以肯定的是,你不需要用C++
原创粉丝点击