[PHP] Six Practical Encryption and Decryption Methods

Release your eyes, put on your headphones and listen~!
As we all know, the most important thing to do back-end is security. If security goes wrong, it is a big problem. Today, the blogger shares six PHP encryption and decryption methods, without much nonsense, just code.

 [PHP] Six Practical Encryption and Decryption Methods

First

 <? php   function encryptDecrypt($key, $string, $decrypt){    if($decrypt){    $decrypted = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($string), MCRYPT_MODE_CBC, md5(md5($key))), "12");    return $decrypted;    }else{    $encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $string, MCRYPT_MODE_CBC, md5(md5($key))));    return $encrypted;    }    }    // encryption :"z0JAx4qMwcF+db5TNbp/xwdUM84snRsXvvpXuaCa4Bk="   Echo encryptDecrypt ('password ','Hello weba Welcome', 0); // decrypt : "Hello weba welcomes you" echo encryptDecrypt('password', 'z0JAx4qMwcF+db5TNbp/xwdUM84snRsXvvpXuaCa4Bk=',1);   ?>

Second

 <? php   //Cryptographic function function lock_url($txt,$key='liiu'){   $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";   $nh = rand(0,64);   $ch = $chars[$nh];   $mdKey = md5($key.$ch);   $mdKey = substr($mdKey,$nh%8, $nh%8+7);   $txt = base64_encode($txt);   $tmp = '';   $i=0;$ j=0;$k = 0;   for ($i=0; $ i<strlen($txt); $ i++) {   $k = $k == strlen($mdKey) ?  0 : $k;   $j = ($nh+strpos($chars,$txt[$i])+ord($mdKey[$k++]))%64;   $tmp .= $ chars[$j];   }   return urlencode($ch.$tmp);   }   // decrypt Function function unlock_url($txt,$key='liiu'){   $txt = urldecode($txt);   $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";   $ch = $txt[0];   $nh = strpos($chars,$ch);   $mdKey = md5($key.$ch);   $mdKey = substr($mdKey,$nh%8, $nh%8+7);   $txt = substr($txt,1);   $tmp = '';   $i=0;$ j=0; $k = 0;   for ($i=0; $ i<strlen($txt); $ i++) {   $k = $k == strlen($mdKey) ?  0 : $k;   $j = strpos($chars,$txt[$i])-$nh - ord($mdKey[$k++]);   while ($j<0) $j+=64;   $tmp .= $ chars[$j];   }   return base64_decode($tmp);   }   ?>

Third

 <? php   //Improved algorithm //Cryptographic function function lock_url($txt,$key='str'){   $txt = $txt.$ key;   $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";   $nh = rand(0,64);   $ch = $chars[$nh];   $mdKey = md5($key.$ch);   $mdKey = substr($mdKey,$nh%8, $nh%8+7);   $txt = base64_encode($txt);   $tmp = '';   $i=0;$ j=0;$k = 0;   for ($i=0; $ i<strlen($txt); $ i++) {   $k = $k == strlen($mdKey) ?  0 : $k;   $j = ($nh+strpos($chars,$txt[$i])+ord($mdKey[$k++]))%64;   $tmp .= $ chars[$j];   }   return urlencode(base64_encode($ch.$tmp));   }   // decrypt Function function unlock_url($txt,$key='str'){   $txt = base64_decode(urldecode($txt));   $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";   $ch = $txt[0];   $nh = strpos($chars,$ch);   $mdKey = md5($key.$ch);   $mdKey = substr($mdKey,$nh%8, $nh%8+7);   $txt = substr($txt,1);   $tmp = '';   $i=0;$ j=0; $k = 0;   for ($i=0; $ i<strlen($txt); $ i++) {   $k = $k == strlen($mdKey) ?  0 : $k;   $j = strpos($chars,$txt[$i])-$nh - ord($mdKey[$k++]);   while ($j<0) $j+=64;   $tmp .= $ chars[$j];   }   return trim(base64_decode($tmp),$key);   }   ?>

Fourth

 <? php   function passport_encrypt($txt, $key = 'liiu') {    srand((double)microtime() * 1000000);    $encrypt_key = md5(rand(0, 32000));    $ctr = 0;    $tmp = '';    for($i = 0;$ i < strlen($txt); $ i++) {    $ctr = $ctr == strlen($encrypt_key) ?  0 : $ctr;    $tmp .= $ encrypt_key[$ctr]. ($txt[$i] ^ $encrypt_key[$ctr++]);    }    return urlencode(base64_encode(passport_key($tmp, $key)));    }    function passport_decrypt($txt, $key = 'liiu') {    $txt = passport_key(base64_decode(urldecode($txt)), $key);    $tmp = '';    for($i = 0;$ i < strlen($txt); $ i++) {    $md5 = $txt[$i];    $tmp .= $ txt[++$i] ^ $md5;    }    return $tmp;    }    function passport_key($txt, $encrypt_key) {    $encrypt_key = md5($encrypt_key);    $ctr = 0;    $tmp = '';    for($i = 0; $ i < strlen($txt); $ i++) {    $ctr = $ctr == strlen($encrypt_key) ?  0 : $ctr;    $tmp .= $ txt[$i] ^ $encrypt_key[$ctr++];    }    return $tmp;    }    $txt = "1";    $key = "testkey";    $encrypt = passport_encrypt($txt,$key);    $decrypt = passport_decrypt($encrypt,$key);    echo $encrypt."<br>";    echo $decrypt."<br>";    ?>

