[PHP] Simple cryptographic application of XOR or operation of [bit conversion accumulation]

Source: Internet
Author: User

The symbol of XOR is ^. Bitwise XOR, the equivalent long Binary mode performs a bitwise XOR OR operation on each bit of a bitwise or binary number. The result of the operation is that if a bit is different then the bit is 1, otherwise the bit is the 0.xor operation of the inverse is itself, that is, two times or the same number of the final result is unchanged, i.e. (a XOR b) xor B = A. The XOR operation can be used for simple encryption, such as I want to say to my mm 1314520, but afraid of others know, so the two sides agreed to take my birthday 19880516 as a key. 1314520 xor 19880516 = 20665500, I will tell the 20665500 mm. MM again calculates the value of 20665500 xor 19880516 and gets 1314520, so she understands my intentions. The same bit differs by 1 and the same is 0. 0010111100 (^ or XOR)----------------11001 results x <-x # yy <-x @ yx <-x @ y Executes the first sentence after x becomes x # Y. Then the second sentence is actually y <-x # y @ y, because # and @ are inverse each other, then y becomes the original x. In the third sentence, X is actually assigned to (x # y) @ x, and if the # operation has an Exchange law, then x becomes the original y when the value is assigned. The result of these three sentences is that the positions of X and Y are interchanged. Using PHP with this principle allows for simple and efficient encryption
function Mycrypt ($STR, $key) {$slen = strlen ($str); $klen = strlen ($key); $cipher = "; for ($i =0; $i < $slen; $i = $i + $kle N) {$cipher. = substr ($str, $i, $klen) ^ $key;} return $cipher;}

The receiver implements simple decryption

function Myuncrypt ($STR, $key) {$slen = strlen ($str); $klen = strlen ($key); $plain = "; for ($i =0; $i < $slen; $i = $i + $kl EN) {$plain. = $key ^substr ($str, $i, $klen);} return $plain;}

[PHP] Simple cryptographic application of XOR or operation of [bit conversion accumulation]

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.