GLBasic forum

Codesnippets => Math => Topic started by: Topzombie on 2010-Apr-11

Title: ROT47 decoder/encoder
Post by: Topzombie on 2010-Apr-11
Function
Code (glbasic) Select
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








Example:
Code (glbasic) Select
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)
READSTR 1, String$, TextFileSize
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
Title: Re: ROT47 decoder/encoder
Post by: bigsofty on 2010-Apr-11
Some sort of simple encryption?  :doubt:
Title: Re: ROT47 decoder/encoder
Post by: Neurox on 2010-Apr-12
Quote from: bigsofty on 2010-Apr-11
Some sort of simple encryption?  :doubt:
Yes, ROT47 is a variant of ROT13.
See wikipedia : http://en.wikipedia.org/wiki/Rot13

Bye bye,
Neurox
Title: Re: ROT47 decoder/encoder
Post by: bigsofty on 2010-Apr-12
ah, I see, handy... very quick and simple, thank you!  :good:
Title: Re: ROT47 decoder/encoder
Post by: spicypixel on 2012-Jul-16
Code (glbasic) Select

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


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