Home Backend Development PHP Tutorial PHP使用DES进行加密与解密的方法详解_PHP

PHP使用DES进行加密与解密的方法详解_PHP

Jun 01, 2016 pm 12:06 PM
des encrypt and decode

DES是一种标准的数据加密算法,关于这个算法的详细介绍可以参考wiki和百度百科:

wiki百科    百度百科

php中有一个扩展可以支持DES的加密算法,是:extension=php_mcrypt.dll

在配置文件中将这个扩展打开还不能够在windows环境下使用

需要将PHP文件夹下的 libmcrypt.dll 拷贝到系统的 system32 目录下,这是通过phpinfo可以查看到mcrypt表示这个模块可以正常试用了。

下面是PHP中使用DES加密解密的一个例子:
复制代码 代码如下:
//$input - stuff to decrypt
    //$key - the secret key to use

    function do_mencrypt($input, $key)
    {
        $input = str_replace(""n", "", $input);
        $input = str_replace(""t", "", $input);
        $input = str_replace(""r", "", $input);
        $key = substr(md5($key), 0, 24);
        $td = mcrypt_module_open('tripledes', '', 'ecb', '');
        $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
        mcrypt_generic_init($td, $key, $iv);
        $encrypted_data = mcrypt_generic($td, $input);
        mcrypt_generic_deinit($td);
        mcrypt_module_close($td);
        return trim(chop(base64_encode($encrypted_data)));
    }

    //$input - stuff to decrypt
    //$key - the secret key to use

    function do_mdecrypt($input, $key)
    {
        $input = str_replace(""n", "", $input);
        $input = str_replace(""t", "", $input);
        $input = str_replace(""r", "", $input);
        $input = trim(chop(base64_decode($input)));
        $td = mcrypt_module_open('tripledes', '', 'ecb', '');
        $key = substr(md5($key), 0, 24);
        $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
        mcrypt_generic_init($td, $key, $iv);
        $decrypted_data = mdecrypt_generic($td, $input);
        mcrypt_generic_deinit($td);
        mcrypt_module_close($td);
        return trim(chop($decrypted_data));

    }  

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 Article Tags

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)

How to add Win11 encryption function to the right-click menu? How to add Win11 encryption and decryption right-click shortcut How to add Win11 encryption function to the right-click menu? How to add Win11 encryption and decryption right-click shortcut Jan 07, 2024 am 08:45 AM

How to add Win11 encryption function to the right-click menu? How to add Win11 encryption and decryption right-click shortcut

Encrypt and decrypt sensitive data using Yii framework middleware Encrypt and decrypt sensitive data using Yii framework middleware Jul 28, 2023 pm 07:12 PM

Encrypt and decrypt sensitive data using Yii framework middleware

Analysis of encryption and decryption examples of PHP and Java des Analysis of encryption and decryption examples of PHP and Java des Apr 29, 2023 am 09:40 AM

Analysis of encryption and decryption examples of PHP and Java des

Encryption and decryption implementation method developed in PHP in WeChat applet Encryption and decryption implementation method developed in PHP in WeChat applet Jun 01, 2023 am 08:12 AM

Encryption and decryption implementation method developed in PHP in WeChat applet

Data encryption and decryption using React Query and database Data encryption and decryption using React Query and database Sep 26, 2023 pm 12:53 PM

Data encryption and decryption using React Query and database

PHP mailbox development: implementing email encryption and decryption functions PHP mailbox development: implementing email encryption and decryption functions Sep 12, 2023 am 10:40 AM

PHP mailbox development: implementing email encryption and decryption functions

Example of data encryption and decryption in PHP Tencent Cloud Server API interface docking Example of data encryption and decryption in PHP Tencent Cloud Server API interface docking Jul 05, 2023 pm 06:16 PM

Example of data encryption and decryption in PHP Tencent Cloud Server API interface docking

In-depth study of network communication encryption and decryption of swoole development functions In-depth study of network communication encryption and decryption of swoole development functions Aug 08, 2023 am 08:13 AM

In-depth study of network communication encryption and decryption of swoole development functions

See all articles