Encryption script is not working
Posted: Sun Oct 19, 2008 3:40 pm
I have several pdf files, which need to be encrypted. That is why I downloaded a php class at phpclasses.org which could encrypt thing using AES. I made a script (the one below) which should (but doesn't) encypt all files in a direcotry. When I run the script, I end up with pdf files which will not work after decrypting and are all 30 bytes. Somebody an idea what I did wrong.
And here is the file which decrypts it:
Code: Select all
<?php
ignore_user_abort();
set_time_limit(0);
// The AES class
require_once('AES.class.php');
$key256 = // I am not telling you that... ;)
$Cipher = new AES(AES::AES256);
$fileCount=0;
$filePath=$PATH.'/usr/local/apache2/htdocs/vhc/analyses/';
$dir = opendir($filePath);
while ($file = readdir($dir)) { // This while loop loops through all files in the loop and $file is the filename
if ($file != 'AES.class.php' && $file != 'encrypt.php') {
// Open the file and read it contents
$fh = fopen($file, rb);
$length = filesize($file);
$data = fread($fh, $length);
fclose($fh);
// Ecnrypt the data
$base64data = base64_encode($data);
$hexeddata = $Cipher->stringToHex($base64data);
$encrypteddata = $Cipher->encrypt($hexeddata, $key256);
// Write it to the file
$fh2 = fopen($file, wb);
fwrite($fh2, $encrypteddata);
fclose($fh2);
}
}
?> Code: Select all
<?php
// Check if the person is logged-in (if the cookie is empty, he is not logged in)
if(empty($_COOKIE)) { die("U moet eerst inloggen!"); }
// Aganin the AES class
require_once("AES.class.php");
$Cipher = new AES(AES::AES256);
// Process the POST data to file path
$art=ereg_replace("[^A-Za-z0-9]", "", $_POST['batchnummer']);
$pdf = 'http://127.0.0.1/vhc/analyses/'.$art.'.pdf';
$key256 = // Secret ;)
// Read the file contents
$handle = fopen($pdf, "rb");
$contents = '';
while (!feof($handle)) {
$encrypteddata .= fread($handle, 8192);
}
fclose($handle);
// Decrypt the data
$hexeddata = $Cipher->decrypt($encryteddata, $key256);
// Make the Hex a string again and base64 decode it.
$base64data = $Cipher->hexToString($hexeddata);
$data = base64_decode($base64data);
// Offer the data as a downloadable pdf
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="analyse_certificaat.pdf"');
echo $data;
// Exit
exit(0);
?>