PHP for symmetric encryption and decryption

Source: Internet
Author: User
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

Related Article

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.