Page 1 of 1

ROT-13 Encoding/Decoding

Posted: Mon Feb 23, 2004 9:51 pm
by AlGonzalez
Code updated for 5.20+

See code comments for description.

Code: Select all

; Rotate 13 encoder/decoder function
;
; Some newsgroup postings are rot-13 encoded.
; It is sometimes used to hide possibly offensive material
; from accidental viewing (off-color jokes, for instance).
; The message will appear as gibberish.
;
; Rot-13 encoding can be used for other purposes as well.
; Discussions that give away the ending of a novel or a movie,
; For example, can be encoded so that only someone so inclined
; may view it.

Procedure.s Rot13(text.s) 
  Protected result.s, textLen 
  
  textLen = Len(Text) 
  If textLen > 0 
    result = Space(textLen)        
    
    Protected i, c.w
    
    For i = 0 To (textLen - 1) 
      c = PeekB(@text + i)
      ; If A..Z add 13 If then > Z subtract 26
      ; in order to wrap it back into range
      If (c >=65) And (c <= 90) 
        c + 13 
        If (c > 90) : c - 26 : EndIf 
      Else 
        ; If a..z add 13 If then > z subtract 26 
        ; in order to wrap it back into range
        If (c >= 97) And (c <= 122) 
          c + 13 
          If (c > 122) : c - 26 : EndIf 
        Else 
          ; Otherwise do nothing 
        EndIf 
      EndIf 
      PokeB(@result + i, c) 
    Next i 
  EndIf 
  
  ProcedureReturn result 
EndProcedure

If OpenConsole()
  s.s = "Dude - where's my car!"
  PrintN("Original Text:  " + s)
  s = Rot13(s)
  PrintN("Rot-13 Encoded: " + s)
  s = Rot13(s)
  PrintN("Rot-13 Decoded: " + s)
  
  PrintN("")
  PrintN("-- The alphabet")
  s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  PrintN("Original Text:  " + s)
  s = Rot13(s)
  PrintN("Rot-13 Encoded: " + s)
  s = Rot13(s)
  PrintN("Rot-13 Decoded: " + s)
  
  Print("Press a key when done... ")
  Repeat : Until Inkey()
  CloseConsole()
EndIf

End

Posted: Wed Mar 03, 2004 6:17 am
by AlGonzalez
Here is another encoder/decoder using bitwise xor:

Code: Select all

; Simple XOr encoder/decoder function
Procedure.s XOREncode(text.s, key.s)
    Protected result.s, textLen.l

    textLen = Len(text)
    If textLen
        result = text

        Protected keyLen.l
        keyLen = Len(key)

        If keyLen
            Protected i.l
            For i = 0 To (textLen - 1)
                ; using modulo to determine which letter from key to use
                PokeB(@result + i, PeekB(@result + i) ! (~PeekB(@key + (i % keyLen))))
            Next i
        EndIf
    EndIf    
    
    ProcedureReturn result
EndProcedure
Example:

Code: Select all

If OpenConsole()
    s.s = "Dude - where's my car!"
    PrintN("Original Text:  " + s)
    s = XOrEncode(s, "purebasic")
    PrintN("XOr Encoded:    " + s)
    s = XOrEncode(s, "purebasic")
    PrintN("XOr Decoded:    " + s)

    PrintN("")
    PrintN("-- The alphabet")
    s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    PrintN("Original Text:  " + s)
    s = XOrEncode(s, "PureBasic")
    PrintN("XOr Encoded:    " + s)
    s = XOrEncode(s, "PureBasic")
    PrintN("XOr Decoded:    " + s)

    Print("Press a key when done... ")
    Repeat : Until Inkey()
    CloseConsole()
EndIf

End