Implementation of PHP encryption algorithm reversible encryption algorithm and decryption sharing _php tutorial

Source: Internet
Author: User
Tags ord
The encryption algorithm is as follows:
Copy CodeThe 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:
Copy the Code code 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 ($data, $i, 1) + substr)-Ord (substr ($char, $i, 1)));
}
Else
{
$str. = Chr (Ord (substr ($data, $i, 1))-Ord (substr ($char, $i, 1)));
}
}
return $str;
}

The process of encrypting and decrypting above requires the use of an encryption key (i.e., parameter $key).

Copy the Code code as follows:
$data = ' PHP encryption and decryption algorithm '; Information that is encrypted
$key = ' 123 '; Secret key
$encrypt = Encrypt ($data, $key);
$decrypt = Decrypt ($encrypt, $key);
echo $encrypt, "\ n", $decrypt;

The above output will look similar to the following results:
Copy the Code code as follows:
Gnicsozzg+hns9zcfea7sefnghxf
PHP Encryption and decryption algorithm

As can be seen from the above results, this is a set of reversible encryption and decryption algorithms that can be used to encrypt some data that needs to be restored.

http://www.bkjia.com/PHPjc/726031.html www.bkjia.com true http://www.bkjia.com/PHPjc/726031.html techarticle The encryption algorithm is as follows: The copy code 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.