[bmx] XOR string encrypt by Shagwana [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:42

Previous topic - Next topic

BlitzBot

Title : XOR string encrypt
Author : Shagwana
Posted : 1+ years ago

Description : 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).

Code :
Code: blitzmax
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 :


apo(Posted 1+ years ago)

 thnx!