search
HomeBackend DevelopmentPHP TutorialSeveral commonly used encryption functions in PHP_php tips

MD5加密:

string md5 ( string $str [, bool $raw_output = false ] )

1.md5()默认情况下以 32 字符十六进制数字形式返回散列值,它接受两个参数,第一个为要加密的字符串,第二个为raw_output的布尔值,默认为false,如果设置为true,md5()则会返回原始的 16 位二进制格式报文摘要

2.md5()为单向加密,没有逆向解密算法,但是还是可以对一些常见的字符串通过收集,枚举,碰撞等方法破解

<&#63;php
$username='jellybool';
$password='jellybool.com';
/*简单地对字符串进行md5加密*/
echo md5($username);
echo "<hr>";
echo md5($password);
echo "<hr>";
/*更推荐的做法是对重要的敏感数据进行多次加密,以防被轻易破解*/
echo md5(md5($password));

/*以上输出:
  username:4f5436e5d72608fb647b691e8edcf42e
  password:7bf02cf0f4af6da4accbc73d2a175476
  password(两次加密):864704bb35754f8cd0232cba6b91521b
*/

Crypt加密:

string crypt ( string $str [, string $salt ] )

1.crypt()接受两个参数,第一个为需要加密的字符串,第二个为盐值(就是加密干扰值,如果没有提供,则默认由PHP自动生成);返回散列后的字符串或一个少于 13 字符的字符串,后者为了区别盐值。
2.crypt()为单向加密,跟md5一样。

<&#63;php
$password='jellybool.com';
echo crypt($password);
//输出:$1$Fe0.qr5.$WOhkI4/5VPo7n7TnXHh5K
/*第二个$与第三个$之间的八个字符是由PHP生成的,每刷新一次就变一次
*/
echo "<hr>";
echo crypt($password,"jellybool");
//输出:je7fNiu1KNaEs
/*当我们要加自定义的盐值时,如例子中的jellybool作为第二个参数直接加入,
超出两位字符的会截取前两位*/
echo "<hr>";
echo crypt($password,'$1$jellybool$');
//输出:$1$jellyboo$DxH7wF7SygRpWb6XBBgfH/
/* crypt加密函数有多种盐值加密支持,以上例子展示的是MD5散列作为盐值,该方式下
盐值以$1$$的形式加入,如例子中的jellybool加在后两个$符之间,
超出八位字符的会截取前八位,总长为12位;crypt默认就是这种形式。
*/
echo "<hr>";
//crypt还有多种盐值加密支持,详见手册

Sha1加密:

