2 more classic PHP encryption and decryption function sharing _php instance

Source: Internet
Author: User
Tags chr decrypt md5 ord sha1 encryption

Sometimes in the project we need to use PHP to encrypt specific information, that is, through the encryption algorithm to generate an encrypted string, the encrypted string can be decrypted by decryption algorithm to facilitate the process of decryption information processing.
The most common applications are in user login and some API data exchange scenarios.

The author contains some of the more classic PHP encryption and decryption function code, to share with you. Encryption and decryption principle is generally through a certain encryption and decryption algorithm, the key into the algorithm, the end of the encryption and decryption results.
1, very authcode encryption function, discuz! Classic code (with detailed):

Copy Code code as follows:

function Authcode ($string, $operation = ' DECODE ', $key = ', $expiry = 0) {
Dynamic key length, the same plaintext will generate different ciphertext is depending on the dynamic key
$ckey _length = 4;

Secret Spoon
$key = MD5 ($key $key: $GLOBALS [' Discuz_auth_key ']);

Key A will participate in encryption and decryption
$keya = MD5 (substr ($key, 0, 16));
Key B is used for data integrity verification.
$KEYB = MD5 (substr ($key, 16, 16));
Key C used to change the generated ciphertext
$KEYC = $ckey _length? ($operation = = ' DECODE ' substr ($string, 0, $ckey _length):
SUBSTR (MD5 (Microtime ()),-$ckey _length)): ';
Keys for participating operations
$cryptkey = $keya. MD5 ($keya. $KEYC);
$key _length = strlen ($cryptkey);
Clear text, the first 10 bits are used to hold the timestamp, verify the validity of the data when decrypting, 10 to 26 bits to save the $KEYB (key B),
Data integrity is validated through this key during decryption
If it is decoded, it will start from the $ckey_length bit, because the ciphertext before the $ckey_length bit to save the dynamic key to ensure that the decryption is correct
$string = $operation = = ' DECODE '? Base64_decode (substr ($string, $ckey _length)):
sprintf ('%010d ', $expiry? $expiry + Time (): 0). substr (MD5 ($string. $keyb), 0). $string;
$string _length = strlen ($string);
$result = ';
$box = Range (0, 255);
$rndkey = Array ();
Create a key book
for ($i = 0; $i <= 255; $i + +) {
$rndkey [$i] = Ord ($cryptkey [$i% $key _length]);
}
Using a fixed algorithm to disrupt the key book, increase the randomness, seems very complex, in fact, will 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;
}
Core Plus decryption section
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;
To derive a key from a key book, or to convert it to a character.
$result. = Chr (ord ($string [$i]) ^ ($box [($box [$a] + $box [$j])% 256]);
}
if ($operation = = ' DECODE ') {
Verify data validation, see the format of unencrypted plaintext
if (substr ($result, 0,) = 0 | | substr ($result, 0)-time () > 0) &&
substr ($result) = = substr (MD5 (substr ($result,). $keyb), 0, 16)) {
Return substr ($result, 26);
} else {
Return ";
}
} else {
Keep the dynamic key in the ciphertext, which is why the same clear text can be decrypted after producing a different cipher.
Because the encrypted ciphertext may be some special characters, the copy process may be lost, so use base64 encoding
Return $KEYC. Str_replace (' = ', ', Base64_encode ($result));
}
}

$string in function Authcode ($string, $operation, $key, $expiry): string, plaintext, or ciphertext; $operation: decode means decryption, other means encryption; $key: Key; $ Expiry: The validity of redaction.

Usage:

Copy Code code as follows:

$str = ' abcdef ';
$key = ' www.helloweba.com ';
Echo Authcode ($str, ' ENCODE ', $key, 0); Encryption
$str = ' 56f4yer1di2wtzwmqsfpps9hwyojnfp2mpc8sohrrxo7bok ';
Echo Authcode ($str, ' DECODE ', $key, 0); Decrypt

2, add the decryption function encrypt ():

Copy Code code as follows:
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: Need to encrypt the decrypted string, $operation: The judge is encrypted or decrypted, E is encrypted, d is decryption; $key: Key.
Usage:

Copy Code code as follows:

$STR = ' abc ';
$key = ' www.helloweba.com ';
$token = Encrypt ($str, ' E ', $key);
Echo ' encrypt: '. Encrypt ($str, ' E ', $key);
Echo ' Decrypt: '. Encrypt ($str, ' D ', $key);

PS: About encryption technology, the site also provides the following encryption tools for your reference to use:

MD5 Online encryption tool: Http://tools.jb51.net/password/CreateMD5Password

Escape Encryption/decryption tool: http://tools.jb51.net/password/escapepwd

Online SHA1 encryption tool: Http://tools.jb51.net/password/sha1encode

short link (short URL) online generation tool: http://tools.jb51.net/password/dwzcreate

short link (short URL) online Restore tool: Http://tools.jb51.net/password/unshorturl

High Intensity password generator: http://tools.jb51.net/password/CreateStrongPassword

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.