PHP加密解密
計應134 凌豪
1.使用crypt()函數進行加密
crypt()函數可以完成單向加密功能,語法如下:
string crypt(string str[, string salt]);
其中,str參數是需要加密的字符串,salt參數為加密時使用的干擾串。如果省略掉第二個參數,則會隨機生成一個干擾串。crypt()函數支持4種算法和長度,如表所示。
聲明一個字符串變量$str,賦值為“This is test”,然后用crypt()函數進行加密並輸出
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>使用crypt()函數進行加密</title>
<style type="text/css">
<!--
body,td,th {
font-size: 12px;
}
body {
margin-left: 10px;
margin-top: 10px;
margin-right: 10px;
margin-bottom: 10px;
}
-->
</style></head>
<body>
<?php
$str ='This is test!'; //聲明字符串變量$str
echo '加密前$str的值為:'.$str;
$crypttostr = crypt($str); //對變量$str加密
echo '<p>加密后$str的值為:'.$crypttostr; //輸出加密后的變量
?>
</body>
</html>
2.使用md5()函數進行加密
md5()函數使用MD5算法。MD5的全稱是Message-Digest Algorithm 5(信息-摘要算法),它的作用是把不同長度的數據信息經過一系列的算法計算成一個128位的數值,就是把一個任意長度的字節串變換成一定長的大整數。注意這里是“字節串”而不是“字符串”,因為這種變換只與字節的值有關,與字符集或編碼方式無關。md5()函數的格式如下:
string md5 ( string str [, bool raw_output] );
其中字符串str為要加密的明文,raw_output參數如果設為true,則函數返回一個二進制形式的密文,該參數默認為false。
實現登錄注冊功能,並將用戶密碼通過MD5加密保存到數據庫中其代碼如下:
創建注冊界面:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link href="css/style.css" rel="stylesheet" type="text/css">
<title>會員注冊</title>
</head>
<body>
<div align="center">
<script language="javascript">
function register(){
if(myform.name.value=="")
{alert("會員名稱不能為空!!");myform.name.focus();return false;}
if(myform.pwd.value=="")
{alert("會員密碼不能為空!!");myform.pwd.focus();return false;}
}
</script>
<form name="myform" method="post" action="register_ok.php">
<table width="778" height="314" border="0" cellpadding="0"
cellspacing="0">
<tr>
<td valign="top" background="images/reg.gif"><table width="778" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="255" height="255"> </td>
<td width="66"> </td>
<td width="125"> </td>
<td width="332"> </td>
</tr>
<tr>
<td height="31"> </td>
<td align="center">注冊名稱:</td>
<td><input name="name" type="text" id="name" size="16"></td>
<td> </td>
</tr>
<tr>
<td height="35"> </td>
<td align="center">注冊密碼:</td>
<td><input name="pwd" type="password" id="pwd" size="16"></td>
<td> </td>
</tr>
<tr>
<td height="34"> </td>
<td> </td>
<td align="right"><input name="imageField" type="image"
src="images/reg.jpg" width="87" height="24" border="0"
onClick="return register();">
</a> </td>
<td> </td>
</tr>
<tr>
<td height="87"> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</table></td>
</tr>
</table>
</form>
</div>
</body>
</html>
創建register_ok.php
<?php
class chkinput { //定義chkinput類
var $name; //定義成員變量
var $pwd; //定義成員變量
function chkinput($x, $y) { //定義成員方法
$this->name = $x; //為成員變量賦值
$this->pwd = $y; //為成員變量賦值
}
function checkinput() { //定義方法,完成用戶注冊
include "conn/conn.php"; //通過include調用數據庫連接文件
$info = mysql_query ( "insert into tb_user(user,password)value('" . $this->name . "','" . $this->pwd . "')" );
if ($info == false) { //根據添加操作的返回結果,給出提示信息
echo "<script language='javascript'>alert('會員注冊失敗!');history.back();</script>";
exit ();
} else {
$_SESSION [admin_name] = $this->name; //注冊成功后,將用戶名賦給SESSION變量
echo "<script language='javascript'>alert('恭喜您,注冊成功!');window.location.href='index.php';</script>";
}
}
}
$obj = new chkinput ( trim ( $_POST [name] ), trim ( md5 ( $_POST [pwd] ) ) ); //實例化類
$obj->checkinput (); //根據返回對象調用方法執行注冊操作
?>
3.使用sha1()函數進行加密
和MD5類似的還有SHA算法。SHA全稱為Secure Hash Algorithm(安全哈希算法),PHP提供的sha1()函數使用的就是SHA算法,函數的語法如下:
string sha1 ( string str [, bool raw_output] )
函數返回一個40位的十六進制數,如果參數raw_output為true,則返回一個20位的二進制數。默認raw_output為false。
下面是分別對一個字符串進項MD5和SHA加密運算,代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>使用md5()和sha1()函數進行加密</title>
<style type="text/css">
<!--
body,td,th {
font-size: 12px;
}
body {
margin-left: 10px;
margin-top: 10px;
margin-right: 10px;
margin-bottom: 10px;
}
-->
</style></head>
<body>
<div align="center">
<table border="1" cellspacing="0" cellpadding="0">
<tr>
<td height="30" colspan="2" align="center" valign="middle" scope="col"><?php echo 'md5()和shal()函數的對比效果'; ?></td>
</tr>
<tr>
<td width="200" height="30" align="right" valign="middle"><?php echo '使用md5()函數加密字符串PHPER:' ?></td>
<td width="200" height="30" align="center" valign="middle"><?php echo md5('PHPER'); ?></td>
</tr>
<tr>
<td width="200" height="30" align="right" valign="middle"><?php echo '使用shal()函數加密字符串PHPER:'; ?></td>
<td width="200" height="30" align="center" valign="middle"><?php echo sha1('PHPER'); ?></td>
</tr>
</table>
</div>
</body>
</html>