Encrypt data and decrypt data using AES encryption algorithm in PHP

Source: Internet
Author: User
Tags decrypt ord

This algorithm encrypts the data, stores it, and restores it using the previously encrypted secret key when it is needed.

In addition to this, there is the AES algorithm can be very good data encryption, in the transmission process is not easy to crack.

In PHP, we must first install the MCrypt module, and add the corresponding version of the extension to PHP.

AES encryption mode and Padding methods have the following, but not all

Copy CodeThe code is as follows: algorithm/mode/padding 16 bytes After encryption data length less than 16 bytes after encryption length
Aes/cbc/nopadding 16 Not supported
Aes/cbc/pkcs5padding 32 16
Aes/cbc/iso10126padding 32 16
Aes/cfb/nopadding 16 Raw data length
Aes/cfb/pkcs5padding 32 16
Aes/cfb/iso10126padding 32 16
Aes/ecb/nopadding 16 Not supported
Aes/ecb/pkcs5padding 32 16
Aes/ecb/iso10126padding 32 16
Aes/ofb/nopadding 16 Raw data length
Aes/ofb/pkcs5padding 32 16
Aes/ofb/iso10126padding 32 16
Aes/pcbc/nopadding 16 Not supported
Aes/pcbc/pkcs5padding 32 16
Aes/pcbc/iso10126padding 32 16


The following is the use of AES in PHP to encrypt data

AES-CBC Encryption Scheme


Copy CodeThe code is as follows: <?php
$privateKey = "1234567812345678";
$iv = "1234567812345678";
$data = "Test String";


Encryption

Copy CodeThe code is as follows: $encrypted = Mcrypt_encrypt (mcrypt_rijndael_128, $privateKey, $data, MCRYPT_MODE_CBC, $IV);
$encrypted = Base64_encode ($encrypted);



Decrypt

Copy CodeThe code is as follows: $decrypted = Base64_decode ($encrypted);
$decrypted = Mcrypt_decrypt (mcrypt_rijndael_128, $privateKey, $decrypted, MCRYPT_MODE_CBC, $IV);
Echo $decrypted;
?>


AES-ECB Encryption Scheme

Copy CodeThe code is as follows: <?php
Encryption
$key = ' 1234567890123456 ';
$content = ' Hello ';
$padkey = Pad2length ($key, 16);
$cipher = Mcrypt_module_open (mcrypt_rijndael_128, ', MCRYPT_MODE_ECB, ');
$iv _size = mcrypt_enc_get_iv_size ($cipher);
$iv = Mcrypt_create_iv ($iv _size, Mcrypt_rand); #IV自动生成?
Echo ' automatically generates the length of IV: '. strlen ($IV). ' Bit: '. Bin2Hex ($IV). ' <br> ';
if (Mcrypt_generic_init ($cipher, Pad2length ($key, +), $IV)! =-1)
{
PHP pads with NULL bytes If $content are not a multiple of the block size:
$cipherText = Mcrypt_generic ($cipher, Pad2length ($content, 16));
Mcrypt_generic_deinit ($cipher);
Mcrypt_module_close ($cipher);

Display the result in hex.
printf ("128-bit encrypted result:\n%s\n\n", Bin2Hex ($cipherText));
Print ("<br/>");

}



Decrypt

Copy CodeThe code is as follows: $MW = Bin2Hex ($cipherText);
$TD = Mcrypt_module_open (mcrypt_rijndael_128, ', MCRYPT_MODE_ECB, ');
if (Mcrypt_generic_init ($TD, $padkey, $iv)! =-1)
{
$p _t = Mdecrypt_generic ($TD, Hextostr ($MW));
Mcrypt_generic_deinit ($TD);
Mcrypt_module_close ($TD);

$p _t = trimEnd ($p _t);
Echo ' decryption: ';
Print ($p _t);
Print ("<br/>");
Print (Bin2Hex ($p _t));
Echo ' <br><br> ';
}


$text complements the length of $padlen multiples

Copy CodeThe code is as follows: function Pad2length ($text, $padlen) {
$len = strlen ($text)% $padlen;
$res = $text;
$span = $padlen-$len;
for ($i =0; $i < $span; $i + +) {
$res. = Chr ($span);
}
return $res;
}



Remove the extra length after decryption (because the length of the block_size is satisfied when the encryption is added)

Copy CodeThe code is as follows: function TrimEnd ($text) {
$len = strlen ($text);
$c = $text [$len-1];
if (Ord ($c) < $len) {
for ($i = $len-ord ($c); $i < $len; $i + +) {
if ($text [$i]! = $c) {
return $text;
}
}
Return substr ($text, 0, $len-ord ($c));
}
return $text;
}
16 Binary to 2 binary string
function Hextostr ($hex)
{
$bin = "";
for ($i =0; $i <strlen ($hex)-1; $i +=2)
{
$bin. =CHR (Hexdec ($hex [$i]. $hex [$i +1]);
}
return $bin;
}


AES-ECB Encryption Scheme

Copy CodeThe code is as follows: <?php
$key = ' 1234567890123456 ';
$key = Pad2length ($key, 16);
$iv = ' ASDFF ';
$content = ' Hello ';
$content = Pad2length ($content, 16);
$AESed = Bin2Hex (Mcrypt_encrypt (mcrypt_rijndael_128, $key, $content, MCRYPT_MODE_ECB, $iv)); #加密
echo "128-bit encrypted Result:". $AESed. ' <br> ';
$jiemi = Mcrypt_decrypt (mcrypt_rijndael_128, $key, Hextostr ($AESed), MCRYPT_MODE_ECB, $IV); #解密
Echo ' decryption: ';
echo trimEnd ($jiemi);
?>


The above is just my list of simple 3 encryption methods, in fact there are many methods, we need to continue to learn. The road to cryptography is still a long way off.

Reprint please specify (B5 tutorial net) original link: http://www.bcty365.com/content-10-723-1.html

Encrypt data and decrypt data using AES encryption algorithm in 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.