Main Menu

Encode / Decode String

Started by kevin, February 02, 2023, 07:38:35 AM

Previous topic - Next topic

kevin


   These functions are an encode and simple decoder.   The method used is we encode the source string by stepping through the input string character by character.   For each character we mult it by our key value, then output this value as a string following by a delimiter character which is the minus character in this example.       Next we step through this numeric data and replace various combinations with a-z.     

   The RestoreJunk does the same thing but in reverse to get back to the original string. 


PlayBASIC Code: [Select]
   openscreen  1200,500,32,1

s$="..Hello World.."


print "Original String:"+s$

Key= 42287
Encoded$=MakeJunk(s$,Key)

print "Encoded String:"+Encoded$

Result$=RestoreJunk(Encoded$,key)

print ""

print "Decoded String:"+Result$

if s$=Result$
print "Match"
else
print "They Dont match"
endif



Sync
waitkey



Function MakeJunk(s$,key)

// Convert to numbers
For lp =1 to Len(s$)
ThisCHR=mid(s$,lp)
ThisCHR=ThisCHR * Key
result$+=str$(ThisCHR)+"-"
next

#print result$

// Convert Numbers to letters
for lp=asc("z") to asc("a") step -1
ThisCHR = lp-asc("a")
result$ = replace$(Result$,str$(ThisCHR),chr$(lp))
next


EndFunction result$


Function RestoreJunk(s$,key)

// Convert Numbers to letters
for lp=asc("z") to asc("a") step -1
ThisCHR = lp-asc("a")
s$ = replace$(s$,chr$(lp),str$(ThisCHR) )
next

print s$

Dim Numbers(1000)

Count=SplitToArray(s$,"-",Numbers())

for lp =0 to Count-1
Result$+=chr$(Numbers(lp) / Key)
next

EndFunction result$