string sha1 ( string $str [, bool $raw_output = false ]

1.跟md5很像,不同的是sha1()默认情况下返回40个字符的散列值,传入参数性质一样,第一个为加密的字符串,第二个为raw_output的布尔值,默认为false,如果设置为true,sha1()则会返回原始的20 位原始格式报文摘要
2.sha1()也是单行加密,没有逆向解密算法

<&#63;php
$my_intro="jellybool";
echo sha1($my_intro);
//输出:c98885c04c1208fd4d0b1dadd3bd2a9ff4d042ca
echo "<hr>";
//当然,可以将多种加密算法混合使用
echo md5(sha1($my_intro));
//输出:94f25bf9214f88b1ef065a3f9b5d9874
//这种方式的双重加密也可以提高数据的安全性

Urlencode加密:

string urlencode ( string $str )
1.一个参数,传入要加密的字符串(通常应用于对URL的加密),
2.urlencode为双向加密,可以用urldecode来加密(严格意义上来说,不算真正的加密)
3.返回字符串,此字符串中除了 -_. 之外的所有非字母数字字符都将被替换成百分号(%)后跟两位十六进制数,空格则编码为加号(+)。

<&#63;php
  //urlencode()通常用于URL中明文数据的隐藏
  $my_urlencode="jellybool.com&#63;jellybool=true + 4-3%5= \& @!";
  echo urlencode($my_urlencode);
  //输出:jellybool.com%3Fjellybool%3Dtrue+%2B+4-3%255%3D+%5C%26+%40%21
  echo "<hr>";
  $my_urldecode="jellybool.com%3Fjellybool%3Dtrue+%2B+4-3%255%3D+%5C%26+%40%21";
  echo urldecode($my_urldecode);
  //输出:jellybool.com&#63;jellybool=true + 4-3%5= \& @! 
  //还原了$my_urlencode的输出
  echo "<hr>";
  $my_urldecode="http://www.baidu.com/s&#63;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);
  /*输出:http://www.baidu.com/s&#63;word=jellybool 觉累不爱&tn=98236947_hao_pg&ie=utf-8
  没错,这就是在百度搜索jellybool 觉累不爱
  */

  /*
  =========================================================================
  解决第二个经典问题
  =========================================================================
  */
  $pre_url_encode="jellybool.com&#63;username=jellybool&password=jelly";
  //在实际开发中,我们很多时候要构造这种URL,这是没有问题的
  $url_decode  ="jellybool.com&#63;username=jelly&bool&password=jelly";
  /*注意上面两个变量的差别:第一个的username=jellybool,
              第二个为username=jelly&bool
  这种情况下用$_GET()来接受是会出问题的,这是可以用下面的方法解决 
  */
  $username="jelly&bool";
  $url_decode  ="jellybool.com&#63;username=".urlencode($username)."&password=jelly";
  //这是可以很好的解决问题

  /*
  总结一下常见的urlencode()的转换字符
    ?=> %3F
    = => %3D
    % => %25
    & => %26
    \ => %5C
    + => %2B
    空格 => +
  */

base64编码加密:

string base64decode ( string $encodeddata )
1.base64_encode()接受一个参数,也就是要编码的数据(这里不说字符串,是因为很多时候base64用来编码图片)
2.base64encode()为双向加密,可用base64decode()来解密

echo base64_encode($my_intro);
echo "<hr>";
/*输出:SmVsbHlCb29s5piv5LiA5Liq6Lqr5p2Q5pyJ6auY5bqmLOiCqeiGgOacieWuveW
6pizog7jogozmnInljprluqYs5oCd5oOz5pyJ5rex5bqm55qE5Zu95a625YWN5qOA5Lq
UQee6p+S8mOi0qOS8quWJjeerr0lU55S35bGM5Lid
*/
echo base64_decode('SmVsbHlCb29s5piv5LiA5Liq6Lqr5p2Q5pyJ6auY5bqmLOiCqeiGg
OacieWuveW6pizog7jogozmnInljprluqYs5oCd5oOz5pyJ5rex5bqm55qE5Zu95a6
25YWN5qOA5LqUQee6p+S8mOi0qOS8quWJjeerr0lU55S35bGM5Lid');

/*输出:JellyBool是一个身材有高度,肩膀有宽度,胸肌有厚度,思想有深度的国家免检五A
级优质伪前端IT男屌丝
*/

一个图片的例子:

<&#63;php
/*
一个图片的应用例子
*/
$filename="https://worktile.com/img/index/index_video.png";

$data=file_get_contents($filename);
echo base64_encode($data);
/*然后你查看网页源码就会得到一大串base64的字符串,
再用base64_decode()还原就可以得到图片
*/

本文由 JellyBool 创作,原文

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP in Action: Real-World Examples and ApplicationsPHP in Action: Real-World Examples and ApplicationsApr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: Creating Interactive Web Content with EasePHP: Creating Interactive Web Content with EaseApr 14, 2025 am 12:15 AM

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python: Comparing Two Popular Programming LanguagesPHP and Python: Comparing Two Popular Programming LanguagesApr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

The Enduring Relevance of PHP: Is It Still Alive?The Enduring Relevance of PHP: Is It Still Alive?Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP's Current Status: A Look at Web Development TrendsPHP's Current Status: A Look at Web Development TrendsApr 13, 2025 am 12:20 AM

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP vs. Other Languages: A ComparisonPHP vs. Other Languages: A ComparisonApr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP vs. Python: Core Features and FunctionalityPHP vs. Python: Core Features and FunctionalityApr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP: A Key Language for Web DevelopmentPHP: A Key Language for Web DevelopmentApr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment