ChimpCipher() - Easy XOR encryption for strings

Monkey Forums/Monkey Code/ChimpCipher() - Easy XOR encryption for strings

Nobuyuki(Posted 2013) [#1]
Here's a simple cipher to protect cursory examination of strings inside your SaveState() and other plain text files. Manipulation of values by the end-user would require knowledge of the key, which can be found through statistical analysis of repeating (or known) characters in the cyphertext of at least as many bytes of data there are in the key. (so, the longer your key is, the better!) With many kinds of data, this method provides protection against trivial attempts to tamper with your data.

I've only tested keys and ciphertext in 7-bit ascii -- unicode characters might get turned into mojibake on cipher, depending on the target. Plain-old-text, JSON and XML should be okay.

Function ChimpCipher:String(input:String, key:String)
	Local out:int[input.Length] 'output string chars
	Local k:Int 'Key iterator
	
	For Local i:Int = 0 Until input.Length
		Local ochr:Int = input[i]
		If k >= key.Length Then k = 0  'cycle
		
		out[i] = ochr ~ key[k]
		k += 1
	Next
	
	Return String.FromChars(out)
End Function


'''''''Example usage below:
Const key:String = "test key"
Const ctext:String = "output block.~nOUTPUT BLOCK ~~ [] {} <> :|!;~n	~qOutput Block~q~n"

Function Main:Int()
	Print ctext
	Print ChimpCipher(ctext, key) + "~n" 'ciphertext
	Print ChimpCipher(ChimpCipher(ctext, key), key)  'output, to prove 1:1
End Function