预览加载中,请您耐心等待几秒...
1/10
2/10
3/10
4/10
5/10
6/10
7/10
8/10
9/10
10/10

亲,该文档总共11页,到这已经超出免费预览范围,如果喜欢就直接下载吧~

如果您无法下载资料,请参考说明:

1、部分资料下载需要金币,请确保您的账户上有足够的金币

2、已购买过的文档,再次下载不重复扣费

3、资料包下载后请先用软件解压,在使用对应软件打开

PHP实现AES加密 1、Aas类申明 <?php/*-----------------------------------------------*//*AESimplementationinPHP(c)ChrisVeness2005-2011.Rightoffreeuseisgrantedforall*//*commercialornon-commercialuseunderCC-BYlicence.Nowarrantyofanyformisoffered.*//*-----------------------------------------------*/classAes{/***AESCipherfunction:encrypt'input'withRijndaelalgorithm**@paraminputmessageasbyte-array(16bytes)*@paramwkeyscheduleas2Dbyte-array(Nr+1xNbbytes)-*generatedfromthecipherkeybykeyExpansion()*@returnciphertextasbyte-array(16bytes)*/publicstaticfunctioncipher($input,$w){ //maincipherfunction[§5.1]$Nb=4;//blocksize(inwords):noofcolumnsinstate(fixedat4forAES)$Nr=count($w)/$Nb-1;//noofrounds:10/12/14for128/192/256-bitkeys$state=array();//initialise4xNbbyte-array'state'withinput[§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++){//applyNrrounds$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);//convertstateto1-darraybeforereturning[§3.4]for($i=0;$i<4*$Nb;$i++)$output[$i]=$state[$i%4][floor($i/4)];return$output;}privatestaticfunctionaddRoundKey($state,$w,$rnd,$Nb){//xorRoundKeyintostateS[§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;}privatestaticfunctionsubBytes($s,$Nb){//applySBoxtostateS[§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;}privatestaticfunctionshiftRows($s,$Nb){//shiftrowrofstateSleftbyrbytes[§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];//shiftintotempcopyfor($c=0;$c<4;$c++)$s[$r][$c]=$t[$c];//andcopyback}//notethatthiswillworkforNb=4,5,6,butnot7,8(always4forAES):return$s;//seefp.gladman.plus.com/cryptography_techno