Page 1 of 1

Simple & Silly - Send "encrypted" data to PHP

Posted: Mon Dec 20, 2010 9:43 pm
by Nituvious
I found myself wondering if I could send data to my a website and still keep it somewhat secure without a lot of risk. I was, and it turned out pretty well for me(DISCLAIMER: I am a newbie). It was a great learning experience! :)
I think I have commented everything important. Feel free to play with the example and the high score stuff, I purge it every so often.

This is a simply just character rotation, it really shouldn't be called an encryption at all I think, but I don't know what it would be called.

Example of use(I know this is a TERRIBLE example, but it was just a quick write up):
Download
The result:
http://anotherprophecy.com/system/scrip ... hscore.txt

SimpleEncyption.pbi:

Code: Select all

InitNetwork()
Procedure.s GetUniqueID(lpURL.s)
	header$ = GetHTTPHeader(lpURL.s)
	string$ = StringField(header$,2,Chr(34)) ; Your site may be different, you will probably need to change this.
	;Debug string$
	ProcedureReturn string$
EndProcedure


Procedure.s EncryptURLData(lpString.s,encryptKey)
	Structure eData
		oldchar.s
		newchar.s
	EndStructure

	setAlphaNumeric$ = "A B a b c X Y Z d 6 7 8 e f R S T g h i O P Q j k l U V W n m n L 0 1 2 M N o p q I J K r s t F G H u v w C D E x y z 3 4 5 9 . -" ; I do not advise including < > / \ ; , characters
	Dim Characters.eData(Len(ReplaceString(setAlphaNumeric$," ","",#PB_String_NoCase))) ; -- Size of  each character in 

	Characters(0)\oldchar = " "
	Characters(0)\newchar = ","+Str(encryptKey*2) ; -- The "encryption" I am using is just replacing the letter with the result of multiplying a unique ID by 2.

	For x = 1 To CountString(setAlphaNumeric$," ")+1 ; -- Places each letter in setAlphaNumeric$ inside of Characters() array.
		Characters(x)\oldchar = StringField(setAlphaNumeric$,x," ")
		Characters(x)\newchar = ","+Str(x+(encryptKey*2))
	Next x ; <-- I am not really sure what the variable here is doing, I hopefully can get an explanation on the forum.
	
	q = 1 : While #True ; -- I am just looping the procedure until each getCharacter matches characters(w)\oldchar
		; -- the loop ends when the length of lpString.s has been exceeded. Since it only increases when the letter
		; -- has been found this should be very accurate.
		; -- I think this is a form of bubblesort? (I didn't know the term until AFTER writing this though)
		getCharacter.s = Mid(lpString.s,q,1)
		If getCharacter.s = Characters(w)\oldchar
			EncryptedString.s + Characters(w)\newchar
			If q >= Len(lpString.s) : Break : Else : w = 0 : q + 1 : EndIf
		Else
			If w <= ArraySize(Characters()) - 1 : w + 1 : Else : w = 0 : EndIf
		EndIf
	Wend
	
	ProcedureReturn EncryptedString.s ; -- Return our new "encrypted" string.

EndProcedure

Procedure SendData(URL.s,lpString.s)
	getKey$ = GetUniqueID(URL.s) ; Create the key
	encryptedData$ = EncryptURLData(lpString.s,200)
	Delay(2000)
	foo.s = GetHTTPHeader(URL.s+"?key="+getKey$+"&in="+encryptedData$)
EndProcedure
Decryptor.php:

Code: Select all

<?PHP	
	$getData = $_GET["in"]; // encrypted information
	$getKey = $_GET["key"]; // unique Key reply

	/*
		Function Name:
			_simple_ DecryptURLData()
		Purpose:
			Take encrypted $_GET[] string and decrypt it with specified encryption key.
		Last updated:
			12/13/2010
	*/
	function DecryptURLData($fooData,$decryptKey) {
		// Freakin'... After typing this out 3 minutes later I stumble on str_split(), d'oh!
		//$arrayCharactersOld = array(" ", "A", "B", "a", "b", "c", "X", "Y", "Z", "d", "6",
		//						"7", "8", "e", "f", "R", "S", "T", "g", "h", "i",
		//						"O", "P", "Q", "j", "k", "l", "U", "V", "W", "n", 
		//						"m", "n", "L", "0", "1", "2", "M", "N", "o", "p",
		//						"q", "I", "J", "K", "r", "s", "t", "F", "G", "H",
		//						"u", "v", "w", "C", "D", "E", "x", "y", "z", "3", 
		//						"4", "5", "9", ".");
		
		$arrayCharactersOld = str_split(" ABabcXYZd678efRSTghiOPQjklUVWnmnL012MNopqIJKrstFGHuvwCDExyz3459.-"); // This should echo our program's version
		// Create new array to hold the "encrypted" letters
		$arrayCharactersNew = array("");
		$arrayCharactersNew[0] = $decryptKey*2;	// Space; filled in here so the for loop
												// can get to the meat and potatoes(and beer).
		for ($x = 1; $x <= count($arrayCharactersOld); $x++) {
			$arrayCharactersNew[$x]	= ($x + ($decryptKey*2));
		}
		
		// Decrypt our string, first we need to find "," which will where a letter begins.
		$arrayData = explode(",",$fooData); // Explode works very well in this case.

		while ($checkA <= count($arrayData)) {
			if ($arrayData[$checkA] == $arrayCharactersNew[$checkB]) {
				$stringDecrypted .= $arrayCharactersOld[$checkB];
				$checkB = 0; // set second "loop" back to zero so we don't have a continue until the end of time
				if ($checkA >= count($arrayData)) {
					// break the while loop since we've matched and decrypted everything
					break;
				}
				else {
				$checkA++; // when a match has been found we increase our first loop, if you can call it that.
				}
			}
			else {
				if ($checkB >= count($arrayCharactersNew)) {
					$checkB = 0; // set second "loop" back to zero so we don't have a continue until the end of time
				}
				else {
					$checkB++; // if no match has been found then we reset the second "loop". We do this until we eventually make a match.
				}
			}
		}
		return $stringDecrypted;
	}

	function CreateUniqueKey() {
		// This function checks directory for an existing key
		// if it finds one, it deletes it, otherwise it will create a new one.
		$uniqueID = uniqid();
		if (file_exists("keys/$uniqueID.key") == TRUE) {
			unlink("keys/$uniqueID.key");
		} 
		else {
			$uniqueFileHandle = fopen("keys/$uniqueID.key",'w') or die("");
			fclose($uniqueFileHandle);
			return $uniqueID;
		}
	}

	function HandleUniqueKey($getKey,$getData) {
		// This function checks if UniqueKey matches any file in directory
		// if it does then it deletes it and then decrypts our data and saves to a file
		if (file_exists("keys/$getKey.key") == TRUE) {
			unlink("keys/$getKey.key");

			$fileHandle = fopen("piggy_highscore.txt",'a+') or die("");
			fwrite($fileHandle,DecryptURLData($getData,200). "\n");
			fclose($fileHandle);
		}
		else {
			echo "Error - Bad unique key";
		}
	}
	
	function HighScore($getData,$getKey) {
		// combine functions from above
		// This function checks if either $_GET is empty
		// Should change this to a check for example, $_GET["createnewkey"];
		// that way anyone who accidently browse to this file won't create a key that will not be used
		if (($getData == "") ||($getKey == "")) {
			$uniqueID = CreateUniqueKey();
			Header(":\"$uniqueID\"");
		}
		else {
			HandleUniqueKey($getKey,$getData);
		}
	}

	HighScore($getData,$getKey);

?>
[edit] fixed broken link

Re: Simple & Silly - Send "encrypted" data to PHP

Posted: Tue Dec 21, 2010 12:20 am
by IdeasVacuum
Nice work.
Freakin'... After typing this out 3 minutes later I stumble on str_split(), d'oh!
. Happens to me all the time!

Re: Simple & Silly - Send "encrypted" data to PHP

Posted: Sun Feb 06, 2011 11:41 pm
by Nituvious
Updated the download link, it's no longer broken now. :?