Php encryption algorithm implementation reversible encryption algorithm and decryption sharing _ PHP Tutorial

Source: Internet
Author: User
Php encryption algorithm implements reversible encryption algorithm and decryption and sharing. The encryption algorithm is as follows: Copy the code: functionencrypt ($ data, $ key) {$ keymd5 ($ key); $ x0; $ lenstrlen ($ data ); $ lstrlen ($ key); for ($ i0; $ I $ len; $ I ++) {if ($ x encryption algorithm:

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:

The code is as follows:


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.

The authorization 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 =...

Related Article

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.