Standard PHP AES encryption algorithm class _php technique

Source: Internet
Author: User
Tags learn php

Share a standard PHP AES encryption algorithm class, where Mcrypt_get_block_size (' rijndael-128 ', ' ECB '), if it is easier to get it wrong without understanding the principle, you can pass Mcrypt_list_ The algorithms function looks at the encryption algorithm ID you need.

Copy Code code as follows:

<?php
/**
* AES128 encryption and decryption class
* @author dy
*
*/
Defined (' Inejbuy ') or exit (' Access invalid! ');
Class aes{
Secret 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 encrypted data
$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 encrypted data
$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;
}
/**
* Fill algorithm
* @param string $source
* @return String
*/
function addpkcs7padding ($source) {
$source = Trim ($source);
$block = mcrypt_get_block_size (' rijndael-128 ', ' ECB ');
$pad = $block-(strlen ($source)% $block);
if ($pad <= $block) {
$char = Chr ($pad);
$source. = Str_repeat ($char, $pad);
}
return $source;
}
/**
* Remove Fill 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 the entire content described in this article, I hope that you learn PHP AES encryption algorithm classes help.

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.