PHP uses XOR or XOR to encrypt and decrypt files

Source: Internet
Author: User
Tags decrypt

PHP uses XOR (XOR) to encrypt/decrypt files

Principle: Each byte of the file with the key for the bitwise XOR or operation (XOR), decryption and then perform an exclusive OR operation.

The code is as follows:

01.<?php
02.
$source = ' test.jpg ';
$encrypt _file = ' test_enc.jpg ';
$decrypt _file = ' test_dec.jpg ';
$key = ' d89475d32ea8bbe933dbd299599eea3e ';
07.
08.echo ' <p>source:</p> ';
09.echo ' ';
10.echo ' 11.
12.file_encrypt ($source, $encrypt _file, $key); Encrypt
13.
14.echo ' <p>encrypt file:</p> ';
15.echo ' ";
16.echo ' 17.
18.file_encrypt ($encrypt _file, $decrypt _file, $key); Decrypt
19.
20.echo ' <p>decrypt file:</p> ';
21.echo ' ";
22.
23./** file encryption, using the key and the original text or generate ciphertext, decryption and then perform a different or
24.* @param String $source the file to encrypt or decrypt
25.* @param String $dest encrypted or decrypted file
26.* @param String $key key
27.*/
28.function File_encrypt ($source, $dest, $key) {
if (file_exists ($source)) {
30.
$content = '; The string after processing
$keylen = strlen ($key); Key length
$index = 0;
34.
$fp = fopen ($source, ' RB ');
36.
A. while (!feof ($fp)) {
$tmp = Fread ($fp, 1);
$content. = $tmp ^ substr ($key, $index% $keylen, 1);
$index + +;
41.}
42.
Fclose ($FP);
44.
File_put_contents return ($dest, $content, true);
46.
}else{.
return false;
49.}
50.}
51.
52.?>

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/PHP/

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.