在网站的开发过程中,常常需要对部分数据(如用户密码)进行加密,本文主要介绍PHP的几个常见的加密函数
MD5加密:
string md5 ( string $str [, bool $raw_output = false ] )
1.md5()默认情况下以 32 字符十六进制数字形式返回散列值,它接受两个参数,第一个为要加密的字符串,第二个为raw_output的布尔值,默认为false,如果设置为true,md5()则会返回原始的 16 位二进制格式报文摘要
2.md5()为单向加密,没有逆向解密算法,但是还是可以对一些常见的字符串通过收集,枚举,碰撞等方法破解
<?php
$username='jellybool';
$password='jellybool.com';
echo md5($username);
echo "<hr>";
echo md5($password);
echo "<hr>";
echo md5(md5($password));
Crypt加密:
string crypt ( string $str [, string $salt ] )
1.crypt()接受两个参数,第一个为需要加密的字符串,第二个为盐值(就是加密干扰值,如果没有提供,则默认由PHP自动生成);返回散列后的字符串或一个少于 13 字符的字符串,后者为了区别盐值。
2.crypt()为单向加密,跟md5一样。
<?php
$password='jellybool.com';
echo crypt($password);
echo "<hr>";
echo crypt($password,"jellybool");
echo "<hr>";
echo crypt($password,'$1$jellybool$');
echo "<hr>";
Sha1加密:
string sha1 ( string $str [, bool $raw_output = false ]
1.跟md5很像,不同的是sha1()默认情况下返回40个字符的散列值,传入参数性质一样,第一个为加密的字符串,第二个为raw_output的布尔值,默认为false,如果设置为true,sha1()则会返回原始的20 位原始格式报文摘要
2.sha1()也是单行加密,没有逆向解密算法
<?php
$my_intro="jellybool";
echo sha1($my_intro);
echo "<hr>";
echo md5(sha1($my_intro));
Urlencode加密:
string urlencode ( string $str )
1.一个参数,传入要加密的字符串(通常应用于对URL的加密),
2.urlencode为双向加密,可以用urldecode来加密(严格意义上来说,不算真正的加密)
3.返回字符串,此字符串中除了 -_. 之外的所有非字母数字字符都将被替换成百分号(%)后跟两位十六进制数,空格则编码为加号(+)。
<?php
$my_urlencode="jellybool.com?jellybool=true + 4-3%5= \& @!";
echo urlencode($my_urlencode);
echo "<hr>";
$my_urldecode="jellybool.com%3Fjellybool%3Dtrue+%2B+4-3%255%3D+%5C%26+%40%21";
echo urldecode($my_urldecode);
echo "<hr>";
$my_urldecode="http://www.baidu.com/s?word=jellybool+%E8%A7%89%E7%B4%AF%E4%B8%8D%E7%88%B1&tn=98236947_hao_pg&ie=utf-8";
echo urldecode($my_urldecode);
$pre_url_encode="jellybool.com?username=jellybool&password=jelly";
$url_decode ="jellybool.com?username=jelly&bool&password=jelly";
$username="jelly&bool";
$url_decode ="jellybool.com?username=".urlencode($username)."&password=jelly";
base64编码加密:
string base64_decode ( string $encoded_data )
1.base64_encode()接受一个参数,也就是要编码的数据(这里不说字符串,是因为很多时候base64用来编码图片)
2.base64_encode()为双向加密,可用base64_decode()来解密
<?php
$my_intro="JellyBool是一个身材有高度,肩膀有宽度,胸肌有厚度,思想有深度的
国家免检五A级优质伪前端IT男屌丝";
echo base64_encode($my_intro);
echo "<hr>";
echo base64_decode('SmVsbHlCb29s5piv5LiA5Liq6Lqr5p2Q5pyJ6auY5bqmLOiCqeiGg
OacieWuveW6pizog7jogozmnInljprluqYs5oCd5oOz5pyJ5rex5bqm55qE5Zu95a6
25YWN5qOA5LqUQee6p+S8mOi0qOS8quWJjeerr0lU55S35bGM5Lid');
一个图片的例子:
<?php
$filename="https://worktile.com/img/index/index_video.png";
$data=file_get_contents($filename);
echo base64_encode($data);