aesdemo.php:
Example
Copy the Code code as follows:
Require_once ('./aes.php ');
$aes = new Aes ();
$aes = new Aes (TRUE);//Store the encrypted string as hexadecimal
$aes = new Aes (true,true);//With debug information and encrypted string stored in hexadecimal
$key = "This is a + byte key";//Key
$keys = $aes->makekey ($key);
$encode = "123456";//The string that is encrypted
$CT = $aes->encryptstring ($encode, $keys);
echo "encode =". $ct. "
";
$CPT = $aes->decryptstring ($ct, $keys);
echo "decode =". $cpt;
?>
Example, AES encryption class
Copy the Code code as follows:
PHP AES Encryption Class
Class Aesmcrypt {
public $iv = null;
public $key = null;
Public $bit = 128;
Private $cipher;
Public function __construct ($bit, $key, $iv, $mode) {
if (Empty ($bit) | | empty ($key) | | empty ($IV) | | empty ($mode))
return NULL;
$this->bit = $bit;
$this->key = $key;
$this->iv = $iv;
$this->mode = $mode;
Switch ($this->bit) {
Case 192: $this->cipher = mcrypt_rijndael_192; Break
Case: $this->cipher = mcrypt_rijndael_256; Break
Default: $this->cipher = mcrypt_rijndael_128;
}
Switch ($this->mode) {
Case ' ECB ': $this->mode = MCRYPT_MODE_ECB; Break
Case ' CFB ': $this->mode = MCRYPT_MODE_CFB; Break
Case ' OFB ': $this->mode = MCRYPT_MODE_OFB; Break
Case ' NOFB ': $this->mode = MCRYPT_MODE_NOFB; Break
Default: $this->mode = MCRYPT_MODE_CBC;
}
}
Public function Encrypt ($data) {
$data = Base64_encode (Mcrypt_encrypt ($this->cipher, $this->key, $data, $this->mode, $this->iv));
return $data;
}
Public function Decrypt ($data) {
$data = Mcrypt_decrypt ($this->cipher, $this->key, Base64_decode ($data), $this->mode, $this->iv);
$data = RTrim (RTrim ($data), "..");
return $data;
}
}
How to use
$aes = new Aesmcrypt ($bit = n, $key = ' abcdef1234567890 ', $iv = ' 0987654321FEDCBA ', $mode = ' CBC ');
$c = $aes->encrypt (' haowei.me ');
Var_dump ($aes->decrypt ($c));
Example, with an encryption-able decryption class
Copy the Code code as follows:
/**
* AES encryption, decryption class
* @author hushangming
*
Usage
*
*//Instantiation Class
*//Parameter $_bit: format, support 256, 192, 128, default is 128 bytes
*//Parameter $_type: Encryption/decryption mode, support for CFB, CBC, NOFB, OFB, stream, ECB, default ECB
*//Parameter $_key: Key, default is Abcdefghijuklmno
* $string = ' Laohu ';
*//encryption
* $encodeString = $tcaes->encode ($string);
*//decryption
* $decodeString = $tcaes->decode ($encodeString);
*/
Class tcaes{
Private $_bit = mcrypt_rijndael_256;
Private $_type = MCRYPT_MODE_CBC;
Private $_key = ' abcdefghijuklmno0123456789012345 ';
Private $_key = ' Abcdefghijuklmno '; Secret key
Private $_use_base64 = true;
Private $_iv_size = null;
Private $_iv = null;
/**
* @param string $_key key
* @param int $_bit default using 128 bytes
* @param string $_type encryption and decryption method
* @param boolean $_USE_BASE64 uses base64 two encryption by default
*/
Public function __construct ($_key = ", $_bit = $, $_type = ' ECB ', $_use_base64 = True) {
Encrypted bytes
if (192 = = = $_bit) {
$this->_bit = mcrypt_rijndael_192;
}elseif (+ = = $_bit) {
$this->_bit = mcrypt_rijndael_128;
}else{
$this->_bit = mcrypt_rijndael_256;
}
Encryption method
if (' cfb ' = = = = $_type) {
$this->_type = MCRYPT_MODE_CFB;
}elseif (' cbc ' = = = = $_type) {
$this->_type = MCRYPT_MODE_CBC;
}elseif (' nofb ' = = = = $_type) {
$this->_type = MCRYPT_MODE_NOFB;
}elseif (' ofb ' = = = = $_type) {
$this->_type = MCRYPT_MODE_OFB;
}elseif (' stream ' = = = = $_type) {
$this->_type = Mcrypt_mode_stream;
}else{
$this->_type = MCRYPT_MODE_ECB;
}
Secret key
if (!empty ($_key)) {
$this->_key = $_key;
}
Whether to use Base64
$this->_use_base64 = $_use_base64;
$this->_iv_size = mcrypt_get_iv_size ($this->_bit, $this->_type);
$this->_iv = Mcrypt_create_iv ($this->_iv_size, Mcrypt_rand);
}
/**
* Encryption
* @param string $string strings to be encrypted
* @return String
*/
Public function encode ($string) {
if (MCRYPT_MODE_ECB = = = $this->_type) {
$encodeString = Mcrypt_encrypt ($this->_bit, $this->_key, $string, $this->_type);
}else{
$encodeString = Mcrypt_encrypt ($this->_bit, $this->_key, $string, $this->_type, $this->_iv);
}
if ($this->_use_base64)
$encodeString = Base64_encode ($encodeString);
return $encodeString;
}
/**
* Decryption
* @param string $string to be decrypted
* @return String
*/
Public function decode ($string) {
if ($this->_use_base64)
$string = Base64_decode ($string);
$string = $this->tohexstring ($string);
if (MCRYPT_MODE_ECB = = = $this->_type) {
$decodeString = Mcrypt_decrypt ($this->_bit, $this->_key, $string, $this->_type);
}else{
$decodeString = Mcrypt_decrypt ($this->_bit, $this->_key, $string, $this->_type, $this->_iv);
}
return $decodeString;
}
/**
* Convert $string to Hex
* @param string $string
* @return Stream
*/
Private Function tohexstring ($string) {
$buf = "";
for ($i = 0; $i < strlen ($string); $i + +) {
$val = Dechex (Ord ($string {$i}));
if (strlen ($val) < 2)
$val = "0". $val;
$buf. = $val;
}
return $buf;
}
/**
* Convert hexadecimal stream $string to string
* @param stream $string
* @return String
*/
Private Function fromhexstring ($string) {
$buf = "";
for ($i = 0; $i < strlen ($string); $i + = 2) {
$val = Chr (Hexdec (substr ($string, $i, 2)));
$buf. = $val;
}
return $buf;
}
}