Table of Contents
数据加密可以简单的理解为:明文(文件或者数据)-->算法处理-->不可读的密文,进而达到加密的效果。
php中的几种加密方式
Home Backend Development PHP Tutorial PHP加密与实际应用

PHP加密与实际应用

Jun 20, 2016 pm 12:45 PM

数据加密可以简单的理解为:明文(文件或者数据)-->算法处理-->不可读的密文,进而达到加密的效果。

php中的几种加密方式

  • md5加密算法

  • crypt算法

  • sha1加密算法

  • URL编码技术编码

  • base64编码

  • 其中 md5、crypt、sha1 都是单向加密算法 (对不同长度的信息进行散列计算,得到固定长度的输出,这个过程是单向的,不能通过对固定长度的输出通过计算得到输入信息)。

    md5()加密算法

    string md5 ( string $str [, bool $raw_output = false ] )
    以 32 字符十六进制数字形式返回散列值。
    如果可选的 raw_output 被设置为 TRUE,那么 MD5 报文摘要将以原始的 16 位二进制格式返回。

        header("Content-type:text/html;charset=utf-8");    $name = "yuesir";    echo md5($name);    echo "<hr/>";    echo md5($name, true);
    Copy after login
    e217951255a1f0b2c9d8fea477af795e??U???????w?y^
    Copy after login

    Crypt()加密算法

    string crypt ( string $str [, string $salt ] )
    $salt是加密是的干扰码,使编码更安全;可选的盐值字符串。如果没有提供,算法行为将由不同的算法实现决定,并可能导致不可预料的结果
    crypt() 返回一个基于标准 UNIX DES 算法或系统上其他可用的替代算法的散列字符串。
    如果没有提供盐值,PHP 将自动生成一个 2 个字符(DES)或者 12 个字符(MD5)的盐值

    note:

  • 如果加密是没有加上 $salt 这个参数,将随机生成一个干扰码,否则刷新加密密文不变

  •     header("Content-type:text/html;charset=utf-8");    $name = "yuesir";    echo crypt($name);    echo "<hr/>";    echo crypt($name, 'hello');
    Copy after login
        $1$BG2.0N3.$zysIbnXYFkPyqr9g8XFo/1    heS64YGnAn6Wc
    Copy after login
    **$1$BG2.0N3.$** 是通过 CRYPT_MD5 生成的散列值特征是以 $1$开头,以$结束,其间有不超过8位的随机字符,$之后的是密文正文
    Copy after login

    sha1() 加密算法

    string sha1 ( string $str [, bool $raw_output = false ] )
    如果可选的 raw_output 参数被设置为 TRUE,那么 sha1 摘要将以 20 字符长度的原始格式返回,否则返回值是一个 40 字符长度的十六进制数字。

        header("Content-type:text/html;charset=utf-8");    $name = "yuesir";    echo sha1($name);    echo "<hr/>";    echo sha1($name, 'hello');
    Copy after login
        1b15630e04990268e3f64c32a119417642fb98d0    c?h??L2?AvB???
    Copy after login
        和md5()差不多,但返回的字符串长度更长(40位)    由于此函数依赖的算法已不足够复杂,不推荐使用此函数对明文密码加密
    Copy after login

    URL编码技术

    string urlencode ( string $str )
    返回字符串,此字符串中除了 -_. 之外的所有非字母数字字符都将被替换成百分号(%)后跟两位十六进制数,空格则编码为加号(+)

        header("Content-type:text/html;charset=utf-8");    $url = "http://yufu.me?q=hello world + 你好&username=&amp";    echo urlencode($url) . "<br/>";    echo "<a href='".urlencode($url)."'>点我</a>";
    Copy after login
    http%3A%2F%2Fyufu.me%3Fq%3Dhello+world+%2B+%E4%BD%A0%E5%A5%BD%26username%3D%26amp点我
    Copy after login

    urldecode

    string urldecode ( string $str )
    解码已编码的 URL 字符串, 解码给出的已编码字符串中的任何 %##。返回解码后的字符串

        header("Content-type:text/html;charset=utf-8");    $url = "http://yufu.me?q=hello world + 你好&username=&amp";    echo urlencode($url) . "<hr/>";    echo    urldecode("http%3A%2F%2Fyufu.me%3Fq%3Dhello+world+%2B+%E4%BD%A0%E5%A5%BD%26username%3D%26amp");----------http%3A%2F%2Fyufu.me%3Fq%3Dhello+world+%2B+%E4%BD%A0%E5%A5%BD%26username%3D%26amphttp://yufu.me?q=hello world + 你好&username=&
    Copy after login

    注意到 username 的值& 被浏览器解析成了 &
    解决方法是:

    较为简单的解决办法是使用 & 代替 & 作为分隔符。你不需要为此修改 PHP 的 arg_separator。让它仍为 &,而仅使用 htmlentities() 或 htmlspecialchars() 对你的 URL 进行编码。

    base64加密技术

    string base64_encode ( string $data )
    使用 MIME base64 对数据进行编码, 是为了使二进制数据可以通过非纯 8-bit 的传输层传输,例如电子邮件的主体; Base64-encoded 数据要比原始数据多占用 33% 左右的空间

    base64_decode
    string base64_decode ( string $encoded_data )
    对使用 MIME base64 编码的数据进行解码,返回原始数据,失败则返回 FALSE。返回的数据可能是二进制的

        $img_path = 'image/1_meitu_1.jpg';    $data = file_get_contents($img_path);    echo base64_encode($data);    echo "<img  src='data:image/jpeg;base64,".base64_encode($data)."'/ alt="PHP加密与实际应用" >";
    Copy after login
    Statement of this Website
    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

    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)
    2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    Repo: How To Revive Teammates
    4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    Hello Kitty Island Adventure: How To Get Giant Seeds
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

    Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

    Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

    Following its high-profile acquisition by Facebook in 2012, Instagram adopted two sets of APIs for third-party use. These are the Instagram Graph API and the Instagram Basic Display API.As a developer building an app that requires information from a

    Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

    Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

    Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

    This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

    Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

    Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

    cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

    The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

    12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

    Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

    Announcement of 2025 PHP Situation Survey Announcement of 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

    The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

    See all articles