if($EncMethod $= "") $EncMethod = 1;
$EncVersion = "1.0";
function FileObject::enc_openForRead(%this,%file,%key) {
	if(!isFile(%file)) {
		error("ERROR: Cannot open non-existant file for decryption");
		return;
	}
	if(%key $= "") {
		error("ERROR: Cannot open encrypted file without password");
		return;
	}
	%this.enc_key = %key;
	%this.openForRead(%file);
	%header = trim(%this.readLine());
	if(getSubStr(%header, 0, 3) !$= "ENC") {
		error("ERROR: Cannot read file (Bad header)");
		%this.close();
		return;
	}
	if(getSubStr(%header, 3, strLen($EncVersion)) !$= $EncVersion) {
		error("ERROR: Cannot read file (Version mismatch; expected " @ $EncVersion @ ", got " @ getSubStr(%header, 3, strLen(%header)-4) @ ")");
		%this.close();
		return;
	}
	%method          = getSubStr(%header, 3+strLen($EncVersion), 1);
	%this.enc_method = %method;
}
function FileObject::enc_openForWrite(%this, %file, %key, %method) {
	if(%method $= "") %method = $EncMethod;
	if(%key $= "") {
		error("ERROR: Cannot write an encrypted file without password");
		return;
	}
	%this.enc_key    = %key;
	%this.enc_method = %method;
	%this.openForWrite(%file);
	%this.writeLine("ENC" @ $EncVersion @ %method);
}
function FileObject::enc_readLine(%this) {
	%strLine = %this.readLine();
	return decryptData(%strLine,%this.enc_key,%this.enc_method);
}
function FileObject::enc_peekLine(%this) {
	%strLine = %this.peekLine();
	return decryptData(%strLine,%this.enc_key,%this.enc_method);
}
function FileObject::enc_writeLine(%this,%line) {
	%this.writeLine(encryptData(%line,%this.enc_key,%this.enc_method));
}
function FileObject::enc_close(%this) {
	%this.enc_key = "";
	%this.close();
}
function getHex(%num) {
	%tbl      = "0123456789ABCDEF";
	%lower    = getSubStr(%tbl,mFloor(%num / 16),1);
	%higher   = getSubStr(%tbl,mFloor(%num % 16),1);
	%ret      = %lower@%higher;
	return %ret;
}
function getDec(%hex) {
	if(stripChars(%hex, "0123456789ABCDEF") !$= "") return 0;
	eval("%result=0x"@%hex@";");
	return %result;
}
function encryptData(%string, %key, %method) {
	if(%method $= "") %method = 1;
	for(%i=0;%i<strLen(%string);%i++) {
		%asc = strAsc(getSubStr(%string,%i,1))+strAsc(getSubStr(%key,%keyPos,1));
		%keyPos++;
		if(%keyPos >= strLen(%key)) %keyPos = 0;
		%newStr = %newStr SPC %asc;
	}
	%newStr = trim(%newStr);
	%rnd = getStringCRC(%key);
	setRandomSeed(%rnd);
	for(%i=0;%i<getWordCount(%newStr);%i++) {
		%num = getWord(%newStr,%i);
		%rnd = %rnd+getRandom(0,1000);
		setRandomSeed(%rnd);
		if(%method == 1)
			%newStr = setWord(%newStr,%i,getHex(%num+getRandom(0,100))@":");
		else
			%newStr = setWord(%newStr,%i,toAsciiTable(%num+getRandom(0,100)));
	}
	if(%method == 1) %ret = getSubStr(stripChars(%newStr," "),0,strLen(stripChars(%newStr," "))-1);
	else %ret = getSubStr(stripChars(%newStr," "),0,strLen(stripChars(%newStr," ")));

	return %ret;
}
function decryptData(%string, %key, %method) {
	if(%method $= "") %method = 1;
	%rnd = getStringCRC(%key);
	setRandomSeed(%rnd);
	if(%method == 1) {
		%string = strReplace(%string,":"," ");
		for(%i=0;%i<getWordCount(%string);%i++) {
			%bin = getWord(%string,%i);
			%rnd = %rnd+getRandom(0,1000);
			setRandomSeed(%rnd);
			%oldStr = %oldStr SPC getDec(%bin)-getRandom(0,100);
		}
	} else {
		for(%i=0;%i<strLen(%string);%i++) {
			%bin = getSubStr(%string, %i, 1);
			%rnd = %rnd+getRandom(0,1000);
			setRandomSeed(%rnd);
			%oldStr = %oldStr SPC getDec(fromAsciiTableToHex(%bin))-getRandom(0,100);
		}
	}
	%oldStr = trim(%oldStr);
	for(%i=0;%i<getWordCount(%oldStr);%i++) {
		%asc = strFormat("%c",mAbs(getWord(%oldStr,%i)-strAsc(getSubStr(%key,%keyPos,1))));
		%keyPos++;
		if(%keyPos >= strLen(%key)) %keyPos = 0;
		%newStr = %newStr @ %asc;
	}
	return trim(%newStr);
}
function toAsciiTable(%num) {
	return getSubStr($ASCIITable_Enc, %num % (strLen($ASCIITable_Enc)-1), 1);
}
function fromAsciiTableToHex(%chr) {
	%num = strPos($ASCIITable_Enc, %chr);
	return getHex(%num);
}
function strAsc(%n) {
	// wrapper
	return strPos($ASCIITable_Enc,%n);
}
function strFormat(%a,%chr) {
	// wrapper
	return getSubStr($ASCIITable_Enc,%chr,1);
}
$ASCIITable_Enc=
        "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F"@
	"\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F"@
	"\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2A\x2B\x2C\x2D\x2E\x2F"@
	"\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3A\x3B\x3C\x3D\x3E\x3F"@
	"\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4A\x4B\x4C\x4D\x4E\x4F"@
	"\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5A\x5B\x5C\x5D\x5E\x5F"@
	"\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6A\x6B\x6C\x6D\x6E\x6F"@
	"\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7A\x7B\x7C\x7D\x7E\x7F"@
	"\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F"@
	"\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F"@
	"\xA0\xA1\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xAA\xAB\xAC\xAD\xAE\xAF"@
	"\xB0\xB1\xB2\xB3\xB4\xB5\xB6\xB7\xB8\xB9\xBA\xBB\xBC\xBD\xBE\xBF"@
	"\xC0\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xCA\xCB\xCC\xCD\xCE\xCF"@
	"\xD0\xD1\xD2\xD3\xD4\xD5\xD6\xD7\xD8\xD9\xDA\xDB\xDC\xDD\xDE\xDF"@
	"\xE0\xE1\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xEA\xEB\xEC\xED\xEE\xEF"@
	"\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\xFA\xFB\xFC\xFD\xFE\xFF";