Php symmetric encryption algorithm example _ php instance

Source: Internet
Author: User
Tags mcrypt
This article mainly introduces the php symmetric encryption algorithm example. if you need a friend, refer to the 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 (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 (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, 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:


Header ('content-Type: text/html; Charset = utf-8 ;');

Include "xcrypt. php ";

Echo'

';
$ A = isset ($ _ GET ['A'])? $ _ GET ['A']: 'Test 100 ';

// Key
$ Key = '000000'; // 12345678123456781234567812345678 bit
$ Key = '000000'; // 1234567812345678 bit
$ Key = '000000'; // 64 bit

// Set the mode and IV
$ M = new Xcrypt ($ key, 'cbc', 'auto ');

// Obtain the vector value
Echo 'vector :';
Var_dump ($ m-> getIV ());

// Encryption
$ B = $ m-> encrypt ($ a, 'base64 ');
// Decrypt
$ C = $ m-> decrypt ($ B, 'base64 ');

Echo 'encrypted :';
Var_dump ($ B );
Echo 'decrypted :';
Var_dump ($ c );
Echo'
';

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.