4KB sha1 version arch-tag: symetric cryptography using sha1 For peoples who find this using google: homepage of this "project" is on http://www.karlin.mff.cuni.cz/~hkmaly/php (including documentation) */ function bytexor($a,$b,$l) { $c=""; for($i=0;$i<$l;$i++) { $c.=$a{$i}^$b{$i}; } return($c); } function binsha1($val) { return(pack("H*",sha1($val))); } function decrypt_sha1($msg,$heslo,$time) { $key=$heslo.$time;$sifra=""; $key1=binsha1($key); while($msg) { $m=substr($msg,0,20); $msg=substr($msg,20); echo "."; $sifra.=$m=bytexor($m,$key1,20); $key1=binsha1($key.$key1.$m); } echo "\n"; return($sifra); } function crypt_sha1($msg,$heslo,$time) { $key=$heslo.$time;$sifra=""; $key1=binsha1($key); while($msg) { $m=substr($msg,0,20); $msg=substr($msg,20); echo "."; $sifra.=bytexor($m,$key1,20); $key1=binsha1($key.$key1.$m); } echo "\n"; return($sifra); } if(!($key=$_SERVER['argv'][2])) exit("Usage: ".$_SERVER['argv'][0]." mode password file ...\n\tAvailable modes: c - crypt, d - decrypt\n"); $mode=$_SERVER['argv'][1]; for($i=3;$i<$_SERVER['argc'];$i++) { $name=$_SERVER['argv'][$i]; $fdi=fopen($name,"r"); if($name=="php://stdin") $name="stdin"; if($mode=='c') { $fdo=fopen($name.".md5crypted","w"); $time=time(); $ver=2; fwrite($fdo,$time."-sha1-".$ver."\n"); $fce="crypt_sha1"; } else if($mode=='d') { $fdo=fopen($name.".plain","w"); $time=trim(fgets($fdi,100)); if($ver=strrchr($time,"-")) { $ver=0-$ver; $time=explode("-",$time); if($time['1']!='sha1') echo "File $name not crypted using sha1\n"; $time=$time['0']; } $fce="decrypt_sha1"; } else exit("Usage: ".$_SERVER['argv'][0]." mode password file ...\n\tAvailable modes: c - crypt, d - decrypt\n"); while(!feof($fdi)) { $buf=fread($fdi,4096); fwrite($fdo,$fce($buf,$key,$time)); if($ver) $time++; } fclose($fdi); fclose($fdo); } ?>