PHP symmetric encryption Algorithm example _php instance

Source: Internet
Author: User
Tags base64 chr decrypt mcrypt md5


PHP Symmetric encryption algorithm

KEY is the previously defined constant

Copy Code code as follows:

Mcrypt::encrypt ();
Mcrypt::d ecrypt ();

Copy 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 Code code as follows:

/**
* Common symmetric encryption Algorithm class
* Support key: 64/128/256 bit (byte length 8/16/32)
* Support algorithm: Des/aes (according to the key length automatic matching use: 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
*
* @param string key
* @param string mode
* @param string vector ("off": Do not use/"Auto": Auto/Other: specified 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 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 '); The CFB must have a vector
Break
Case ' ECB ':
$this->mode = MCRYPT_MODE_ECB;
$iv = ' off '; The ECB doesn't 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 code (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 code (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 Code code as follows:

<?php
Header (' content-type:text/html; Charset=utf-8; ');

Include "xcrypt.php";

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

Secret key
$key = ' 12345678123456781234567812345678 '; 256 bit
$key = ' 1234567812345678 '; 128 bit
$key = ' 12345678 '; Bit

Set Mode and IV
$m = new Xcrypt ($key, ' CBC ', ' Auto ');

Get vector values
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 ' after decryption: ';
Var_dump ($c);
Echo ' </pre> ';

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.