- /**
- * 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);
- }
- 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;
- }
- }
Copy CodeExamples of Use:
- !--? php header (' content-type:text/html; Charset=utf-8; ');
-
- include "xcrypt.php";
-
- Echo '
//////////////////////////////////////
- $a = isset ($_get[' a '])? $_get[' A ']: ' Test 123 ';
-
- //Key
- $key = ' 12345678123456781234567812345678 ';//256 bit
- $key = ' 1234567812345678 ';//1 The-bit
- $key = ' 12345678 ';//64 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 ');
- //Decryption
- $c = $m->decrypt ($b, ' base64 ');
-
- Echo ' after encryption: ';
- Var_dump ($b);
- Echo ' after decryption: ';
- Var_dump ($c);
-
-
- /////////////////////////////////////////
- echo '
';
Copy Code
|