[Notes] several PHP encryption algorithms-php Tutorial

Source: Internet
Author: User
Tags crypt
[Note] several PHP encryption algorithms 1. Discuz authcode
 0 verify data validity // substr ($ result, 10, 16) = substr (md5 (substr ($ result, 26 ). $ keyb), 0, 16) verify data integrity // verify data validity. please refer to the unencrypted plaintext format if (substr ($ result, 0, 10) = 0 | substr ($ result, 0, 10)-time ()> 0) & substr ($ result, 10, 16) = substr (md5 (substr ($ result, 26 ). $ keyb), 0, 16) {return substr ($ result, 26) ;}else {return '';}} else {// Save the dynamic key in the ciphertext, this is also the reason why different ciphertext texts can be decrypted in the same plain text. // because the encrypted ciphertext may be special characters, the replication process may be lost, therefore, return $ keyc is encoded in base64 format. str_replace ('=', '', base64_encode ($ result) ;}}$ a =" www.test.com "; $ B = authcode ($ a," ENCODE ", "abc123"); echo $ B."
"; Echo authcode ($ B," DECODE "," abc123 ");
2. simple symmetric encryption algorithm
 * @ Date * @ update 2014-10-10 * @ return String */function encode ($ string = '', $ 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 ));} /*** decryption of simple symmetric encryption algorithm * @ param String $ string the String to be decrypted * @ param String $ skey decryption KEY * @ author Anyon Zou
 
  
* @ Date * @ update 2014-10-10 * @ return String */function decode ($ string = '', $ skey = 'cxphp ') {$ strArr = str_split (str_replace (array ('Oss 0o', 'Oss 000o', 'oo00o'), array ('=', '+ ','/'), $ string), 2); $ strCount = count ($ strArr); foreach (str_split ($ skey) as $ key => $ value) $ key <= $ strCount & isset ($ strArr [$ key]) & $ strArr [$ key] [1] ===$ value & $ strArr [$ key] = $ strArr [$ key] [0]; return base64_decode (join ('', $ strArr);} echo'
  
'; $ Str = '2014 23365247,54, four classical styles'; echo "string:". $ str ."
"; Echo" encode: ". ($ enstring = encode ($ str )).'
'; Echo "decode:". decode ($ enstring );
3. DES encryption and decryption
   Key = $ key; if ($ iv = 0) {$ this-> iv = $ key;} else {$ this-> iv = $ iv ;}} // encryption function encrypt ($ str) {$ size = mcrypt_get_block_size (MCRYPT_DES, MCRYPT_MODE_CBC); $ str = $ this-> pkcs5Pad ($ str, $ size ); $ data = mcrypt_cbc (MCRYPT_DES, $ this-> key, $ str, MCRYPT_ENCRYPT, $ this-> iv); // $ data = strtoupper (bin2hex ($ data )); // return the uppercase hexadecimal string return base64_encode ($ data);} // decrypt func Tion decrypt ($ str) {$ str = base64_decode ($ str); // $ strBin = $ this-> hex2bin (strtolower ($ str )); $ str = mcrypt_cbc (MCRYPT_DES, $ this-> key, $ str, MCRYPT_DECRYPT, $ this-> iv); $ str = $ this-> pkcs5Unpad ($ str ); return $ str;} function hex2bin ($ hexData) {$ binData = ""; for ($ I = 0; $ I <strlen ($ hexData); $ I + = 2) {$ binData. = chr (hexdec (substr ($ hexData, $ I, 2 )));} Return $ binData;} function pkcs5Pad ($ text, $ blocksize) {$ pad = $ blocksize-(strlen ($ text) % $ blocksize); return $ text. str_repeat (chr ($ pad), $ pad);} 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; return substr ($ text, 0,-1 * $ pad) ;}}$ str = 'abc'; $ key = '2016 '; // $ crypt = new DES ($ key); $ mstr = $ crypt-> encrypt ($ str ); $ str = $ crypt-> decrypt ($ mstr); echo $ str. '<=> '. $ mstr;
4. PHP hex2bin
   '; Echo hexXbin ($ t );

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.