600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > PHP实现AES256加密算法实例 aes256加密算法实例

PHP实现AES256加密算法实例 aes256加密算法实例

时间:2022-10-18 03:30:41

相关推荐

PHP实现AES256加密算法实例 aes256加密算法实例

php教程|php手册

PHP,加密,算法,加密算法

php教程-php手册

PHP实现AES256加密算法实例,aes256加密算法实例

15.05.1源码,vscode 代码飘红,ubuntu挂起后,tomcat接受tcp,sqlite判断是否有表,前端用什么框架做开发工具,爬虫关键词搜索步骤,oss 上传 php,短视频seo方案,php音乐网站源码,网页图片飞入特效,单页面网站模板lzw

本文实例讲述了PHP实现AES256加密算法的方法,是较为常见的一种加密算法。分享给大家供大家参考。具体如下:

体育赛事源码,vscode下载下载失败,光盘恢复 ubuntu,tomcat加入项目编译,qt-sqlite停车场项目,怀孕五个月吃了小爬虫药,redhat安装php,烟台seo推广哪家便宜,电影院网站模板,oa网站模板下载lzw

aes.class.php文件如下:

点滴记忆源码,怎么设置vscode为中文,ubuntu 检查pci,启动tomcat配置jdk,爬虫 参数验证,php过滤nbsp,荆州百度seo技巧,内容扫码打赏后才能看的网站源码,查看discuz模板lzw

