#!/usr/bin/perl

# Written By: Satalink
# 	      04 Sep, 2004

$i = 1;
$timer = 60;
alarm($timer);
$enc=1024;
$hostname = "HostName=192.168.1.1";
$username = "UserName=root";
$password = "Password=root";
$securefile = "securefile.dat";
$offset = int(99 + rand(28));
$string = "$offset"."$hostname"."|:|"."$username"."|:|"."$password";

$encmax = length($string);
$encpos = 0;
&ENCRYPT;
$encmax = length($encryption);
$encpos = 0;
&DECRYPT;

print "\n\n";
print "================================================================\n";
print "Encryption = "."$encryption\n";
print "Decryption = "."$decryption\n";
print "================================================================\n";

#$encmax = 255;
#$encpos = 0;
#&ASCIITABLE;

sub ENCRYPT {
	while ($encpos <= $encmax) {
		#new FILLER of #enc
		#############################################
		$randfilldigit = (rand(127) + 128);
		#############################################
		if($encpos < 3) {
		$char = substr($string, $encpos, 1);
		$digit = ord($char);
		$character = chr($digit + 94);
		$encryption = "$encryption"."$character";		
		} else {
		$char = substr($string, $encpos, 1);
		$digit = ord($char);
		$character = chr($digit + $offset);
		$encryption = "$encryption"."$character";
		}
		$encpos = ($encpos + 1);
		}
	open DATFILE, ">$securefile"  or warn "Can\'t open $securefile\n";
		binmode(DATFILE);
		$l = length($encryption);
		print DATFILE pack("a$l", "$encryption\n");
	close(DATFILE);
}

sub DECRYPT {
	$_format = "Z";
	open DATFILE, "$securefile";
		binmode(DATFILE);
		@DATSEC = <DATFILE>;
		foreach $upcln (@DATSEC) {
			print "@DATSEC";
			$l = length($upcln);
			push @SECUREDATA,  unpack("a$l", $upcln);
   		}
	close(DATFILE);
	foreach $encryption(@SECUREDATA) {
		while ($encpos < $encmax) {
			if($encpos < 3) {
			$decryption = "";
			$char = substr($encryption, $encpos, 1);
			$digit = ord($char);
			$character = chr($digit - 94);
			$randoffset = "$randoffset"."$character";		
			} else {
			$char = substr($encryption, $encpos, 1);
			$digit = ord($char);
			$character = chr($digit - $randoffset);
			$decryption = "$decryption"."$character";
			}
			$encpos = ($encpos + 1);
		}
	}
}

sub ASCIITABLE {
	while ($encpos < $encmax) {
		$character = chr($encpos);
		$encryption = "$encryption"."$character";
		print "\t$character\t$encpos\n";
		$encpos = ($encpos + 1);
	}
}

