#!/usr/bin/php $i % 0x0100, 'permutation' => $j, 'stream' => $stream ); return($output); } function bCypher($state,$input) { $length = strlen($input); $streamdata = genStream($state,$length,0x00,0x00); for($i=0x00; $i<$length; $i++) { // The overhead of an array seems to be justified in the time saved // not processing ord() so many times: see genStream(); #$output .= chr(ord($streamdata['stream']{$i}) ^ ord($input{$i})); $output .= chr($streamdata['stream'][$i] ^ ord($input{$i})); } return($output); } function bsEnDeCrypt($rawkey,$rawiv,$nMixes,$input) { $key = keyBuild($rawkey,$rawiv); $output = bCypher(stateGen($key,$nMixes),$input); return($output); } // DOCUMENT FUNCTIONS // function decryptDoc($fn,$rawkey) { $file = fopen($fn,'r'); $data = base64_decode(fread($file,filesize($fn))); $nMixes = ord($data{0}) % 64; $ivlen = ord($data{1}); $output = $ivlen; $rawiv = substr($data,2,$ivlen); $input = substr($data,2+$ivlen,filesize($fn)); $output = bsEnDeCrypt($rawkey,$rawiv,$nMixes,$input); fclose($file); echo($output); } function encryptDoc($fn,$rawkey,$ivlen,$nMixes) { $file = fopen($fn,'r'); $input = fread($file,filesize($fn)); fclose($file); $rawiv = ivRaw(ivGen($ivlen)); $cypher = bsEnDeCrypt($rawkey,$rawiv,$nMixes,$input); $output = base64_encode(chr($nMixes).chr($ivlen).$rawiv.$cypher); echo($output); } ?>