Code archives/Miscellaneous/XOR string encrypt

This code has been declared by its author to be Public Domain code.

Download source code

XOR string encrypt by Shagwana2013
A simple (and quick) function for encoding a string with a phrase. Uses XOR to change each char. Run a second time on the same string to decode it (using the same phrase).
SuperStrict

Local sMsg:String="stephen greener is a cool cat"
Local sPhrase:string="www.sublimegames.com"

print "-----"
print "(phrase) :"+sPhrase
print "original :"+sMsg
local c:string=sXorEncode(sMsg,sPhrase)
print "encode   :"+c
local d:string=sXorEncode(c,sPhrase)
print "decode   :"+d
print "-----"
End


Function sXorEncode:string(sMessage:String,sPhrase:String)
	If sPhrase.length=0 then return sMessage 	'No encoding as no phrase
	if sMessage.length=0 then return ""   	'No message so no encoding

	Local sBuffer:String=""
	Local iPhrasePos:Int=0

	For local i:int=0 to sMessage.length-1

		Local iPhrase:Int=sPhrase[iPhrasePos]
		Local iCurrent:Int=sMessage[i]

		sBuffer:+Chr(Byte(iPhrase~iCurrent)) 	'Simple XOR encrypt

		iPhrasePos:+1  	'Next char in the phrase, wrap around
		if iPhrasePos>=sPhrase.length then iPhrasePos=0

	Next

	Return sBuffer	
EndFunction

Comments

apo2013
thnx!


Code Archives Forum