<?php /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ /* AES implementation in PHP (c) Chris Veness -. Right of free use is granted for all */ /* commercial or non-commercial use under CC-BY licence. No warranty of any form is offered. */ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ class Aes { /** * AES Cipher function: encrypt input with Rijndael algorithm * * @param input message as byte-array (16 bytes) * @param w key schedule as 2D byte-array (Nr+1 x Nb bytes) - * generated from the cipher key by keyExpansion() * @return ciphertext as byte-array (16 bytes) */ public static function cipher($input, $w) { // main cipher function [§5.1] $Nb = 4; // block size (in words): no of columns in state (fixed at 4 for AES) $Nr = count($w)/$Nb - 1; // no of rounds: 10/12/14 for 128/192/256-bit keys$state = array(); // initialise 4xNb byte-array state with input [§3.4] for ($i=0; $i<4*$Nb; $i++) $state[$i%4][floor($i/4)] = $input[$i];$state = self::addRoundKey($state, $w, 0, $Nb);for ($round=1; $round<$Nr; $round++) { // apply Nr rounds $state = self::subBytes($state, $Nb); $state = self::shiftRows($state, $Nb); $state = self::mixColumns($state, $Nb); $state = self::addRoundKey($state, $w, $round, $Nb); }$state = self::subBytes($state, $Nb); $state = self::shiftRows($state, $Nb); $state = self::addRoundKey($state, $w, $Nr, $Nb);$output = array(4*$Nb); // convert state to 1-d array before returning [§3.4] for ($i=0; $i<4*$Nb; $i++) $output[$i] = $state[$i%4][floor($i/4)]; return $output; }private static function addRoundKey($state, $w, $rnd, $Nb) { // xor Round Key into state S [§5.1.4] for ($r=0; $r<4; $r++) { for ($c=0; $c<$Nb; $c++) $state[$r][$c] ^= $w[$rnd*4+$c][$r]; } return $state; } private static function subBytes($s, $Nb) { // apply SBox to state S [§5.1.1] for ($r=0; $r<4; $r++) { for ($c=0; $c<$Nb; $c++) $s[$r][$c] = self::$sBox[$s[$r][$c]]; } return $s; } private static function shiftRows($s, $Nb) { // shift row r of state S left by r bytes [§5.1.2] $t = array(4); for ($r=1; $r<4; $r++) { for ($c=0; $c<4; $c++) $t[$c] = $s[$r][($c+$r)%$Nb]; // shift into temp copy for ($c=0; $c<4; $c++) $s[$r][$c] = $t[$c];// and copy back }// note that this will work for Nb=4,5,6, but not 7,8 (always 4 for AES): return $s; // see fp./cryptography_technology/rijndael/aes.spec.311.pdf } private static function mixColumns($s, $Nb) { // combine bytes of each col of state S [§5.1.3] for ($c=0; $c<4; $c++) { $a = array(4); // a is a copy of the current column from s $b = array(4); //  is a•{02} in GF(2^8) for ($i=0; $i<4; $i++) {$a[$i] = $s[$i][$c];$b[$i] = $s[$i][$c]&0x80 ? $s[$i][$c]<<1 ^ 0x011b : $s[$i][$c]<<1; } // a[n] ^ b[n] is a•{03} in GF(2^8) $s[0][$c] = $b[0] ^ $a[1] ^ $b[1] ^ $a[2] ^ $a[3]; // 2*a0 + 3*a1 + a2 + a3 $s[1][$c] = $a[0] ^ $b[1] ^ $a[2] ^ $b[2] ^ $a[3]; // a0 * 2*a1 + 3*a2 + a3 $s[2][$c] = $a[0] ^ $a[1] ^ $b[2] ^ $a[3] ^ $b[3]; // a0 + a1 + 2*a2 + 3*a3 $s[3][$c] = $a[0] ^ $b[0] ^ $a[1] ^ $a[2] ^ $b[3]; // 3*a0 + a1 + a2 + 2*a3 } return $s; } /** * Key expansion for Rijndael cipher(): performs key expansion on cipher key * to generate a key schedule * * @param key cipher key byte-array (16 bytes) * @return key schedule as 2D byte-array (Nr+1 x Nb bytes) */ public static function keyExpansion($key) { // generate Key Schedule from Cipher Key [§5.2] $Nb = 4; // block size (in words): no of columns in state (fixed at 4 for AES) $Nk = count($key)/4; // key length (in words): 4/6/8 for 128/192/256-bit keys $Nr = $Nk + 6; // no of rounds: 10/12/14 for 128/192/256-bit keys$w = array(); $temp = array();for ($i=0; $i<$Nk; $i++) { $r = array($key[4*$i], $key[4*$i+1], $key[4*$i+2], $key[4*$i+3]); $w[$i] = $r; }for ($i=$Nk; $i<($Nb*($Nr+1)); $i++) { $w[$i] = array(); for ($t=0; $t<4; $t++) $temp[$t] = $w[$i-1][$t]; if ($i % $Nk == 0) {$temp = self::subWord(self::rotWord($temp));for ($t=0; $t 6 && $i%$Nk == 4) {$temp = self::subWord($temp); } for ($t=0; $t<4; $t++) $w[$i][$t] = $w[$i-$Nk][$t] ^ $temp[$t]; } return $w; } private static function subWord($w) { // apply SBox to 4-byte word w for ($i=0; $i<4; $i++) $w[$i] = self::$sBox[$w[$i]]; return $w; } private static function rotWord($w) { // rotate 4-byte word w left by one byte $tmp = $w[0]; for ($i=0; $i

aesctr.class.php文件如下:

<?php /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ /* AES counter (CTR) mode implementation in PHP (c) Chris Veness -. Right of free use is */ /* granted for all commercial or non-commercial use under CC-BY licence. No warranty of any */ /* form is offered.*/ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ class AesCtr extends Aes { /** * Encrypt a text using AES encryption in Counter mode of operation * - see http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf * * Unicode multi-byte character safe * * @param plaintext source text to be encrypted * @param password the password to use to generate a key * @param nBits number of bits to be used in the key (128, 192, or 256) * @param keep keep 1:each not change 0:each change(default) * @returnencrypted text */ public static function encrypt($plaintext, $password, $nBits, $keep=0) { $blockSize = 16; // block size fixed at 16 bytes / 128 bits (Nb=4) for AES if (!($nBits==128 || $nBits==192 || $nBits==256)) return \; // standard allows 128/192/256 bit keys // note PHP (5) gives us plaintext and password in UTF8 encoding!// use AES itself to encrypt password to get cipher key (using plain password as source for // key expansion) - gives us well encrypted key $nBytes = $nBits/8; // no bytes in key $pwBytes = array(); for ($i=0; $i<$nBytes; $i++) $pwBytes[$i] = ord(substr($password,$i,1)) & 0xff; $key = Aes::cipher($pwBytes, Aes::keyExpansion($pwBytes)); $key = array_merge($key, array_slice($key, 0, $nBytes-16)); // expand key to 16/24/32 bytes long// initialise 1st 8 bytes of counter block with nonce (NIST SP800-38A §B.2): [0-1] = millisec, // [2-3] = random, [4-7] = seconds, giving guaranteed sub-ms uniqueness up to Feb 2106 $counterBlock = array(); if($keep==0){$nonce = floor(microtime(true)*1000); // timestamp: milliseconds since 1-Jan-1970$nonceMs = $nonce%1000;$nonceSec = floor($nonce/1000);$nonceRnd = floor(rand(0, 0xffff)); }else{$nonce = 10000;$nonceMs = $nonce%1000;$nonceSec = floor($nonce/1000);$nonceRnd = 10000; }for ($i=0; $i<2; $i++) $counterBlock[$i] = self::urs($nonceMs, $i*8) & 0xff; for ($i=0; $i<2; $i++) $counterBlock[$i+2] = self::urs($nonceRnd, $i*8) & 0xff; for ($i=0; $i<4; $i++) $counterBlock[$i+4] = self::urs($nonceSec, $i*8) & 0xff;// and convert it to a string to go on the front of the ciphertext $ctrTxt = \; for ($i=0; $i<8; $i++) $ctrTxt .= chr($counterBlock[$i]);// generate key schedule - an expansion of the key into distinct Key Rounds for each round $keySchedule = Aes::keyExpansion($key); //print_r($keySchedule);$blockCount = ceil(strlen($plaintext)/$blockSize); $ciphertxt = array(); // ciphertext as array of stringsfor ($b=0; $b<$blockCount; $b++) { // set counter (block #) in last 8 bytes of counter block (leaving nonce in 1st 8 bytes) // done in two stages for 32-bit ops: using two words allows us to go past 2^32 blocks (68GB) for ($c=0; $c<4; $c++) $counterBlock[15-$c] = self::urs($b, $c*8) & 0xff; for ($c=0; $c<4; $c++) $counterBlock[15-$c-4] = self::urs($b/0x100000000, $c*8);$cipherCntr = Aes::cipher($counterBlock, $keySchedule); // -- encrypt counter block --// block size is reduced on final block $blockLength = $b<$blockCount-1 ? $blockSize : (strlen($plaintext)-1)%$blockSize+1; $cipherByte = array(); for ($i=0; $i<$blockLength; $i++) { // -- xor plaintext with ciphered counter byte-by-byte --$cipherByte[$i] = $cipherCntr[$i] ^ ord(substr($plaintext, $b*$blockSize+$i, 1));$cipherByte[$i] = chr($cipherByte[$i]); } $ciphertxt[$b] = implode(\, $cipherByte); // escape troublesome characters in ciphertext }// implode is more efficient than repeated string concatenation $ciphertext = $ctrTxt . implode(\, $ciphertxt); $ciphertext = base64_encode($ciphertext); return $ciphertext; } /** * Decrypt a text encrypted by AES in counter mode of operation * * @param ciphertext source text to be decrypted * @param password the password to use to generate a key * @param nBits number of bits to be used in the key (128, 192, or 256) * @returndecrypted text */ public static function decrypt($ciphertext, $password, $nBits) { $blockSize = 16; // block size fixed at 16 bytes / 128 bits (Nb=4) for AES if (!($nBits==128 || $nBits==192 || $nBits==256)) return \; // standard allows 128/192/256 bit keys $ciphertext = base64_decode($ciphertext);// use AES to encrypt password (mirroring encrypt routine) $nBytes = $nBits/8; // no bytes in key $pwBytes = array(); for ($i=0; $i<$nBytes; $i++) $pwBytes[$i] = ord(substr($password,$i,1)) & 0xff; $key = Aes::cipher($pwBytes, Aes::keyExpansion($pwBytes)); $key = array_merge($key, array_slice($key, 0, $nBytes-16)); // expand key to 16/24/32 bytes long// recover nonce from 1st element of ciphertext $counterBlock = array(); $ctrTxt = substr($ciphertext, 0, 8); for ($i=0; $i<8; $i++) $counterBlock[$i] = ord(substr($ctrTxt,$i,1));// generate key schedule $keySchedule = Aes::keyExpansion($key);// separate ciphertext into blocks (skipping past initial 8 bytes) $nBlocks = ceil((strlen($ciphertext)-8) / $blockSize); $ct = array(); for ($b=0; $b<$nBlocks; $b++) $ct[$b] = substr($ciphertext, 8+$b*$blockSize, 16); $ciphertext = $ct; // ciphertext is now array of block-length strings// plaintext will get generated block-by-block into array of block-length strings $plaintxt = array();for ($b=0; $b<$nBlocks; $b++) { // set counter (block #) in last 8 bytes of counter block (leaving nonce in 1st 8 bytes) for ($c=0; $c<4; $c++) $counterBlock[15-$c] = self::urs($b, $c*8) & 0xff; for ($c=0; $c<4; $c++) $counterBlock[15-$c-4] = self::urs(($b+1)/0x100000000-1, $c*8) & 0xff;$cipherCntr = Aes::cipher($counterBlock, $keySchedule); // encrypt counter block$plaintxtByte = array(); for ($i=0; $i>> operator nor unsigned ints * * @param a number to be shifted (32-bit integer) * @param b number of bits to shift a to the right (0..31) * @return a right-shifted and zero-filled by b bits */ private static function urs($a, $b) { $a &= 0xffffffff; $b &= 0x1f; // (bounds check) if ($a&0x80000000 && $b>0) { // if left-most bit set $a = ($a>>1) & 0x7fffffff; // right-shift one bit & clear left-most bit $a = $a >> ($b-1);// remaining right-shifts } else { // otherwise $a = ($a>>$b); // use normal right-shift } return $a; } } ?>

Demo实例程序如下:

<?php require aes.class.php; // AES PHP implementation require aesctr.class.php; // AES Counter Mode implementation echo each change

; $mstr = AesCtr::encrypt(Hello World, key, 256); echo "Encrypt String : $mstr

"; $dstr = AesCtr::decrypt($mstr, key, 256); echo "Decrypt String : $dstr

"; echo each not change

; $mstr = AesCtr::encrypt(Hello World, key, 256, 1); // keep=1 echo "Encrypt String : $mstr

"; $dstr = AesCtr::decrypt($mstr, key, 256); echo "Decrypt String : $dstr

"; ?>

这里再介绍另一使用 PHP mcrypt 加解密方法:

/* aes 256 encrypt * @param String $ostr * @param String $securekey * @param String $type encrypt, decrypt */ function aes($ostr, $securekey, $type=encrypt){ if($ostr==\){return \; }$key = $securekey; $iv = strrev($securekey); $td = mcrypt_module_open( ijndael-256, \, ofb, \); mcrypt_generic_init($td, $key, $iv); $str = \; switch($type){case encrypt: $str = base64_encode(mcrypt_generic($td, $ostr)); break;case decrypt: $str = mdecrypt_generic($td, base64_decode($ostr)); break; } mcrypt_generic_deinit($td); return $str; } // Demo $key = "fdipzone14showmethemoney!@#$"; $str = "show me the money"; $ostr = aes($str, $key); echo "String 1: $ostr

"; $dstr = aes($ostr, $key, decrypt); echo "String 2: $dstr

";

PHP怎实现AES加解密

php加载Mcrypt组件php_mycrypt.dll/.so,支持AES和3DES编码,

只是该模块没有提供补齐padding方法,要自己用PHP代码写PKCS7之类的补齐方法

php加密算法的一些疑问

1、加密算法是MCRYPT_RIJNDAEL_128,至于是不是你说的AES,就不好说了。我个人认为应该不是。毕竟两者长得不太像。

2、代码没有硬伤,但是所有加密都有可能被破的,穷举法耗时问题而已。

3、IV用于初始化算法用的。一样需要保密。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。