欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php写的AES加密解密类分享

程序员文章站 2023-01-08 21:51:49
今天写了一个php的aes加密类。适用于yii的扩展。 如果不用在yii框架中,把代码中yii::app()->params['encryptkey'] 换成你对应...

今天写了一个php的aes加密类。适用于yii的扩展。
如果不用在yii框架中,把代码中yii::app()->params['encryptkey'] 换成你对应的默认key就可以了。
类代码:

<?php
/**
 * php aes加解密类
 * 如果要与java共用,则密钥长度应该为16位长度
 * 因为java只支持128位加密,所以php也用128位加密,可以与java互转。
 * 同时aes的标准也是128位。只是rijndael算法可以支持128,192和256位加密。
 * java 要使用aes/cbc/nopadding标准来加解密
 * 
 * @author terry
 *
 */
class phpaes
{
	/**
	 * this was aes-128 / cbc / nopadding encrypted.
	 * return base64_encode string
	 * @author terry
	 * @param string $plaintext
	 * @param string $key
	 */
	public static function aesencrypt($plaintext,$key = null)
	{
		$plaintext = trim($plaintext);
		if ($plaintext == '') return '';
		if(!extension_loaded('mcrypt'))
			throw new cexception(yii::t('yii','aesencrypt requires php mcrypt extension to be loaded in order to use data encryption feature.'));
		$size = mcrypt_get_block_size(mcrypt_rijndael_128, mcrypt_mode_cbc);
		$module = mcrypt_module_open(mcrypt_rijndael_128, '', mcrypt_mode_cbc, '');
		$key=self::substr($key===null ? yii::app()->params['encryptkey'] : $key, 0, mcrypt_enc_get_key_size($module));
		/* create the iv and determine the keysize length, use mcrypt_rand
		 * on windows instead */
		$iv = substr(md5($key),0,mcrypt_enc_get_iv_size($module));
		/* intialize encryption */
		mcrypt_generic_init($module, $key, $iv);

		/* encrypt data */
		$encrypted = mcrypt_generic($module, $plaintext);

		/* terminate encryption handler */
		mcrypt_generic_deinit($module);
		mcrypt_module_close($module);
		return base64_encode($encrypted);
	}

	/**
	 * this was aes-128 / cbc / nopadding decrypted.
	 * @author terry
	 * @param string $encrypted		base64_encode encrypted string
	 * @param string $key
	 * @throws cexception
	 * @return string
	 */
	public static function aesdecrypt($encrypted, $key = null)
	{
		if ($encrypted == '') return '';
		if(!extension_loaded('mcrypt'))
			throw new cexception(yii::t('yii','aesdecrypt requires php mcrypt extension to be loaded in order to use data encryption feature.'));

		$ciphertext_dec = base64_decode($encrypted);
		$module = mcrypt_module_open(mcrypt_rijndael_128, '', mcrypt_mode_cbc, '');
		$key=self::substr($key===null ? yii::app()->params['encryptkey'] : $key, 0, mcrypt_enc_get_key_size($module));

		$iv = substr(md5($key),0,mcrypt_enc_get_iv_size($module));

		/* initialize encryption module for decryption */
		mcrypt_generic_init($module, $key, $iv);

		/* decrypt encrypted string */
		$decrypted = mdecrypt_generic($module, $ciphertext_dec);

		/* terminate decryption handle and close module */
		mcrypt_generic_deinit($module);
		mcrypt_module_close($module);
		return rtrim($decrypted,"\0");
	}

	/**
	 * returns the length of the given string.
	 * if available uses the multibyte string function mb_strlen.
	 * @param string $string the string being measured for length
	 * @return integer the length of the string
	 */
	private static function strlen($string)
	{
		return extension_loaded('mbstring') ? mb_strlen($string,'8bit') : strlen($string);
	}

	/**
	 * returns the portion of string specified by the start and length parameters.
	 * if available uses the multibyte string function mb_substr
	 * @param string $string the input string. must be one character or longer.
	 * @param integer $start the starting position
	 * @param integer $length the desired portion length
	 * @return string the extracted part of string, or false on failure or an empty string.
	 */
	private static function substr($string,$start,$length)
	{
		return extension_loaded('mbstring') ? mb_substr($string,$start,$length,'8bit') : substr($string,$start,$length);
	}
}