» » Use PHP to deal with string encryption and decryption

 

Use PHP to deal with string encryption and decryption

Author: bamboo06 on 5-11-2014, 01:37, views: 2853

2
Sometimes we need to project a specific PHP information is encrypted, encrypted string that is generated by an encryption algorithm, the encrypted string can be decrypted by the decryption algorithm, to facilitate the decrypted program information is processed.
Use PHP to deal with string encryption and decryption

The most common applications in the user login, and some API data exchange scenarios.
I included some of the more classic PHP code encryption and decryption functions for everyone to share. Cryptographic principles generally through some encryption and decryption algorithms, the key is added to the algorithm, encryption and decryption finally get results.
1 Very awesome authcode encryption functions (with Detailed)

function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {   
    // Dynamic key length, the same plaintext will produce different ciphertext is to rely on dynamic key
    $ckey_length = 4;   
       
    // key   
    $key = md5($key ? $key : $GLOBALS['discuz_auth_key']);   
       
    // Will participate in a decryption key   
    $keya = md5(substr($key, 0, 16));   
    // Key b will be used for data integrity verification  
    $keyb = md5(substr($key, 16, 16));   
    // Ciphertext c keys generated for change   
    $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): 
substr(md5(microtime()), -$ckey_length)) : '';   
    // Involved in key operations   
    $cryptkey = $keya.md5($keya.$keyc);   
    $key_length = strlen($cryptkey);   
    // Plaintext, before 10 to save the timestamp to verify the validity of data decryption, 10-26 to save $ keyb (key b), 
//Will verify data integrity through this key decryption   
    // If it is decoded, it will start from the first $ ckey_length bit, because the first ciphertext $ ckey_length-save dynamic key, in order to ensure the 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 keys book  
    for($i = 0; $i <= 255; $i++) {   
        $rndkey[$i] = ord($cryptkey[$i % $key_length]);   
    }   
    // With a fixed algorithm, disrupting key book, increasing randomness, it seems very complicated, actually does not increase the intensity of the 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;   
    }   
    // Encryption and decryption core 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;   
        // Key results from the key book XOR, then into character   
        $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));   
    }   
    if($operation == 'DECODE') {  
        // Validate data validation, see 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 {   
        // The reason for the dynamic key stored in cipher text, which is why the same plaintext, ciphertext can produce different after decryption   
        // Because encrypted ciphertext may be some special characters, the replication process may be lost, so use base64 encoding   
        return $keyc.str_replace('=', '', base64_encode($result));   
    }   
}

Function authcode ($ string, $ operation, $ key, $ expiry) of $ string: string, plain or cipher; $ operation: DECODE, said decryption, other representations encryption; $ key: Key; $ expiry: ciphertext expiration date.
Usage:

$str = 'abcdef'; 
$key = 'www.goocode.net'; 
echo authcode($str,'ENCODE',$key,0); //Encryption 
$str = '56f4yER1DI2WTzWMqsfPpS9hwyoJnFP2MpC8SOhRrxO7BOk'; 
echo authcode($str,'DECODE',$key,0); //Decryption 


2 the encryption and decryption functions encrypt ()

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)); 
    } 
} 

Function encrypt ($ string, $ operation, $ key) in $ string: require encryption and decryption of string; $ operation: judgment is encryption or decryption, E is encryption, D represents the decryption; $ key: the key.
Usage:

$str = 'abc'; 
$key = 'www.goocode.net'; 
$token = encrypt($str, 'E', $key); 
echo 'Encryption: '.encrypt($str, 'E', $key); 
echo 'Decryption: '.encrypt($str, 'D', $key); 

Category: PHP Scripts

Dear visitor, you are browsing our website as Guest.
We strongly recommend you to register and login to view hidden contents.
<
  • 0 Comments
  • 0 Articles
16 December 2017 22:58

Jack

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
PHP is a good source that we use for many purpose but people not know we also generate short urls through PHP language.An paper writer service define it more clearly about PHP language usage.

<
  • 0 Comments
  • 0 Articles
7 January 2018 15:18

TAO

Reply
  • Group: Guests
  • РRegistered date: --
  • Status:
 
It fares well as an active wear also; say tennis, golf or rolex replica sale horse riding. In water, it can go down till 200 feet, to be on the safe side. The price range of the replica watches sale is pretty low compared to its Jap, Swiss or American counterparts and are simpler to operate. Of course the rolex replica Jap and Swiss make some astounding stuff but complexity costs more. Those are bit too technical as well (mostly) and doesn’t suit well right at the first. Even veteran replica watches uk users may go for The rolex replica uk as a piece for general, every day use while taking out their prized, very expensive, higher end hublot replica uk from Jap and Swiss brands only on special occasions.

Information
Comment on the news site is possible only within (days) days from the date of publication.