'-------------------------------------------------------------------
'-- simple replacement cipher vbscript sample - davemoats.com
'--
'-- This sample is provided 'as-is', without any express or implied warranty.
'-- In no event will the authors be held liable for any damages arising from
'-- the use of this sample code.
'--
'-- algorithm for the simple cipher originated from
'-- http://www.dickinson.edu/~braught/courses/cs131s99/Labs/Lab10.html
'--
'-------------------------------------------------------------------
option explicit

' define the variables required
dim cipherArray(27)
dim characterArray(27)
dim strTest

' load the arrays used to provide the simple replacement cipher
initializeArrays cipherArray, characterArray

' prompt the user for a string to encrypt
strTest = inputbox( "Please enter a string to run through the replacement cipher" , "Simple Replacement Cipher" )

if strTest = "" then

     wscript.echo "You must enter a string!"
     wscript.quit()

end if

wscript.echo "You entered - " & strTest

' calling the crypt function to    perform the cipher replacement
strTest = crypt( strTest, characterArray, cipherArray )

wscript.echo "After the replacement cipher - " & strTest

' calling the crypt function to reverse the cipher
strTest = crypt( strTest, cipherArray, characterArray )

wscript.echo "Back to normal - " & strTest

'function to perform the cipher
function crypt( strInput, SourceArray, ConversionArray )

     dim strOut
     dim cCheck
     dim i
     dim j
     dim bFoundIt

     strOut = ""
     cCheck = ""
     i = 1      j = 1
     ' loop through the string looking at each character
     for i=1 to len( strInput )

            'get the character we want
            cCheck = mid( strInput, i, 1 )

            bFoundIt = "FALSE"

            'find the corresponding letter
            for j=1 to ubound( SourceArray )

                 if cCheck = SourceArray(j) then

                        strOut = strOut + ConversionArray(j)
                        bFoundIt = "TRUE"
                        exit for

                 end if

            next

            ' unable to find the entered character so add it to the out put
            if bFoundIt = "FALSE" then

                    strOut = strOut + cCheck

            end if

     next

     ' returning the ciphered value
     crypt = strOut

end function

' initialize the arrays used to perform cipher
sub initializeArrays( cipherArray, characterArray )

     cipherArray(1) = "s"
     cipherArray(2) = "c"
     cipherArray(3) = "h"
     cipherArray(4) = "n"
     cipherArray(5) = "i"
     cipherArray(6) = "t"
     cipherArray(7) = "z"
     cipherArray(8) = "e"
     cipherArray(9) = "l"
     cipherArray(10) = "m"
     cipherArray(11) = "o"
     cipherArray(12) = "p"
     cipherArray(13) = "q"
     cipherArray(14) = "r"
     cipherArray(15) = "u"
     cipherArray(16) = "v"
     cipherArray(17) = "w"
     cipherArray(18) = "x"
     cipherArray(19) = "y"
     cipherArray(20) = "a"
     cipherArray(21) = "b"
     cipherArray(22) = "d"
     cipherArray(23) = "f"
     cipherArray(24) = "g"
     cipherArray(25) = "j"
     cipherArray(26) = "k"

     characterArray(1)= "a"
     characterArray(2)= "b"
     characterArray(3)= "c"
     characterArray(4)= "d"
     characterArray(5)= "e"
     characterArray(6)= "f"
     characterArray(7)= "g"
     characterArray(8)= "h"
     characterArray(9)= "i"
     characterArray(10)= "j"
     characterArray(11)= "k"
     characterArray(12)= "l"
     characterArray(13)= "m"
     characterArray(14)= "n"
     characterArray(15)= "o"
     characterArray(16)= "p"
     characterArray(17)= "q"
     characterArray(18)= "r"
     characterArray(19)= "s"
     characterArray(20)= "t"
     characterArray(21)= "u"
     characterArray(22)= "v"
     characterArray(23)= "w"
     characterArray(24)= "x"
     characterArray(25)= "y"
     characterArray(26)= "z"

end sub


Copyright © 2005 - 2013 Dave Moats.  All rights reserved.  Links: Copyright © by their respective owners.
NO WARRANTIES EXTENDED.  Void where prohibited by law.  Please report any issues or broken links.
You may link to this site freely from your own site.  You may quote from this site, but please include a link to the original source on the originating site.