PHP Encryption decryption

Source: Internet
Author: User

PHP Encryption decryption

The 134 of the Ling Hao

1. Encrypt using the crypt () function

The crypt () function can complete one-way encryption, with the following syntax:

String Crypt (String str[, string salt]);

Where the str parameter is a string that needs to be encrypted, the salt parameter is the interference string used when encrypting. If the second argument is omitted, a disturbance string is randomly generated. The crypt () function supports 4 algorithms and lengths, as shown in the table.

Declare a string variable $str, assign a value of "This is Test", and then use the crypt () function to encrypt and output

DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title> using the Crypt () function for encryption </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>
<body>
<?php
$str = ' This is test! '; Declaring a String variable $str
Echo ' Before encryption $str value is: '. $str;
$crypttostr = Crypt ($STR); Encrypt the variable $str
Echo ' <p> encrypted after $STR value is: '. $crypttostr; Output the encrypted variable
?>
</body>

2. Encrypt using the MD5 () function

The MD5 () function uses the MD5 algorithm. The full name of the MD5 is Message-digest algorithm 5 (Information-Digest algorithm), which is the function of different lengths of data information through a series of algorithms to calculate the value of a 128-bit, that is, an arbitrary length of the byte string transformed into a certain long large integer. Note that this is a "byte string" instead of a "string" because the transformation is only related to the value of the byte, regardless of the character set or encoding. The format of the MD5 () function is as follows:

String MD5 (String str [, BOOL raw_output]);

Where the string str is the plaintext to be encrypted, and if the Raw_output parameter is set to True, the function returns a binary form of the ciphertext, which defaults to false.

Implement the login registration function and save the user password through MD5 encryption to the database with the following code:

To create a registration interface:



<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<link href= "Css/style.css" rel= "stylesheet" type= "Text/css" >
<title> Member Registration </title>

<body>
<div align= "center" >
<script language= "JavaScript" >
function Register () {
if (myform.name.value== "")
{alert ("the member name cannot be empty!! "); Myform.name.focus (); return false;}
if (myform.pwd.value== "")
{alert ("The member password cannot be empty!! "); 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>
&LT;TD valign= "Top" background= "Images/reg.gif" ><table width= "778" border= "0" cellspacing= "0" cellpadding= "0" >
<tr>
&LT;TD width= "255" height= "255" >&nbsp;</td>
&LT;TD width= ">&nbsp;</td>"
&LT;TD width= ">&nbsp;</td>"
&LT;TD width= "332" >&nbsp;</td>
</tr>
<tr>
&LT;TD height= ">&nbsp;</td>"
&LT;TD align= "center" > Registered name:</td>
<td><input name= "name" type= "text" id= "name" size= "></td>"
<td>&nbsp;</td>
</tr>
<tr>
&LT;TD height= ">&nbsp;</td>"
&LT;TD align= "center" > Registration Password:</td>
<td><input name= "pwd" type= "password" id= "pwd" size= "></td>"
<td>&nbsp;</td>
</tr>
<tr>
&LT;TD height= ">&nbsp;</td>"
<td>&nbsp;</td>
&LT;TD align= "right" ><input name= "ImageField" type= "image"
Src= "Images/reg.jpg" width= "height=" border= "0"
onclick= "return register ();" >
</a>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
&LT;TD height= ">&nbsp;</td>"
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table></td>
</tr>
</table>
</form>
</div>
</body>


Create register_ok.php

<?php
Class Chkinput {//define CHKINPUT classes
var $name; Defining member variables
var $pwd; Defining member variables
function Chkinput ($x, $y) {//define Member method
$this->name = $x; Assigning a value to a member variable
$this->pwd = $y; Assigning a value to a member variable
}
function Checkinput () {//define method to complete user registration
Include "conn/conn.php"; To invoke a database connection file through include
$info = mysql_query ("INSERT into Tb_user (User,password) value ('). $this->name. "‘,‘" . $this->pwd. "‘)" );
if ($info = = False) {//Give a hint based on the return result of the add operation
echo "<script language= ' JavaScript ' >alert (' member registration failed! '); History.back ();</script> ";
Exit ();
} else {
$_session [Admin_name] = $this->name; After successful registration, assign the user name to the session variable
echo "<script language= ' JavaScript ' >alert (' Congratulations, registration successful! "Window.location.href= ' index.php ';</script>";
}
}
}
$obj = new Chkinput (Trim ($_post [name]), TRIM (MD5 ($_post [pwd])); Instantiating classes
$obj->checkinput (); To perform a registration operation based on the return object invocation method
?>

3. Encrypt using the SHA1 () function

Similar to MD5, there are SHA algorithms. SHA is all called Secure Hash algorithm (secure hashing algorithm), PHP provides the SHA1 () function using the SHA algorithm, the syntax of the function is as follows:

String SHA1 (String str [, BOOL Raw_output])

The function returns a 40-bit hexadecimal number and returns a 20-bit binary number if the parameter raw_output is true. The default raw_output is false.

The following is a string of input MD5 and SHA cryptographic operations, respectively, the code is as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title> using the MD5 () and SHA1 () functions for encryption </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>
<body>
<div align= "center" >
<table border= "1" cellspacing= "0" cellpadding= "0" >
<tr>
&LT;TD height= "colspan=" 2 "align=" center "valign=" middle "scope=" col "><?php Echo ' MD5 () and shal () function of the contrasting effect ';? ></td>
</tr>
<tr>
&LT;TD width= "height=" "align=" right "valign=" middle "><?php echo" use MD5 () function to encrypt string phper: '?></td>
&LT;TD width= "height=" "align=" center "valign=" middle "><?php echo MD5 (' Phper ');?></td>
</tr>
<tr>
&LT;TD width= "$" height= "align=" right "valign=" middle "><?php Echo ' use Shal () function to encrypt string phper: ';? ></td >
&LT;TD width= "" "height=" "align=" center "valign=" middle "><?php echo SHA1 (' Phper '); ?></td>
</tr>
</table>
</div>
</body>


PHP Encryption decryption

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.