萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> php編程 >> PHP實現AES256加密算法實例

PHP實現AES256加密算法實例

 本文實例講述了PHP實現AES256加密算法的方法,是較為常見的一種加密算法。分享給大家供大家參考。具體如下:

aes.class.php文件如下:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 <?php  /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */  /* AES implementation in PHP (c) Chris Veness 2005-2011. 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.gladman.plus.com/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); // 'b' is a•{02} in GF(2^8) 
copyright © 萬盛學電腦網 all rights reserved