This article mainly introduces the simple symmetric encryption and decryption method implemented by PHP, summarizes the common methods of symmetric encryption and decryption of PHP with the example form, and needs the friend to refer to the following
In this paper, a simple symmetric encryption and decryption method for PHP implementation is described. Share to everyone for your reference, as follows:
Method One: Yii's own encryption method
/*** encryption * @var string [value to encrypt]*/$secretKey = "WWJ"; $data = $res [' u_id ']; $encryptedData = Yii:: $app->getsecurity () Encryptbypassword ($data, $secretKey);
/*** decryption * @var [type] [value before encryption]*/$aid = $req->get (' uid '); $secretKey = "WWJ"; $uid = Yii:: $app->getsecurity () Decryptbypassword ($aid, $secretKey);
Method Two:
/** * Secure URL code * @param type $data * @return type */function encode ($data) {return str_replace (Array (' + ', '/', ' = '), array ('-', ' _ ', '), Base64_encode (Serialize ($data)));} /*** Secure URL decoding * @param type $string * @return type*/function decode ($string) {$data = Str_replace (Array ('-', ' _ '), Array (' + ') , '/'), $string); $mod 4 = strlen ($data)% 4; ($mod 4) && $data. = substr (' = = = ', $mod 4); Return Unserialize (Base64_decode ($data));}
Method Three:
/*** encryption * @param [type] $code [description]* @return [Type] [description]*/public static function Encrypt ($code) {Retu RN UrlEncode (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))));} /** * decryption * @param [type] $code [description] * @return [Type] [description] */public static function decrypt ($code) {R Eturn UrlDecode (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)));}
Method Four:
/*** Simple Symmetric encryption * @param string $string [string]* to be encrypted @param strings $skey [Encrypted key]* @return [type] [encrypted]*/function encode ($st Ring = ', $skey = ' cxphp ') { $STRARR = Str_split (Base64_encode ($string)); $strCount = count ($STRARR); foreach (Str_split ($skey) as $key = + $value) $key < $strCount && $strArr [$key].= $value; return Str_replace (Array (' = ', ' + ', '/'), Array (' o0o0o ', ' o000o ', ' oo00o '), join (', $STRARR));}
/*** simple Symmetric decryption * @param string $string [the encrypted value]* @param string $skey [Encrypted key]* @return [type] [the string before encryption]*/function decode ($s Tring = ', $skey = ' cxphp ') { $STRARR = str_split (Str_replace (' o0o0o ', ' o000o ', ' oo00o '), array (' = ', ' + ', '/'), $string), 2); $strCount = count ($STRARR); foreach (Str_split ($skey) as $key = + $value) $key <= $strCount && isset ($STRARR [$key]) && $strA rr[$key][1] = = = $value && $strArr [$key] = $STRARR [$key][0]; Return Base64_decode (Join (', $STRARR));}
Related recommendations:
Encryption and decryption of data by PHP symmetric encryption function
PHP symmetric encryption Algorithm (DES/AES) class code
PHP symmetric Cryptographic algorithm Example _php tutorial