Example of AES encryption and decryption in php _ php instance

Source: Internet
Author: User
Put the code first. There are two files: AES. php (aes algorithm file) and aesDemo. php (application instance file) aesDemo. php:

Example,

The Code is as follows:


Require_once ('./AES. php ');
// $ Aes = new AES ();
$ Aes = new AES (true); // stores encrypted strings in hexadecimal format.
// $ Aes = new AES (true, true); // The encrypted string is stored in hexadecimal format with debugging information
$ Key = "this is a 32 byte key"; // key
$ Keys = $ aes-> makeKey ($ key );
$ Encode = "123456"; // encrypted string
$ Ct = $ aes-> encryptString ($ encode, $ keys );
Echo "encode =". $ ct ."
";
$ Cpt = $ aes-> decryptString ($ ct, $ keys );
Echo "decode =". $ cpt;
?>

Example, AES Encryption Class

The Code is 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 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 ($ data ),"..");
Return $ data;
}

}
// Usage
$ Aes = new AESMcrypt ($ bit = 128, $ key = 'abcdef1234567890', $ iv = '0987654321cba CBA ', $ mode = 'cbc ');
$ C = $ aes-> encrypt ('haowei. me ');
Var_dump ($ aes-> decrypt ($ c ));

Example: an encryption and decryption class

The Code is as follows:


/**
* AES encryption and decryption
* @ Author hushangming
*
* Usage:
*


* // Instantiate the class
* // Parameter $ _ bit: format, which can be 256, 192, or 128 bytes. The default value is 128 bytes.
* // Parameter $ _ type: encryption/Decryption mode. The options include cfb, cbc, nofb, ofb, stream, and ecb. The default value is ecb.
* // Parameter $ _ key: key. The default value is abcdefghijuklmno.
* $ Tcaes = new TCAES ();
* $ String = 'laohu ';
* // Encryption
* $ EncodeString = $ tcaes-> encode ($ string );
* // Decrypt
* $ DecodeString = $ tcaes-> decode ($ encodeString );
*

*/
Class TCAES {
Private $ _ bit = MCRYPT_RIJNDAEL_256;
Private $ _ type = MCRYPT_MODE_CBC;
// Private $ _ key = 'abcdefghijuklmno0123456789012345 ';
Private $ _ key = 'abcdefghijuklmno'; // key
Private $ _ use_base64 = true;
Private $ _ iv_size = null;
Private $ _ iv = null;

/**
* @ Param string $ _ key
* @ Param int $ _ bit 128 bytes by default
* @ Param string $ _ type encryption and decryption method
* @ Param boolean $ _ use_base64 uses base64 secondary encryption by default
*/
Public function _ construct ($ _ key = '', $ _ bit = 128, $ _ type = 'ecb ', $ _ use_base64 = true ){
// Encrypted byte
If (192 ==== _ 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;
}
// 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 the 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 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 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 the hexadecimal stream $ string to a 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;
}
}

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.