<?php
	
	abstract class Criptar {
		
		private static function getChave(){
			return 'k#L@p2ZoeAs)*!%@!#dygxg&$FDJH*saR&jaOE9&"#qA$JDHD4\'_**'.chr(168).'%*&*(ys';
		} 
		
		public static function Cripto($Action, $Src){
			if(!$Src){
				return null;
				exit;
			}
			$Key = Criptar::getChave();
			$dest = '';
			$KeyLen = strlen($Key) - 1;
			$KeyPos = -1;
			$Range = 255;
			
			if($Action == strtoupper('E')){
				$offset = rand(0, $Range);
				$dest = sprintf('%02X', $offset);
				for($SrcPos = 0; $SrcPos < strlen($Src); $SrcPos++){
					$SrcAsc = (ord(substr($Src, $SrcPos, 1)) + $offset) % 255;
					if($KeyPos < $KeyLen){
						$KeyPos = $KeyPos + 1;
					}else{
						$KeyPos = 0;
					}
					$SrcAsc = $SrcAsc ^ ord(substr($Key, $KeyPos, 1));
					$dest .= sprintf('%02X', $SrcAsc);
					$offset = $SrcAsc;
				}
			}
			if($Action == strtoupper('D')){
				$offset = hexdec(substr($Src, 0, 2));
				$SrcPos = 2;
				do{
					$SrcAsc = hexdec(substr($Src, $SrcPos, 2));
					if($KeyPos < $KeyLen){
						$KeyPos++;
					}else{
						$KeyPos = 0;
					}
					$TmpSrcAsc = $SrcAsc ^ ord(substr($Key, $KeyPos, 1)); 
					if($TmpSrcAsc <= $offset){
						$TmpSrcAsc = 255 + $TmpSrcAsc - $offset;
					}else{
						$TmpSrcAsc = $TmpSrcAsc - $offset;
					}
					$dest .= chr($TmpSrcAsc);
					$offset = $SrcAsc;
					$SrcPos = $SrcPos + 2;
				}while($SrcPos < strlen($Src));
			}
			return $dest;
		}	
		
	}
	
?>