Example of encrypting data using AES encryption algorithm in PHP

Source: Internet
Author: User
Tags chr decrypt mcrypt ord strlen

When studying Discuz, we found that Discuz has a fairly perfect encryption algorithm (relative). This algorithm can encrypt the data, save it, and then restore it using the secret key that was previously encrypted when needed.

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

In PHP, we must first install the MCrypt this module, and add the appropriate version of the extension to PHP, the details can be seen without recompiling the PHP installation mcrypt extension

AES encryption mode and fill mode are as follows, but not all

Algorithm/mode/padding 16-byte encryption after data length less than 16 bytes encrypted length
Aes/cbc/nopadding 16 does not support
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 does not support
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 does not support
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

The code is as follows Copy Code

<?php
$privateKey = "1234567812345678";
$iv = "1234567812345678";
$data = "Test String";

Encryption
$encrypted = Mcrypt_encrypt (mcrypt_rijndael_128, $privateKey, $data, MCRYPT_MODE_CBC, $IV);
Echo (Base64_encode ($encrypted));
Echo ' <br/> ';

Decrypt
$encryptedData = Base64_decode ("2fbww9+8vpid2/foafzq6q==");
$decrypted = Mcrypt_decrypt (mcrypt_rijndael_128, $privateKey, $encryptedData, MCRYPT_MODE_CBC, $IV);
Echo ($decrypted);
?>

AES-ECB Encryption Scheme

The code is as follows Copy Code

<?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%snn", Bin2Hex ($cipherText));
Print ("<br/>");

}
Decrypt
$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 ' Decrypt: ';
Print ($p _t);
Print ("<br/>");
Print (Bin2Hex ($p _t));
Echo ' <br><br> ';
}
To complement the length of the $padlen multiples by $text
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 (since the length of the block_size is satisfied at the time of encryption)
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 into the 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

The code is as follows Copy Code

<?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 ' Decrypt: ';
echo TrimEnd ($jiemi);
?>

These are just a few of the simple 3 encryption methods I have listed, in fact there are many ways in which we need to continue to learn. The road to cryptography is still a long way off.

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.