PHP reversible encryption and decryption algorithm program code _ PHP Tutorial

Source: Internet
Author: User
PHP reversible encryption and decryption algorithm program code. Our commonly used md5 algorithm is irreversible theoretically, but there is a powerful way to calculate the md5 algorithm, but it only takes time based on the complexity, however, sometimes our common md5 algorithms are irreversible in theory, but there is a powerful way to calculate the md5, but the time needed for complexity is different, but sometimes we want our data to be encrypted and transmitted before it can be decrypted. I will provide you with a function.

For most password encryption, we can use md5, sha1, and other methods. This can effectively prevent data leaks, but these methods only apply to data encryption that does not require restoration.

For information to be restored, a reversible encryption and decryption algorithm is required.

The following PHP functions implement encryption and decryption:

The encryption algorithm is as follows:

The code is as follows:
Function encrypt ($ data, $ key)
{
$ Key = md5 ($ key );
$ X = 0;
$ Len = strlen ($ data );
$ L = strlen ($ key );
For ($ I = 0; $ I <$ len; $ I ++)
{
If ($ x = $ l)
{
$ X = 0;
}
$ Char. = $ key {$ x };
$ X ++;
}
For ($ I = 0; $ I <$ len; $ I ++)
{
$ Str. = chr (ord ($ data {$ I}) + (ord ($ char {$ I}) % 256 );
}
Return base64_encode ($ str );
}

The decryption algorithm is as follows:

The code is as follows:
Function decrypt ($ data, $ key)
{
$ Key = md5 ($ key );
$ X = 0;
$ Data = base64_decode ($ data );
$ Len = strlen ($ data );
$ L = strlen ($ key );
For ($ I = 0; $ I <$ len; $ I ++)
{
If ($ x = $ l)
{
$ X = 0;
}
$ Char. = substr ($ key, $ x, 1 );
$ X ++;
}
For ($ I = 0; $ I <$ len; $ I ++)
{
If (ord (substr ($ data, $ I, 1) <ord (substr ($ char, $ I, 1 )))
{
$ Str. = chr (ord (substr ($ data, $ I, 1) + 256)-ord (substr ($ char, $ I, 1 )));
}
Else
{
$ Str. = chr (ord (substr ($ data, $ I, 1)-ord (substr ($ char, $ I, 1 )));
}
}
Return $ str;
}

An encryption key ($ key) is required for the above encryption and decryption process ).

The code is as follows:

$ Data = 'php encryption and decryption algorithmic '; // encrypted information
$ Key = '000000'; // key
$ Encrypt = encrypt ($ data, $ key );
$ Decrypt = decrypt ($ encrypt, $ key );
Echo $ encrypt, "n", $ decrypt;

The output is similar to the following:

GniCSOzZG + HnS9zcFea7SefNGhXF
PHP encryption and decryption algorithm

From the above results, we can see that this is a set of reversible encryption and decryption algorithms that can be used to encrypt part of the data to be restored.

...

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.