PHP AES Encryption class program code sharing

Source: Internet
Author: User
Tags chr decrypt mcrypt md5 strlen yii

AES encryption algorithm-algorithm principle

The AES algorithm is based on permutation and permutation operations. The arrangement is to reschedule the data, and the permutation is to replace one data unit with another. AES uses several different methods to perform permutation and permutation operations.
AES is an iterative, symmetric key grouping password that uses 128, 192, and 256-bit keys and encrypts and decrypts data in 128-bit (16-byte) groupings. The symmetric key password uses the same key to encrypt and decrypt the data, unlike the public key password using the key pair. The number of bits of encrypted data returned by a block cipher is the same as the input data. Iterative cryptography uses a looping structure that repeats and replaces input data in the loop.

The code is as follows Copy Code

<?php
/**
* PHP AES Plus decryption class
* Because Java only supports 128-bit encryption, PHP also uses 128-bit encryption, you can go with java.
* AES is also the standard of 128-bit. Only the Rijndael algorithm can support 128,192 and 256-bit encryption.
*
* @author Terry
*
*/
Class Phpaes
{
/**
* This is aes-128/cbc/zerobytepadding encrypted.
* Return Base64_encode string
* @author Terry
* @param string $plaintext
* @param string $key
*/
public static function Aesencrypt ($plaintext, $key = null)
{
if ($plaintext = = ") return";
if (!extension_loaded (' MCrypt '))
throw new CException (Yii::t (' Yii ', ' aesencrypt requires PHP mcrypt extension to is loaded in order to use data encryption F Eature. '));
$size = Mcrypt_get_block_size (mcrypt_rijndael_128, MCRYPT_MODE_CBC);
$plaintext = self::P kcs5padding ($plaintext, $size);
$module = Mcrypt_module_open (mcrypt_rijndael_128, ', MCRYPT_MODE_CBC, ');
$key =self::substr ($key ===null? Yii::app ()->params[' Encryptkey ']: $key, 0, Mcrypt_enc_get_key_size ($module));
/* Create the IV and determine the keysize length, use Mcrypt_rand
* on Windows instead * *
$IV = substr (MD5 ($key), 0,mcrypt_enc_get_iv_size ($module));
/* intialize Encryption * *
Mcrypt_generic_init ($module, $key, $IV);

/* ENCRYPT Data * *
$encrypted = Mcrypt_generic ($module, $plaintext);

/* Terminate Encryption Handler * *
Mcrypt_generic_deinit ($module);
Mcrypt_module_close ($module);
Return Base64_encode (Trim ($encrypted));
}

/**
* This is aes-128/cbc/zerobytepadding decrypted.
* @author Terry
* @param string $encrypted base64_encode encrypted string
* @param string $key
* @throws CException
* @return String
*/
public static function Aesdecrypt ($encrypted, $key = null)
{
if ($encrypted = = ") return";
if (!extension_loaded (' MCrypt '))
throw new CException (Yii::t (' Yii ', ' aesdecrypt requires PHP mcrypt extension to is loaded in order to use data encryption F Eature. '));

$ciphertext _dec = Base64_decode ($encrypted);
$module = Mcrypt_module_open (mcrypt_rijndael_128, ', MCRYPT_MODE_CBC, ');
$key =self::substr ($key ===null? Yii::app ()->params[' Encryptkey ']: $key, 0, Mcrypt_enc_get_key_size ($module));

$IV = substr (MD5 ($key), 0,mcrypt_enc_get_iv_size ($module));

/* Initialize Encryption module for decryption * *
Mcrypt_generic_init ($module, $key, $IV);

/* Decrypt Encrypted String * *
$decrypted = Mdecrypt_generic ($module, $ciphertext _dec);

/* Terminate decryption handle and Close module * *
Mcrypt_generic_deinit ($module);
Mcrypt_module_close ($module);
Return self::unpkcs5padding ($decrypted);
}


private static function strlen ($string)
{
Return extension_loaded (' mbstring ')? Mb_strlen ($string, ' 8bit '): strlen ($string);
}

private static function substr ($string, $start, $length)
{
Return extension_loaded (' mbstring ')? Mb_substr ($string, $start, $length, ' 8bit '): substr ($string, $start, $length);
}

private static function pkcs5padding ($text, $blocksize) {
$pad = $blocksize-(Self::strlen ($text)% $blocksize);
Return $text. Str_repeat (Chr ($pad), $pad);
}

private static function unpkcs5padding ($text)
{
$pad = Ord ($text {Self::strlen ($text)-1});
if ($pad > Self::strlen ($text)) return false;
if (strspn ($text, Chr ($pad), Self::strlen ($text)-$pad)!= $pad) return false;
Return substr ($text, 0,-1 * $pad);
}
}

How to use

  code is as follows copy code

<?php
require_once ('./aes.php ');
//$aes = new Aes ();
$aes = new Aes (TRUE);//The encrypted string is stored in hexadecimal
//$aes = new AES (true,true);//With debug information and the encrypted string is stored in hexadecimal
$key = "This is A byte key ";//Key
$keys = $aes->makekey ($key);
$encode =" 123456 ";/encrypted string
$ct = $aes->encryptstrin G ($encode, $keys);
echo "encode =". $ct. " <br> ";
$cpt = $aes->decryptstring ($ct, $keys);
echo "decode =". $cpt;
?

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.