B4A Class ROT13 and Atbash substitution cipher Code

Luis Costa

New Member
Licensed User
Welll this is my first post.
not my first code :) for sure.
hope to be helpful for someone in the world.
very easy to convert to another languase if necessary.
you may distribute and use it for free.
any question please contact me.

the code module contains 2 tipes of substitution cipher.
for more detail about who it works please visit:
http://en.wikipedia.org/wiki/ROT13
and
http://en.wikipedia.org/wiki/Atbash

B4X:
'Code module
'Subs in this code module will be accessible from all modules.
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub
'"rotate by 13 places" substitution cipher
'http://en.wikipedia.org/wiki/ROT13
Public Sub ROT13(stringx As String) As String
    Dim ByteX () As Byte
    ByteX=stringx.GetBytes("UTF8")
    Dim finalX As String
    For x=0 To ByteX.Length-1
        If (ByteX(x)>=97 AND ByteX(x)<=109 )OR (ByteX(x)>=65 AND ByteX(x)<=77 ) Then
            ByteX(x)=ByteX(x)+13
        Else If (ByteX(x)>=110 AND ByteX(x)<=122 )OR (ByteX(x)>=78 AND ByteX(x)<=90 )  Then
            ByteX(x)=ByteX(x)-13
        End If
        finalX=finalX & Chr(ByteX(x))
    Next
    Return finalX
End Sub
'"Atbash" substitution cipher
'http://en.wikipedia.org/wiki/Atbash
Sub Atbash(StringX As String) As String
    Dim ByteX () As Byte
    ByteX=StringX.GetBytes("UTF8")
    Dim finalX As String
    '65-90   97- 122
    For x=0 To ByteX.Length-1
        If (ByteX(x)>=65 AND ByteX(x)<=90 ) Then
            ByteX(x)=Abs(ByteX(x)-155)                    '155 comes from 65+90
        Else If (ByteX(x)>=97 AND ByteX(x)<=122 ) Then
            ByteX(x)=Abs(ByteX(x)-219)                    '219 comes from 97+122
        End If
        finalX=finalX & Chr(ByteX(x))
    Next
    Return finalX
End Sub

Thanks and happy coding :)
Long live FREE CODE
 

Attachments

Top