Home Backend Development PHP Tutorial PHP AES encryption study notes

PHP AES encryption study notes

Nov 29, 2016 am 10:33 AM
php

<?php  
//--------第一种AES-CBC加密方案--------  
//仅为理解之用  
$cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128, &#39;&#39;, MCRYPT_MODE_CBC, &#39;&#39;); #128位 = 16字节 iv必须16字节  
$key128 = &#39;1234567890123456&#39;;  
$iv =  &#39;1234567890123456&#39;;  
$cleartext = &#39;hello&#39;; #待加密的字符串  
if (mcrypt_generic_init($cipher, $key128, $iv) != -1)  
{  
    // PHP pads with NULL bytes if $cleartext is not a multiple of the block size..  
    //如果$cleartext不是128位也就是16字节的倍数,补充NULL字符满足这个条件,返回的结果的长度一样  
    $cipherText = mcrypt_generic($cipher,$cleartext );  
    mcrypt_generic_deinit($cipher);  
      
    // Display the result in hex.  
    //很明显,结果也是16字节的倍数.1个字节用两位16进制表示,所以下面输出的是32的倍数位16进制的字符串   
    echo &#39;第一种AES加密方案:<br>&#39;;  
    printf("128-bit encrypted result:\n%s\n\n",bin2hex($cipherText));  
    echo &#39;<br>&#39;;echo &#39;<br>&#39;;  
}  
//--------第一种AES加密方案--------  
?> 
转载来源:http://www.chilkatsoft.com/p/php_aes.asp http://www.cnblogs.com/adylee/archive/2007/09/14/893438.html 转载来源:http://blog.csdn.net/shushengsky/archive/2009/12/13/4961861.aspx
<?php  
//--------第二种AES-ECB加密方案--------  
//加密  
echo &#39;第二种AES加密方案:<br>&#39;;  
$key = &#39;1234567890123456&#39;;  
$content = &#39;hello&#39;;  
$padkey = pad2Length($key,16);  
$cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128, &#39;&#39;, MCRYPT_MODE_ECB, &#39;&#39;);  
$iv_size = mcrypt_enc_get_iv_size($cipher);  
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); #IV自动生成?  
echo &#39;自动生成iv的长度:&#39;.strlen($iv).&#39;位:&#39;.bin2hex($iv).&#39;<br>&#39;;  
if (mcrypt_generic_init($cipher, pad2Length($key,16), $iv) != -1)  
{  
    // PHP pads with NULL bytes if $content is not a multiple of the block size..  
    $cipherText = mcrypt_generic($cipher,pad2Length($content,16) );  
    mcrypt_generic_deinit($cipher);  
    mcrypt_module_close($cipher);  
     
    // Display the result in hex.  
    printf("128-bit encrypted result:\n%s\n\n",bin2hex($cipherText));  
    print("<br />");  
     
}  
//解密  
$mw = bin2hex($cipherText);  
$td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, &#39;&#39;, MCRYPT_MODE_ECB, &#39;&#39;);  
if (mcrypt_generic_init($td, $padkey, $iv) != -1)  
{  
    $p_t = mdecrypt_generic($td, hexToStr($mw));  
    mcrypt_generic_deinit($td);  
    mcrypt_module_close($td);  
     
    $p_t = trimEnd($p_t);  
    echo &#39;解密:&#39;;  
    print($p_t);  
    print("<br />");  
    print(bin2hex($p_t));  
    echo &#39;<br><br>&#39;;  
}  
//将$text补足$padlen倍数的长度  
function pad2Length($text, $padlen){  
    $len = strlen($text)%$padlen;  
    $res = $text;  
    $span = $padlen-$len;  
    for($i=0; $i<$span; $i++){  
        $res .= chr($span);  
    }  
    return $res;  
}  
//将解密后多余的长度去掉(因为在加密的时候 补充长度满足block_size的长度)  
function trimEnd($text){  
    $len = strlen($text);  
    $c = $text[$len-1];  
    if(ord($c) <$len){  
        for($i=$len-ord($c); $i<$len; $i++){  
            if($text[$i] != $c){  
                return $text;  
            }  
        }  
        return substr($text, 0, $len-ord($c));  
    }  
    return $text;  
}  
//16进制的转为2进制字符串  
function hexToStr($hex)   
{   
    $bin="";   
    for($i=0; $i<strlen($hex)-1; $i+=2)   
    {  
        $bin.=chr(hexdec($hex[$i].$hex[$i+1]));   
    }  
    return $bin;   
}  
//--------第二种AES加密方案--------  
<?php  
//--------第三种AES-ECB加密方案--------  
echo &#39;第三种AES加密方案:<br>&#39;;  
$key = &#39;1234567890123456&#39;;  
$key = pad2Length($key,16);  
$iv = &#39;asdff&#39;;  
$content = &#39;hello&#39;;  
$content = pad2Length($content,16);  
$AESed =  bin2hex( mcrypt_encrypt(MCRYPT_RIJNDAEL_128,$key,$content,MCRYPT_MODE_ECB,$iv) ); #加密  
echo "128-bit encrypted result:".$AESed.&#39;<br>&#39;;  
$jiemi = mcrypt_decrypt(MCRYPT_RIJNDAEL_128,$key,hexToStr($AESed),MCRYPT_MODE_ECB,$iv); #解密  
echo &#39;解密:&#39;;  
echo trimEnd($jiemi);  
//--------第三种AES加密方案--------  
?>
#KEY长度无限制,IV长度必须是block_size的倍数
#如果是MCRYPT_MODE_ECB加密,结果与KEY有关,与IV无关
#如果是MCRYPT_MODE_CBC加密,结果与KEY有关,与IV有关
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
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 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)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles