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/