Example of AES encryption and decryption in PHP _php example

Source: Internet
Author: User
Tags base64 decrypt rtrim

aesdemo.php:

Example

Copy Code code as follows:

<?php
Require_once ('./aes.php ');
$aes = new Aes ();
$aes = new Aes (TRUE);//To store the encrypted string in 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";//Encrypted string
$CT = $aes->encryptstring ($encode, $keys);
echo "encode =". $ct. " <br> ";
$CPT = $aes->decryptstring ($ct, $keys);
echo "decode =". $cpt;
?>

Example, AES encryption class

Copy Code code as follows:

<?php
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: $this->cipher = mcrypt_rijndael_192; Break
Case 256: $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 = 128, $key = ' abcdef1234567890 ', $iv = ' 0987654321FEDCBA ', $mode = ' CBC ');
$c = $aes->encrypt (' haowei.me ');
Var_dump ($aes->decrypt ($c));

example, with an encrypted and decrypted class

Copy Code code as follows:

? Php
/**
* AES encryption, decryption class
* @author hushangming
*
Usage
* <pre>
*//Instantiate class
*//Parameter $_bit: format, support 256, 192, 128, default to 128 bytes of
*//Parameter $_type: Encryption/decryption mode, support for CFB, CBC, NOFB, OFB, stream, ECB, default to ECB
*//Parameter $_key: Key, default to Abcdefghijuklmno
* $tcaes = new Tcaes ();
* $string = ' Laohu ';
*//encryption
* $encodeString = $tcaes->encode ($string);
*//decryption
* $decodeString = $tcaes->decode ($encodeString);
* </pre>
*/
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 uses 128 bytes by default
* @param string $_type encryption and decryption method
* @param boolean $_use_base64 default to use Base64 two times encryption
*/
Public function __construct ($_key = ', $_bit = 128, $_type = ' ECB ', $_use_base64 = True) {
Encrypted bytes
if (= = = = $_bit) {
$this->_bit = mcrypt_rijndael_192;
}elseif (128 = = = $_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 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 decrypt strings
* @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 hexadecimal
* @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;
}
}

Related Article

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.