Fifth

 <? php   //Very powerful authcode encryption function, Discuz! Classic code (with detailed explanation) //$string in function authcode ($string, $operation, $key, $expiry): string, plaintext or ciphertext$ Operation: DECODE decrypt , others represent encryption$ Key: key$ Expiry: the validity period of the ciphertext. function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {      //Dynamic key length. The same plaintext will generate different ciphertext depending on the dynamic key $ckey_length = 4;      //Key $key = md5($key ? $key : $GLOBALS['discuz_auth_key']);      //Key a will participate in adding decrypt $keya = md5(substr($key, 0, 16));      //Key b will be used for data integrity verification $keyb = md5(substr($key, 16, 16));      //Key c is used to change the generated ciphertext $keyc = $ckey_length ?  ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';      //Key participating in the operation $cryptkey = $keya.md5($keya.$keyc);      $key_length = strlen($cryptkey);      //Clear text, the first 10 bits are used to save the time stamp, verify the validity of the data when decrypting, and 10 to 26 bits are used to save $keyb (key b), //The data integrity will be verified through this key during decryption //If it is decoded, it will start from the $key_length bit, because the dynamic key is saved in the $key_length bit before the ciphertext to ensure correct decryption $string = $operation == 'DECODE' ?  base64_decode(substr($string, $ckey_length)) :  sprintf('%010d', $expiry ? $ expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$ string;      $string_length = strlen($string);      $result = '';      $box = range(0, 255);      $rndkey = array();      //Generate Keybook for($i = 0; $ i <= 255; $i++) {      $rndkey[$i] = ord($cryptkey[$i % $key_length]);      }      //It seems very complicated to use a fixed algorithm to disrupt the key book and increase randomness. In fact, it does not increase the strength of ciphertext for($j = $i = 0; $ i < 256; $i++) {      $j = ($j + $box[$i] + $rndkey[$i]) % 256;      $tmp = $box[$i];      $box[$i] = $box[$j];      $box[$j] = $tmp;      }      //Core encryption and decryption part for($a = $j = $i = 0; $ i < $string_length; $i++) {      $a = ($a + 1) % 256;      $j = ($j + $box[$a]) % 256;      $tmp = $box[$a];      $box[$a] = $box[$j];      $box[$j] = $tmp;      //The key obtained from the key book is XOR, and then converted into characters $result .=  chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));      }      if($operation == 'DECODE') {     //To verify the validity of data, see the format of unencrypted plaintext 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 {      //The dynamic key is stored in the ciphertext, which is why the same plaintext can be decrypted after producing different ciphertext //Because the encrypted ciphertext may be some special characters, and the copy process may be lost, it is encoded with base64 return $keyc.str_replace('=', '', base64_encode($result));      }      }    $str = 'abcdef';    $key = 'www.helloweba.com';    echo authcode($str,'ENCODE',$key,0); // Encryption $str = '56f4yER1DI2WTzWMqsfPpS9hwyoJnFP2MpC8SOhRrxO7BOk';    echo authcode($str,'DECODE',$key,0); // decrypt ?>

Sixth

 <? php   //$string in function encrypt ($string, $operation, $key): the string to be encrypted and decrypted$ Operation: determine whether to encrypt or decrypt, E indicates encryption, D indicates decryption$ Key: key. function encrypt($string,$operation,$key=''){    $key=md5($key);    $key_length=strlen($key);    $string=$operation=='D'? base64_decode($string):substr(md5($string.$key),0,8).$ string;    $string_length=strlen($string);    $rndkey=$box=array();    $result='';    for($i=0;$ i<=255;$i++){    $rndkey[$i]=ord($key[$i%$key_length]);    $box[$i]=$i;    }    for($j=$i=0;$ i<256;$i++){    $j=($j+$box[$i]+$rndkey[$i])%256;    $tmp=$box[$i];    $box[$i]=$box[$j];    $box[$j]=$tmp;    }    for($a=$j=$i=0;$ i<$string_length;$i++){    $a=($a+1)%256;    $j=($j+$box[$a])%256;    $tmp=$box[$a];    $box[$a]=$box[$j];    $box[$j]=$tmp;    $result.= chr(ord($string[$i])^($box[($box[$a]+$box[$j])%256]));    }    if($operation=='D'){    if(substr($result,0,8)==substr(md5(substr($result,8).$ key),0,8)){    return substr($result,8);    }else{    return'';    }    }else{    return str_replace('=','',base64_encode($result));    }    }    $str = 'abc';    $key = 'www.helloweba.com';    $token = encrypt($str, 'E', $key);    Echo 'Encryption:'. encrypt ($str, 'E', $key); Echo 'Decryption:'. encrypt ($str, 'D', $key); ?>

This article is transferred from https://www.cnblogs.com/ningjiabing/p/11265204.html

Statement: All articles on this site, unless otherwise specified or marked, are originally published on this site. No individual or organization is allowed to copy, embezzle, collect and publish the content of this website to any website, book and other media platforms without the consent of this website. If the content of this website infringes upon the legitimate rights and interests of the original author, please contact us for handling.

Appreciate TA
A total of {{data. count}} people
People have appreciated
dried food

28 new features of Android 11's first developer preview

2020-4-23 17:15:07

Windows dried food

Black Apple Windows time out of sync? How to solve it? Then make an automatic synchronization plan!

2020-12-14 21:05:46

0 replies A Author M administrators
    There is no discussion yet. Tell me your opinion
Personal Center
Shopping Cart
Coupon
Sign in today
There are new private messages Private Message List
search