Need Help with Encrypting File Uploads

Hi all,

I’m using a PHP script to submit the contents of a form and upload a support document to a Windows 2003 server using IIS6. The documents may contain some confidential information, and I need a way to encrypt (not encode) the documents on upload and then decrypt them when the user logs in and accesses the info. The documents may be in one of several formats (pdf, word, excel, etc.)

I have searched the web and cannot find a way that actually works. Any help from experienced PHP programmers would be a tremendous help!

I Have just found this on siamnet: hope this is of some use

<?php function get_rnd_iv ($iv_len) { $iv = ''; while ($iv_len-- > 0) { $iv .= chr(mt_rand() & 0xff); } return $iv; } function md5_encrypt ($plain_text, $password, $iv_len = 16) { $plain_text .= "\x13"; $n = strlen($plain_text); if ($n % 16) $plain_text .= str_repeat("\0", 16 - ($n % 16)); $i = 0; $enc_text = get_rnd_iv($iv_len); $iv = substr($password ^ $enc_text, 0, 512); while ($i < $n) { $block = substr($plain_text, $i, 16) ^ pack('H*', md5($iv)); $enc_text .= $block; $iv = substr($block . $iv, 0, 512) ^ $password; $i += 16; } return base64_encode($enc_text); } function md5_decrypt ($enc_text, $password, $iv_len = 16) { $enc_text = base64_decode($enc_text); $n = strlen($enc_text); $i = $iv_len; $plain_text = ''; $iv = substr($password ^ substr($enc_text, 0, $iv_len), 0, 512); while ($i < $n) { $block = substr($enc_text, $i, 16); $plain_text .= $block ^ pack('H*', md5($iv)); $iv = substr($block . $iv, 0, 512) ^ $password; $i += 16; } return preg_replace('/\\x13\\x00*$/', '', $plain_text); } // split and encrypt file #$file = "test.txt"; $file = "u/robot.png"; $filesize = filesize($file); $salt = "Th1s Is a gr3aT sALT!!!"; $numberOfFiles = 3; $handle = fopen($file, "r"); $sizePerSplitFile = ceil($filesize / $numberOfFiles); exec("split --bytes={$sizePerSplitFile} {$file} {$file}-"); $a_splits = glob("{$file}-*"); $encFileContents = ""; foreach ($a_splits as $k=>$files) { $encFileContents = md5_encrypt(file_get_contents($files), $salt); file_put_contents($files, $encFileContents); } // decrypt and un-split file $decryptedFile = "e/robot.png"; $encFileContents = ""; foreach ($a_splits as $k=>$files) { $encFileContents .= md5_decrypt(file_get_contents($files), $salt); } file_put_contents($decryptedFile, $encFileContents);
Sponsor our Newsletter | Privacy Policy | Terms of Service