Author Topic: ROT47 decoder/encoder  (Read 6855 times)

Offline Topzombie

  • Mc. Print
  • *
  • Posts: 17
    • View Profile
ROT47 decoder/encoder
« 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
« Last Edit: 2010-Apr-11 by Topzombie »

Offline bigsofty

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 2672
    • View Profile
Re: ROT47 decoder/encoder
« Reply #1 on: 2010-Apr-11 »
Some sort of simple encryption?  :doubt:
Cheers,

Ian.

“It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration.”
(E. W. Dijkstra)

Offline Neurox

  • Mr. Polyvector
  • ***
  • Posts: 184
    • View Profile
    • http://www.borzini.it
Re: ROT47 decoder/encoder
« Reply #2 on: 2010-Apr-12 »
Some sort of simple encryption?  :doubt:
Yes, ROT47 is a variant of ROT13.
See wikipedia : http://en.wikipedia.org/wiki/Rot13

Bye bye,
Neurox
Paolo Borzini | paolo@borzini.it
The WhiteFly Software | www.thewhitefly.it
Service on line for screen printers | www.4pellicole.it

Offline bigsofty

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 2672
    • View Profile
Re: ROT47 decoder/encoder
« Reply #3 on: 2010-Apr-12 »
ah, I see, handy... very quick and simple, thank you!  :good:
Cheers,

Ian.

“It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration.”
(E. W. Dijkstra)

Offline spicypixel

  • Prof. Inline
  • *****
  • Posts: 646
  • Pixel Artist
    • View Profile
    • SpicyPixel.net
Re: ROT47 decoder/encoder
« Reply #4 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. :)
http://www.spicypixel.net | http://www.facebook.com/SpicyPixel.NET

Comps Owned - ZX.81, ZX.48K, ZX.128K+2, Vic20, C64, Atari-ST, A500.600.1200, PC, Apple Mini-Mac.