日期:2014-05-17  浏览次数:20485 次

php代码实现aes加密
发现php4的加密模块在php5的不被支持了,硬要加上这个模块会导致启动时候出现警告对话框。
于是花了一天时间将as3的加密类翻译成了php的,该加密类是google的开源项目
http://code.google.com/p/as3crypto/


<?php
require_once("PKCS5.php");
require_once("AESKey.php");
require_once("ECBMode.php");
require_once("Hex.php");

class Aes
{
	private $_pad;//填充方式
	private $_mode;//加密类
	/**
	 * 构造函数
	 * @param	base64keyString   密钥base64编码字符串
	 */
	public function Aes($base64keyString) 
	{
		$this->_pad = new PKCS5(); //为了与java保持一致,所以采用PKCS5填充
		$key = Hex::string2ByteArray(base64_decode($base64keyString));
		$this->_mode = new ECBMode(new AESKey($key), $this->_pad);
		$this->_pad->setBlockSize($this->_mode->getBlockSize());
	}
	
	
	/**
	 * 将明文加密为密文base64编码字符串
	 * @param	plainSrc		 明文
	 * @return	密文base64编码
	 */
	public function encrypt($plainSrc)
	{
		$src = Hex::string2ByteArray($plainSrc);
		$src = $this->_mode->encrypt($src);
		
		return base64_encode(Hex::ByteArray2String($src));
	}

	
	/**
	 * 将base64编码字符串(密文)解密成 明文
	 * @param	base64Src  密文base64编码字符串
	 * @return	明文
	 */
	public function decrypt($base64Src)
	{
		$src = base64_decode($base64Src);
		$src = $this->_mode->decrypt(Hex::string2ByteArray($src));
		return Hex::byteArray2String($src);
	}
	
	/**
	 * 释放内存
	 */
	public function dispose()
	{
		$this->_mode->dispose();
	}
}
//var_dump(Hex::string2ByteArray(base64_decode("MK2X82eL6jkKbzvlJU1ZMR6rcKO+SBhmbPOmFD/2Mxw=")));

$_aes = new Aes("MK2X82eL6jkKbzvlJU1ZMR6rcKO+SBhmbPOmFD/2Mxw=");
//echo "=================<br>";
$ret = $_aes->encrypt("1234567890abcdef1234567890abcdefaaafdsfsdffasfasfasfasdf");
echo $ret;
var_dump($_aes->decrypt($ret));

?>


<?php
require_once("IPad.php");

class PKCS5 implements IPad
{
	private $blockSize = 0;
	
	public function PKCS5($blockSize=0) {
		$this->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 isn'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;
	}
}
?>




<?php
require_once("ISymmetricKey.php");
require_once("AESKeyInclude.php");
require_once("Hex.php");



class AESKey implements ISymmetricKey
{
	
	private $_keyByte;
	private $_keyLength;
	private $_nNr;
	private $_stateByte;
	private $_tempByte;

	public function AESKey($key/*:ByteArray*/) {
		$this->_tempByte = array();
		$this->_stateByte = array();
		$this->_keyLength = count($key);
		$this->_keyByte = $key;
		$this->expandKey();
	}
	
	// produce Nb bytes for each round
	private function expandKey() 
	{
		$tmp0=0;
		$tmp1=0;
		$tmp2=0;
		$tmp3=0;
		$tmp4=0;
		$idx=0;
		$Nk = $this->_keyLength/4;
		//echo("count:".$Nk."<br>".count($this->_keyByte)."<br>");
		$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++ ) {
			$tmp0 = $_keyByte[4*$idx - 4];
			$tmp1 = $_keyByte[4*$idx - 3];
			$tmp2 = $_keyByte[4*$idx - 2];
			$tmp3 = $_keyByte[4*$idx - 1];
			if( !($idx % $Nk) ) {
				$tmp4 = $tmp3;
				$tmp3 = $Sbox[$tmp0];
				$tmp0 = $Sbox[$tmp1] ^ $Rcon[$idx/$Nk];
				$tmp1 = $Sbox[$tmp2];
				$tmp2 = $Sbox[$tmp4];
			} else if( $Nk > 6 && $idx % $Nk == 4 ) {
				$tmp0 = $Sbox[$tmp0];
				$tmp1 = $Sbox[$tmp1];
				$tmp2 = $Sbox[$tmp2];
				$tmp3 = $Sbox[$tmp3];
			}
	
			$_keyByte[4*$idx+0] = $_keyByte[4*$idx - 4*$Nk + 0] ^ $tmp0;
			$_keyByte[4*$idx+1] = $_keyByte