PHP symmetric Cryptographic algorithm Example _php tutorial

Source: Internet
Author: User
PHP Symmetric encryption algorithm

KEY is a previously defined constant
Copy the Code code as follows:
Mcrypt::encrypt ();
Mcrypt::d ecrypt ();

Copy the Code code as follows:
Defined (' ROOT ') or exit (' Access Denied ');

Class mcrypt{

public static function Encrypt ($code) {
Return Base64_encode (Mcrypt_encrypt (mcrypt_rijndael_256, MD5 (KEY), $code, MCRYPT_MODE_ECB, Mcrypt_create_iv (mcrypt_ Get_iv_size (mcrypt_rijndael_256, MCRYPT_MODE_ECB), Mcrypt_rand));
}

public static function decrypt ($code) {
Return Mcrypt_decrypt (mcrypt_rijndael_256, MD5 (KEY), Base64_decode ($code), MCRYPT_MODE_ECB, Mcrypt_create_iv (mcrypt _get_iv_size (mcrypt_rijndael_256, MCRYPT_MODE_ECB), Mcrypt_rand));
}

}

Common symmetric encryption Algorithm (DES/AES) class

xcrypt.php
Copy CodeThe code is as follows:
/**
* Common symmetric encryption algorithm classes
* Support key: 64/128/256 bit (byte length 8/16/32)
* Support algorithm: Des/aes (based on key length automatic matching using: Des:64bit aes:128/256bit)
* Support mode: CBC/ECB/OFB/CFB
* Ciphertext Code: Base64 string/16 binary string/binary string stream
* Fill mode: 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 function
*
* @param string key
* @param string mode
* @param string vector ("off": Not used/"Auto": Auto/Other: Specify value, length with 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 is 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 '); The CFB must have a vector
Break
Case ' ECB ':
$this->mode = MCRYPT_MODE_ECB;
$iv = ' off '; The ECB does not need vectors
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;
}


}


/**
* Get vector values
* @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 cipher encoding (Base64/hex/bin)
* @return String cipher
*/
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 cipher
* @param string cipher 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;
}
}

How to use the above class

Copy the Code code as follows:
Header (' content-type:text/html; Charset=utf-8; ');

Include "xcrypt.php";

Echo '

';    
$a = isset ($_get[' a ')? $_get[' A ']: ' Test 123 ';

Secret key
$key = ' 12345678123456781234567812345678 '; Bit
$key = ' 1234567812345678 '; -bit
$key = ' 12345678 '; A bit

Setup mode and IV
$m = new Xcrypt ($key, ' CBC ', ' Auto ');

Get vector value
echo ' vector: ';
Var_dump ($m->getiv ());

Encryption
$b = $m->encrypt ($a, ' base64 ');
Decrypt
$c = $m->decrypt ($b, ' base64 ');

Echo ' after encryption: ';
Var_dump ($b);
Echo ' decryption: ';
Var_dump ($c);
Echo '
';

http://www.bkjia.com/PHPjc/766111.html www.bkjia.com true http://www.bkjia.com/PHPjc/766111.html techarticle the PHP symmetric encryption algorithm KEY is a previously defined constant copy code code as follows: Mcrypt::encrypt (); Mcrypt::d ecrypt (); Copy the code code as follows: Defined (' ROOT ') or exit (' Access Denie ...

  • 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.