#!/usr/bin/perl

# Written By: Satalink
# 	      09 Sep, 2004

$securefile = "securefile.dat";

$offset = int(100 + rand(27)); 
@UNSECURESTRINGS = <"$offset hostname1|:|user1|:|pass1|:|rootpass1" "$offset hostname2|:|user2|:|pass2|:|rootpass2" "$offset hostname3|:|user3|:|pass3|:|rootpass3" "$offset hostname4|:|user4|:|pass4|:|rootpass4">;

#OPERATION
&READENCRYPTFILE if -f $securefile;
foreach $string(@UNSECURESTRINGS) {
	chomp($string);
	&ENCRYPT;
}

&WRITEENCRYPTFILE;

print "Delete ${securefile.dat}?  >";
$DELETEME = <>;
if ($DELETEME =~ /y|Y/) {
	unlink "$securefile" or warn "Can't delete $securefile\n" if -f $securefile;
}

sub ENCRYPT {
	$encmax = length($string);
	$encpos = 0;
	$encryption = "";
	$offset = int(100 + rand(27));
	while ($encpos <= $encmax) {
		if($encpos < 3) {
		$char = substr($offset, $encpos, 1);
#		$char = substr($string, $encpos, 1);
		$digit = ord($char);
		$character = chr($digit + 128);
		$encryption = "$encryption"."$character";		
		} else {
		$char = substr($string, $encpos, 1);
		$digit = ord($char);
		$character = chr($digit + $offset);
		$encryption = "$encryption"."$character";
		}
		$encpos = ($encpos + 1);
	}
	$l = length($encryption);
	push @SECUREDATA, "$encryption\n";
}

sub DECRYPT {
	$encmax = length($encryption);
	$encpos = 0;
	$decryption = "";
	while ($encpos < $encmax) {
		if($encpos < 3) {
		
		$char = substr($encryption, $encpos, 1);
		$digit = ord($char);
		$character = chr($digit - 128);
		$randoffset = "$randoffset"."$character";
		} else {
		$char = substr($encryption, $encpos, 1);
		$digit = ord($char);
		$character = chr($digit - $randoffset);
		
		}
		$decryption = "$decryption"."$character";
		$encpos = ($encpos + 1);
	}
	push @UNSECURESTRINGS, "$decryption\n";
}

sub READENCRYPTFILE {
	@UNSECURESTRINGS = ();
	open DATFILE, "$securefile";
		@SECURESTRINGS = <DATFILE>;
	close(DATFILE);
	foreach $encryption(@SECURESTRINGS) {
		chomp($encryption);
		$randoffset = "";
		&DECRYPT;	
	}
}

sub WRITEENCRYPTFILE {
	unlink "$securefile" or warn "Can't delete $securefile $!\n" if -f $securefile;
		print "@SECUREDATA";
		open DATFILE, ">$securefile"  or warn "Can\'t open $securefile\n";
			print DATFILE @SECUREDATA;
		close(DATFILE);
}


