PHP Code cashing AES Encryption

Source: Internet
Author: User
Tags pkcs5
PHP code for AES Encryption
found that the PHP4 encryption module in the PHP5 is not supported, hard to add this module will cause the Boot Time Warning dialog box.
So it took a day to translate the AS3 encryption class into PHP, which is Google's Open source project
http://code.google.com/p/as3crypto/


 _pad = new PKCS5 (); For consistency with Java, the PKCS5 is populated with $key = Hex::string2bytearray (Base64_decode ($base 64keyString)); $this->_mode = new Ecbmode (New Aeskey ($key), $this->_pad), $this->_pad->setblocksize ($this->_mode->getblocksize ());} /** * Encrypt plaintext into ciphertext base64 encoded String * @paramplainSrc plaintext * @return ciphertext base64 encoded */public function Encrypt ($PLAINSRC) {$src = Hex::string2 ByteArray ($PLAINSRC); $src = $this->_mode->encrypt ($SRC); return Base64_encode (Hex::bytearray2string ($SRC));} /** * Decrypt Base64 encoded string (ciphertext) into plaintext * @parambase64Src ciphertext base64 encoded String * @return plaintext */public function decrypt ($base 64Src) {$src = bas E64_decode ($base 64Src); $src = $this->_mode->decrypt (Hex::string2bytearray ($SRC)); return Hex:: Bytearray2string ($SRC);} /** * Free memory */public function Dispose () {$this->_mode->dispose ()}} Var_dump (Hex::string2bytearray (Base64_decode ("mk2x82el6jkkbzvlju1zmr6rcko+sbhmbpomfd/2mxw=")); $_aes = new Aes ( "mk2x82el6jkkbzvlju1zmr6rcko+sbhmbpomfd/2mxw=");//echo "=================
"; $ret = $_aes->encrypt (" 1234567890abcdef1234567890abcdefaaafdsfsdffasfasfasfasdf "); Echo $ret; Var_dump ($_aes- >decrypt ($ret));? >


 
  BlockSize = $blockSize;} Public Function pad ($a) {$c = $this->blocksize-count ($a)% $this->blocksize;for ($i =0; $i < $c; + + $i) {$a [] = $c;} return $a;}  Public function Unpad ($a) {$len = count ($a) $c = $len% $this->blocksize;if ($c!=0) throw new Exception ("Pkcs#5::unpad: Bytearray.length ' t a multiple of the blockSize '); $c = $a [$len -1];array_splice ($a, $len-$c, $c); return $a;} Public Function Setblocksize ($bs) {$this->blocksize = $bs;}}? >




 _tempbyte = Array (), $this->_statebyte = Array (), $this->_keylength = count ($key), $this->_keybyte = $key; $this- >expandkey ();} Produce Nb bytes for each roundprivate function Expandkey () {$tmp 0=0; $tmp 1=0; $tmp 2=0; $tmp 3=0; $tmp 4=0; $idx =0; $Nk = $thi S->_keylength/4;//echo ("Count:". $Nk. "
". Count ($this->_keybyte)."
$this->_nnr = $Nk +6;$_keybyte = $this->_keybyte;$_nnr = $this->_nnr; $Nb = aeskeyinclude:: $Nb; $Sbox = Aeskeyinclude::$_sbox; $Rcon = aeskeyinclude::$_rcon;for ($idx = $Nk; $idx < $Nb * ($_nnr + 1); $idx + +) {$tmp 0 = $_keyb yte[4* $idx-4]; $tmp 1 = $_keybyte[4* $idx-3]; $tmp 2 = $_keybyte[4* $idx 2]; $tmp 3 = $_keybyte[4* $idx -1];if (! ( $idx% $Nk)) {$tmp 4 = $tmp 3; $tmp 3 = $Sbox [$tmp 0]; $tmp 0 = $Sbox [$tmp 1] ^ $Rcon [$idx/$Nk]; $tmp 1 = $Sbox [$tmp 2]; $tmp 2 = $Sbo x[$tmp 4];} else if ($Nk > 6 && $idx% $Nk = = 4) {$tmp 0 = $Sbox [$tmp 0]; $tmp 1 = $Sbox [$tmp 1]; $tmp 2 = $Sbox [$tmp 2]; $tmp 3 = $ sbox[$tmp 3];} $_keybyte[4* $idx +0] = $_keybyte[4* $idx -4* $Nk + 0] ^ $tmp 0;$_keybyte[4* $idx +1] = $_keybyte[4* $idx -4* $Nk + 1] ^ $tmp 1;$_ keybyte[4* $idx +2] = $_keybyte[4* $idx -4* $Nk + 2] ^ $tmp 2;$_keybyte[4* $idx +3] = $_keybyte[4* $idx -4* $Nk + 3] ^ $tmp 3;} $this->_keybyte = $_keybyte;} Public Function Getblocksize () {return 16;} Encrypt one-bit blockpublic function encrypt ($block, $index =0) {$_statebyte = & $this->_statebyte;$_keybyte = & $this->_keybyte; $Nb = aeskeyinclude:: $Nb; $_nnr = $this- >_nnr;$_statebyte = Array (); $_statebyte = Array_merge ($_statebyte,array_slice ($block, $index, $Nb)); $this AddRoundKey ($_keybyte, 0); for ($round = 1; $round < $_nnr + 1; + + $round) {if ($round < $_nnr) {$this->mixsubco Lumns ();} else {$this->shiftrows ();} $this->addroundkey ($_keybyte, $round * $Nb * 4);} return $_statebyte;} Public function Decrypt ($block, $index =0) {$_statebyte = & $this->_statebyte;$_keybyte = & $this->_keybyte ; $Nb = aeskeyinclude:: $Nb; $_nnr = $this->_nnr;$_statebyte = Array (); $_statebyte = Array_merge ($_statebyte,array_ Slice ($block, $index, $Nb)) $this->addroundkey ($_keybyte, $_nnr* $Nb); $this->invshiftrows (); for ($round = $ _nnr; $round--; {$this->addroundkey ($_keybyte, $round * $Nb), if ($round) {$this->invmixsubcolumns ();}} return $_statebyte;} Public function Dispose () {}//exchanges columns in each of 4 Rows//row0-unchanged, row1-shifted left 1,//row2-shifted left 2 and row3-shifted left 3protected function Shiftro WS () {$tmp = 0;$_statebyte = & $this->_statebyte; $Sbox = aeskeyinclude::$_sbox;//just substitute row 0$_statebyte[ 0] = $Sbox [$_statebyte[0]]; $_STATEBYTE[4] = $Sbox [$_statebyte[4]];$_statebyte[8] = $Sbox [$_statebyte[8]]; $_STATEBYTE[12] = $Sbox [$_statebyte[12]];//rotate row 1$tmp = $Sbox [$_statebyte[1]]; $_STATEBYTE[1] = $Sbox [$_statebyte[5]];$_statebyte[5] = $Sbox [$_statebyte[9]]; $_STATEBYTE[9] = $Sbox [$_statebyte[13]]; $_STATEBYTE[13] = $tmp;//rotate row 2$tmp = $Sbox [$_statebyte[2]]; $_STATEBYTE[2] = $Sbox [$_statebyte[10]]; $_STATEBYTE[10] = $tmp; $tmp = $Sbox [$_statebyte[6]]; $_STATEBYTE[6] = $Sbox [$_statebyte[14]]; $_STATEBYTE[14] = $tmp;//rotate row 3$tmp = $Sbox [$_statebyte[15]]; $_STATEBYTE[15] = $Sbox [$_statebyte[11]];$_statebyte[11] = $Sbox [$_statebyte[7]]; $_STATEBYTE[7] = $Sbox [$_statebyte[3]]; $_STATEBYTE[3] = $tmp;} Restores columns in eachof 4 rows//row0-unchanged, row1-shifted right 1,//row2-shifted right 2 and row3-shifted right 3protected functio n Invshiftrows () {$tmp = 0;$_statebyte = $this->_statebyte; $InvSbox = aeskeyinclude::$_invsbox;//Restore Row 0$_state BYTE[0] = $InvSbox [$_statebyte[0]]; $_STATEBYTE[4] = $InvSbox [$_statebyte[4]];$_statebyte[8] = $InvSbox [$_statebyte[8]]; $_STATEBYTE[12] = $InvSbox [$_statebyte[12]];//restore row 1$tmp = $InvSbox [$_statebyte[13]]; $_STATEBYTE[13] = $InvSbox [$_statebyte[9]];$_statebyte[9] = $InvSbox [$_statebyte[5]]; $_STATEBYTE[5] = $InvSbox [$_statebyte[1]]; $_STATEBYTE[1] = $tmp;//restore row 2$tmp = $InvSbox [$_statebyte[2]]; $_STATEBYTE[2] = $InvSbox [$_statebyte[10]]; $_STATEBYTE[10] = $tmp; $tmp = $InvSbox [$_statebyte[6]]; $_STATEBYTE[6] = $InvSbox [$_statebyte[14]]; $_STATEBYTE[14] = $tmp;//restore row 3$tmp = $InvSbox [$_statebyte[3]]; $_STATEBYTE[3] = $InvSbox [$_statebyte[7]];$_statebyte[7] = $InvSbox [$_statebyte[11]]; $_STATEBYTE[11] = $InvSbox [$_statebyte[15]]; $_STATEBYTE[15] = $tmp; $this->_statebyte = $_statebyte;} Recombine and mix each row in a columnprotected function Mixsubcolumns () {$_tempbyte = array ();//.length=0; $Xtime 2Sbox = Aeskeyinclude::$_xtime2sbox; $Xtime 3Sbox = Aeskeyinclude::$_xtime3sbox;$_statebyte = & $this->_statebyte;$ Sbox = aeskeyinclude::$_sbox;//mixing column 0$_tempbyte[0] = $Xtime 2sbox[$_statebyte[0]] ^ $Xtime 3sbox[$_statebyte[5] ^ $Sbox [$_statebyte[10]] ^ $Sbox [$_statebyte[15]];$_tempbyte[1] = $Sbox [$_statebyte[0]] ^ $Xtime 2sbox[$_statebyte[5] ] ^ $Xtime 3sbox[$_statebyte[10] ^ $Sbox [$_statebyte[15]];$_tempbyte[2] = $Sbox [$_statebyte[0]] ^ $Sbox [$_statebyte[5] ] ^ $Xtime 2sbox[$_statebyte[10] ^ $Xtime 3sbox[$_statebyte[15]];$_tempbyte[3] = $Xtime 3sbox[$_statebyte[0] ^ $Sbox [$_ STATEBYTE[5] ^ $Sbox [$_statebyte[10]] ^ $Xtime 2sbox[$_statebyte[15]];//mixing column 1$_tempbyte[4] = $Xtime 2sbox[$_ STATEBYTE[4] ^ $Xtime 3sbox[$_statebyte[9]] ^ $Sbox [$_statebyte[14]] ^ $Sbox [$_statebyte[3]];$_tempbyte[5] = $SBOX[$_STATEBYTE[4] ^ $Xtime 2sbox[$_statebyte[9]] ^ $Xtime 3sbox[$_statebyte[14] ^ $Sbox [$_statebyte[3]];$_tempbyte [6] = $Sbox [$_statebyte[4]] ^ $Sbox [$_statebyte[9]] ^ $Xtime 2sbox[$_statebyte[14]] ^ $Xtime 3sbox[$_statebyte[3]];$_ TEMPBYTE[7] = $Xtime 3sbox[$_statebyte[4] ^ $Sbox [$_statebyte[9]] ^ $Sbox [$_statebyte[14]] ^ $Xtime 2sbox[$_statebyte[3 ]];//mixing column 2$_tempbyte[8] = $Xtime 2sbox[$_statebyte[8]] ^ $Xtime 3sbox[$_statebyte[13]] ^ $Sbox [$_statebyte[2]] ^ $Sbox [$_statebyte[7]];$_tempbyte[9] = $Sbox [$_statebyte[8]] ^ $Xtime 2sbox[$_statebyte[13]] ^ $Xtime 3sbox[$_ STATEBYTE[2] ^ $Sbox [$_statebyte[7]];$_tempbyte[10] = $Sbox [$_statebyte[8]] ^ $Sbox [$_statebyte[13]] ^ $Xtime 2sbox[$_ STATEBYTE[2] ^ $Xtime 3sbox[$_statebyte[7]];$_tempbyte[11] = $Xtime 3sbox[$_statebyte[8] ^ $Sbox [$_statebyte[13]] ^ $ SBOX[$_STATEBYTE[2] ^ $Xtime 2sbox[$_statebyte[7]];//mixing column 3$_tempbyte[12] = $Xtime 2sbox[$_statebyte[12]] ^ $ XTIME3SBOX[$_STATEBYTE[1] ^ $Sbox [$_statebyte[6]] ^ $Sbox [$_statebYTE[11]];$_TEMPBYTE[13] = $Sbox [$_statebyte[12]] ^ $Xtime 2sbox[$_statebyte[1]] ^ $Xtime 3sbox[$_statebyte[6]] ^ $Sbox [$ _STATEBYTE[11]];$_TEMPBYTE[14] = $Sbox [$_statebyte[12]] ^ $Sbox [$_statebyte[1]] ^ $Xtime 2sbox[$_statebyte[6]] ^ $ XTIME3SBOX[$_STATEBYTE[11]];$_TEMPBYTE[15] = $Xtime 3sbox[$_statebyte[12] ^ $Sbox [$_statebyte[1]] ^ $Sbox [$_ STATEBYTE[6] ^ $Xtime 2sbox[$_statebyte[11]];/*_statebyte.position=0;_statebyte.writebytes (_tempByte, 0, Nb*4); */$ _statebyte = $_tempbyte; $this->_tempbyte = $_tempbyte;} Restore and Un-mix each row in a columnprotected function Invmixsubcolumns () {$_tempbyte = array ();//.length=0;$_stateb Yte = & $this->_statebyte; $XtimeE = Aeskeyinclude::$_xtimee; $XtimeB = aeskeyinclude::$_xtimeb; $Xtime 9 = aeskeyinclude::$_xtime9; $XtimeD = aeskeyinclude::$_xtimed; $InvSbox = aeskeyinclude::$_invsbox; $Nb = aeskeyinclude::$ nb;//Restore column 0$_tempbyte[0] = $XtimeE [$_statebyte[0]] ^ $XtimeB [$_statebyte[1]] ^ $XtimeD [$_statebyte[2]] ^ $Xtim e9[$_statebyte[3]];$_TEMPBYTE[5] = $Xtime 9[$_statebyte[0] ^ $XtimeE [$_statebyte[1]] ^ $XtimeB [$_statebyte[2]] ^ $XtimeD [$_statebyte[3]];$ _TEMPBYTE[10] = $XtimeD [$_statebyte[0]] ^ $Xtime 9[$_statebyte[1]] ^ $XtimeE [$_statebyte[2]] ^ $XtimeB [$_statebyte[3]]; $_TEMPBYTE[15] = $XtimeB [$_statebyte[0]] ^ $XtimeD [$_statebyte[1]] ^ $Xtime 9[$_statebyte[2] ^ $XtimeE [$_statebyte[3]] ;//Restore column 1$_tempbyte[4] = $XtimeE [$_statebyte[4]] ^ $XtimeB [$_statebyte[5]] ^ $XtimeD [$_statebyte[6]] ^ $Xtime 9 [$_statebyte[7]];$_tempbyte[9] = $Xtime 9[$_statebyte[4]] ^ $XtimeE [$_statebyte[5]] ^ $XtimeB [$_statebyte[6]] ^ $XtimeD [$_STATEBYTE[7]];$_TEMPBYTE[14] = $XtimeD [$_statebyte[4]] ^ $Xtime 9[$_statebyte[5]] ^ $XtimeE [$_statebyte[6]] ^ $ XTIMEB[$_STATEBYTE[7]];$_TEMPBYTE[3] = $XtimeB [$_statebyte[4]] ^ $XtimeD [$_statebyte[5]] ^ $Xtime 9[$_statebyte[6]] ^ $ xtimee[$_statebyte[7]];//Restore column 2$_tempbyte[8] = $XtimeE [$_statebyte[8]] ^ $XtimeB [$_statebyte[9]] ^ $XtimeD [$ _STATEBYTE[10]] ^ $Xtime 9[$_STATEBYTE[11]];$_TEMPBYTE[13] = $Xtime 9[$_statebyte[8] ^ $XtimeE [$_statebyte[9]] ^ $XtimeB [$_statebyte[10]] ^ $XtimeD [$_statebyte[11]];$_tempbyte[ 2] = $XtimeD [$_statebyte[8]] ^ $Xtime 9[$_statebyte[9]] ^ $XtimeE [$_statebyte[10]] ^ $XtimeB [$_statebyte[11]];$_ TEMPBYTE[7] = $XtimeB [$_statebyte[8]] ^ $XtimeD [$_statebyte[9]] ^ $Xtime 9[$_statebyte[10] ^ $XtimeE [$_statebyte[11]] ;//Restore column 3$_tempbyte[12] = $XtimeE [$_statebyte[12]] ^ $XtimeB [$_statebyte[13]] ^ $XtimeD [$_statebyte[14]] ^ $Xt IME9[$_STATEBYTE[15]];$_TEMPBYTE[1] = $Xtime 9[$_statebyte[12] ^ $XtimeE [$_statebyte[13]] ^ $XtimeB [$_statebyte[14]] ^ $XtimeD [$_statebyte[15]];$_tempbyte[6] = $XtimeD [$_statebyte[12]] ^ $Xtime 9[$_statebyte[13]] ^ $XtimeE [$_statebyte[ ] ^ $XtimeB [$_statebyte[15]];$_tempbyte[11] = $XtimeB [$_statebyte[12]] ^ $XtimeD [$_statebyte[13]] ^ $Xtime 9[$_ STATEBYTE[14] ^ $XtimeE [$_statebyte[15]];for ($i =0; $i < 4 * $NB; $i + +) {$_statebyte[$i] = $InvSbox [$_tempbyte[$i]];} }//encrypt/decrypt columns of the keyprotected function AddrouNdkey ($key, $offset) {$_statebyte = & $this->_statebyte;for ($idx = 0; $idx < + + + $idx) {$_statebyte[$idx] ^= $ key[$idx + $offset];}} Public Function toString () {return ' AES '. ( 8* $this->_keylength);}}? >


 _key = $key, if ($padding = = NULL) {$padding = new PKCS5 ($key->getblocksize ()),} else {$padding->setblocksize ($key-& Gt;getblocksize ());} $this->_padding = $padding;} Public Function Getblocksize () {return $this->_key->getblocksize ();} Public function Encrypt ($SRC) {$src = $this->_padding->pad ($src); $blockSize = $this->_key->getblocksize () ; $dst = Array (), $len = count ($SRC); for ($i =0; $i < $len; $i + = $blockSize) {//copy data $tmp = Array_slice ($src, $i, $blockSize); $ret = $this->_key->encrypt ($tmp); $dst = Array_merge ($DST, $ret);} return $DST;} Public function Decrypt ($src) {$blockSize = $this->_key->getblocksize (); $len = count ($SRC);//Sanity Check.if ($ Len% $blockSize!=0) {throw new Exception ("ECB mode cipher length must be a multiple of blockSize". $blockSize);} $tmp = Array (), $DST = Array (), for ($i =0; $i < $len; $i + = $blockSize) {$tmp = Array_slice ($src, $i, $blockSize); $ret = $this- >_key->decrypt ($tmp); $dst = Array_merge ($DST, $ret);} $DST = $this->_padding->unpad ($DST); return $DST;} Public function Dispose () {}public function toString () {return $this->_key->tostring (). " -ecb ";}}? >



 
  



 
  



 
  



 
  


 
  
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.