Codesnippets > Math

ROT47 decoder/encoder

(1/1)

Topzombie:
Function

--- Code: (glbasic) ---FUNCTION ROT47\$: TextInput\$
LOCAL TextLen, convertedText\$
LOCAL ROT47NumberThing
TextLen=LEN(TextInput\$)
FOR ABC=0 TO TextLen-1
IF((ASC(MID\$(TextInput\$, ABC, 1))<127) AND (ASC(MID\$(TextInput\$, ABC, 1))>32))
IF(ASC(MID\$(TextInput\$, ABC, 1))+47 > 126)
ROT47NumberThing=(ASC(MID\$(TextInput\$, ABC, 1))+47)-127
convertedText\$=convertedText\$+CHR\$(33+ROT47NumberThing)
ELSE
convertedText\$=convertedText\$+CHR\$(ASC(MID\$(TextInput\$, ABC, 1))+47)
ENDIF
ELSE
convertedText\$=convertedText\$+MID\$(TextInput\$, ABC, 1)
ENDIF
NEXT
RETURN convertedText\$
ENDFUNCTION
--- End code ---

Example:

--- Code: (glbasic) ---GLOBAL String\$, TextFileSize, ASCIIFileSize, ASCII\$, InputStr\$
GLOBAL Output\$
PRINT "Type !input or a Text",10,10
INPUT InputStr\$,10,20
IF InputStr\$="!input"
TextFileSize=GETFILESIZE("input.txt")
OPENFILE(1, "input.txt", 1)
CLOSEFILE 1
Output\$=ROT47\$(String\$)
ELSE
String\$=InputStr\$
Output\$=ROT47\$(String\$)
ENDIF
OPENFILE(1, "output.txt", 0)
WRITELINE 1, Output\$
CLOSEFILE 1
PRINT Output\$,10,10
SHOWSCREEN
KEYWAIT
END

FUNCTION ROT47\$: TextInput\$
LOCAL TextLen, convertedText\$
LOCAL ROT47NumberThing
TextLen=LEN(TextInput\$)
FOR ABC=0 TO TextLen-1
IF((ASC(MID\$(TextInput\$, ABC, 1))<127) AND (ASC(MID\$(TextInput\$, ABC, 1))>32))
IF(ASC(MID\$(TextInput\$, ABC, 1))+47 > 126)
ROT47NumberThing=(ASC(MID\$(TextInput\$, ABC, 1))+47)-127
convertedText\$=convertedText\$+CHR\$(33+ROT47NumberThing)
ELSE
convertedText\$=convertedText\$+CHR\$(ASC(MID\$(TextInput\$, ABC, 1))+47)
ENDIF
ELSE
convertedText\$=convertedText\$+MID\$(TextInput\$, ABC, 1)
ENDIF
NEXT
RETURN convertedText\$
ENDFUNCTION

--- End code ---

bigsofty:
Some sort of simple encryption?  :doubt:

Neurox:

--- Quote from: bigsofty on 2010-Apr-11 ---Some sort of simple encryption?  :doubt:

--- End quote ---
Yes, ROT47 is a variant of ROT13.
See wikipedia : http://en.wikipedia.org/wiki/Rot13

Bye bye,
Neurox

bigsofty:
ah, I see, handy... very quick and simple, thank you!  :good:

spicypixel:

--- Code: (glbasic) ---function str_rot47(\$str){
return strtr(\$str, '!"#\$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~', 'PQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~!"#\$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNO');
}

--- End code ---

You could use the above PHP ROT47 function to decrypt a simple name / high score combination for an online table. This will decrypt the encrypted text from the GLBASIC function. You will need to update the GLB function to use NETWEBEND with the URL and add the data to the URL so you can use GET from php to grab the url data sent. :)