Example of php symmetric encryption algorithm

Source: Internet
Author: User
Tags mcrypt
This article mainly introduces the example of the php symmetric encryption algorithm. For more information, see

This article mainly introduces the example of the php symmetric encryption algorithm. For more information, see


Php symmetric encryption algorithm

KEY is a previously defined constant.

The Code is as follows:


Mcrypt: encrypt ();
Mcrypt: decrypt ();

The Code is as follows:


Defined ('root') or exit ('Access Denied ');

Class Mcrypt {

Public static function encrypt ($ code ){
Return base64_encode (mcrypt_encrypt (encrypt, md5 (KEY), $ code, MCRYPT_MODE_ECB, mcrypt_create_iv (encrypt, MCRYPT_MODE_ECB), MCRYPT_RAND )));
}

Public static function decrypt ($ code ){
Return mcrypt_decrypt (encrypt, md5 (KEY), base64_decode ($ code), MCRYPT_MODE_ECB, mcrypt_create_iv (encrypt, MCRYPT_MODE_ECB), MCRYPT_RAND ));
}

}

Common symmetric encryption algorithms (DES/AES)

Xcrypt. php

The Code is as follows:


/**
* Common symmetric encryption algorithms
* Supported keys: 64/128/256 bit (Byte Length: 8/16/32)
* Supported algorithms: DES/AES (Auto match based on key length: DES: 64bit AES: 128/256 bit)
* Supported mode: CBC/ECB/OFB/CFB
* Ciphertext encoding: base64 string/hexadecimal string/binary string stream
* Filling Method: PKCS5Padding (DES)
*
* @ Author: linvo
* @ Version: 1.0.0
* @ Date: 2013/1/10
*/
Class Xcrypt {

Private $ mcrypt;
Private $ key;
Private $ mode;
Private $ iv;
Private $ blocksize;

/**
* Constructor
*
* @ Param string key
* @ Param string Mode
* @ Param string vector ("off": Not used/"auto": automatic/Others: specified value, with the same length as the key)
*/
Public function _ construct ($ key, $ mode = 'cbc', $ iv = "off "){
Switch (strlen ($ key )){
Case 8:
$ This-> mcrypt = MCRYPT_DES;
Break;
Case 16:
$ This-> mcrypt = MCRYPT_RIJNDAEL_128;
Break;
Case 32:
$ This-> mcrypt = MCRYPT_RIJNDAEL_256;
Break;
Default:
Die ("Key size must be 8/16/32 ");
}

$ This-> key = $ key;

Switch (strtolower ($ mode )){
Case 'ofb ':
$ This-> mode = MCRYPT_MODE_OFB;
If ($ iv = 'off') die ('ofb must give a IV'); // OFB must have a vector
Break;
Case 'cfb ':
$ This-> mode = MCRYPT_MODE_CFB;
If ($ iv = 'off') die ('cfb must give a IV'); // CFB must have a vector
Break;
Case 'ecb ':
$ This-> mode = MCRYPT_MODE_ECB;
$ Iv = 'off'; // vector not required by ECB
Break;
Case 'cbc ':
Default:
$ This-> mode = MCRYPT_MODE_CBC;
}

Switch (strtolower ($ iv )){
Case "off ":
$ This-> iv = null;
Break;
Case "auto ":
$ Source = PHP_ OS = 'winnt '? MCRYPT_RAND: MCRYPT_DEV_RANDOM;
$ This-> iv = mcrypt_create_iv (mcrypt_get_block_size ($ this-> mcrypt, $ this-> mode), $ source );
Break;
Default:
$ This-> iv = $ iv;
}


}


/**
* Obtain Vector Value
* @ Param string Vector Value encoding (base64/hex/bin)
* @ Return string Vector Value
*/
Public function getIV ($ code = 'base64 '){
Switch ($ code ){
Case 'base64 ':
$ Ret = base64_encode ($ this-> iv );
Break;
Case 'hex ':
$ Ret = bin2hex ($ this-> iv );
Break;
Case 'bin ':
Default:
$ Ret = $ this-> iv;
}
Return $ ret;
}


/**
* Encryption
* @ Param string plaintext
* @ Param string ciphertext encoding (base64/hex/bin)
* @ Return string ciphertext
*/
Public function encrypt ($ str, $ code = 'base64 '){
If ($ this-> mcrypt = MCRYPT_DES) $ str = $ this-> _ pkcs5Pad ($ str );

If (isset ($ this-> iv )){
$ Result = mcrypt_encrypt ($ this-> mcrypt, $ this-> key, $ str, $ this-> mode, $ this-> iv );
} Else {
@ $ Result = mcrypt_encrypt ($ this-> mcrypt, $ this-> key, $ str, $ this-> mode );
}

Switch ($ code ){
Case 'base64 ':
$ Ret = base64_encode ($ result );
Break;
Case 'hex ':
$ Ret = bin2hex ($ result );
Break;
Case 'bin ':
Default:
$ Ret = $ result;
}

Return $ ret;

}

/**
* Decryption
* @ Param string ciphertext
* @ Param string ciphertext encoding (base64/hex/bin)
* @ Return string plaintext
*/
Public function decrypt ($ str, $ code = "base64 "){
$ Ret = false;

Switch ($ code ){
Case 'base64 ':
$ Str = base64_decode ($ str );
Break;
Case 'hex ':
$ Str = $ this-> _ hex2bin ($ str );
Break;
Case 'bin ':
Default:
}

If ($ str! = False ){
If (isset ($ this-> iv )){
$ Ret = mcrypt_decrypt ($ this-> mcrypt, $ this-> key, $ str, $ this-> mode, $ this-> iv );
} Else {
@ $ Ret = mcrypt_decrypt ($ this-> mcrypt, $ this-> key, $ str, $ this-> mode );
}
If ($ this-> mcrypt = MCRYPT_DES) $ ret = $ this-> _ pkcs5Unpad ($ ret );
$ Ret = trim ($ ret );
}

Return $ ret;
}

Private function _ pkcs5Pad ($ text ){
$ This-> blocksize = mcrypt_get_block_size ($ this-> mcrypt, $ this-> mode );
$ Pad = $ this-> blocksize-(strlen ($ text) % $ this-> blocksize );
Return $ text. str_repeat (chr ($ pad), $ pad );
}

Private function _ pkcs5Unpad ($ text ){
$ Pad = ord ($ text {strlen ($ text)-1 });
If ($ pad> strlen ($ text) return false;
If (strspn ($ text, chr ($ pad), strlen ($ text)-$ pad )! = $ Pad) return false;
$ Ret = substr ($ text, 0,-1 * $ pad );
Return $ ret;
}

Private function _ hex2bin ($ hex = false ){
$ Ret = $ hex! = False & preg_match ('/^ [0-9a-fA-F] + $/I', $ hex )? Pack ("H *", $ hex): false;
Return $ ret;
}
}

Usage of the above class

The Code is as follows:

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.