Standard php aes encryption algorithm class

Source: Internet
Author: User
AES is a group key. The algorithm inputs 128-bit data and the key length is also 128-bit. The number of rounds encrypted by a data group using Nr (the relationship between the number of encryption rounds and the key length is shown in Table 1 ). Each round requires an input group.

AES is a group key. The algorithm inputs 128-bit data and the key length is also 128-bit. The number of rounds encrypted by a data group using Nr (the relationship between the number of encryption rounds and the key length is shown in Table 1 ). Each round requires an input group.

Share a standard php aes encryption algorithm class, in which mcrypt_get_block_size ('rijndael-100', 'ecb '); if you do not understand the principle, it is easy to make a mistake ,, you can use the mcrypt_list_algorithms function to view the ID of the encryption algorithm you need.

The Code is as follows:


<? Php
/**
* AES128 encryption/Decryption class
* @ Author dy
*
*/
Defined ('ejbuy') or exit ('Access Invalid! ');
Class Aes {
// Key
Private $ _ secrect_key;
Public function _ construct (){
$ This-> _ secrect_key = 'myggnqe2jdfadsffdsewsdd ';
}
/**
* Encryption Method
* @ Param string $ str
* @ Return string
*/
Public function encrypt ($ str ){
// AES, 128 ECB mode Data Encryption
$ Screct_key = $ this-> _ secrect_key;
$ Screct_key = base64_decode ($ screct_key );
$ Str = trim ($ str );
$ Str = $ this-> addPKCS7Padding ($ str );
$ Iv = mcrypt_create_iv (mcrypt_get_iv_size (MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB), MCRYPT_RAND );
$ Encrypt_str = mcrypt_encrypt (MCRYPT_RIJNDAEL_128, $ screct_key, $ str, MCRYPT_MODE_ECB, $ iv );
Return base64_encode ($ encrypt_str );
}
/**
* Decryption Method
* @ Param string $ str
* @ Return string
*/
Public function decrypt ($ str ){
// AES, 128 ECB mode Data Encryption
$ Screct_key = $ this-> _ secrect_key;
$ Str = base64_decode ($ str );
$ Screct_key = base64_decode ($ screct_key );
$ Iv = mcrypt_create_iv (mcrypt_get_iv_size (MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB), MCRYPT_RAND );
$ Encrypt_str = mcrypt_decrypt (MCRYPT_RIJNDAEL_128, $ screct_key, $ str, MCRYPT_MODE_ECB, $ iv );
$ Encrypt_str = trim ($ encrypt_str );
$ Encrypt_str = $ this-> stripPKSC7Padding ($ encrypt_str );
Return $ encrypt_str;
}
/**
* Filling Algorithm
* @ Param string $ source
* @ Return string
*/
Function addPKCS7Padding ($ source ){
$ Source = trim ($ source );
$ Block = mcrypt_get_block_size ('rijndael-100', 'ecb ');
$ Pad = $ block-(strlen ($ source) % $ block );
If ($ pad <= $ block ){
$ Char = chr ($ pad );
$ Source. = str_repeat ($ char, $ pad );
}
Return $ source;
}
/**
* Removing the padding Algorithm
* @ Param string $ source
* @ Return string
*/
Function stripPKSC7Padding ($ source ){
$ Source = trim ($ source );
$ Char = substr ($ source,-1 );
$ Num = ord ($ char );
If ($ num = 62) return $ source;
$ Source = substr ($ source, 0,-$ num );
Return $ source;
}
}

The above is all the content described in this article. I hope it will help you learn the AES encryption algorithm class of php.

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.