B4A Class ROT13 and Atbash substitution cipher Code

Discussion in 'Additional libraries, classes and official updates' started by Luis Costa, Mar 7, 2015.

  1. Luis Costa

    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

    Code:
    '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 StringAs 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 StringAs 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
     

    Attached Files:

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice