String Scramble/Descramble Algorithm

chriswelborn 0 Tallied Votes 1K Views Share

This is a piece of code I wrote a long time ago when I saw someone make a "guess this scrambled word" game. The program didn't have to unscramble the word to know what the answer was, but I wanted to do it anyways. I wanted an algorithm that could scramble a word or string up, and then put it back together when needed. That way, even if I didn't know what the string was at first, it could still be unscrambled. I actually coupled this with another function that would call strScramble() and strDescramble() a set number of times to really scramble things up. With short words, less than 5 characters long it's really hard to scramble, if you do it more than one time a small word will end up putting itself back together sometimes. I use this and a simple ascii character code shift function as an obfuscator for low-risk data sometimes. It's not high-security, nor uncrackable, but it keeps the honest people from knowing what the data is, or from changing it without creating problems. (my apps have fallbacks/warnings/etc. incase this happens, most apps won't even give you that.)

'EVEN NUMBER???
   ' This first function "IsEven" is used in my scramble functions, 
   ' basically returns true or false if a number is even or odd
   
    ''' <summary>Determines whether an integer is an Even-Number.</summary>
    ''' <param name="n">Number (Integer) to examine.</param>
    ''' <remarks>Example: Dim bNumber as Boolean = IsEven(21) 'Returns FALSE because 21 is NOT even.</remarks>
    ''' <returns><paramref name="Boolean"/> value of the answer.</returns>
    Public Shared Function IsEven(ByVal n As Integer) As Boolean
        Try
            If Strings.Right(n.ToString, 1) = "0" Or _
                Strings.Right(n.ToString, 1) = "2" Or _
                Strings.Right(n.ToString, 1) = "4" Or _
                Strings.Right(n.ToString, 1) = "6" Or _
                Strings.Right(n.ToString, 1) = "8" Then
                Return True
            Else
                Return False
            End If
        Catch eX As Exception
            Return False
        End Try


    End Function
	
 'Scramble String
    Public Shared Function strScramble(ByVal s As String) As String
        Dim sFin As String = "" 'Final string
        Dim sBuf1 As String = "" 'Buffer
        Dim sBuf2 As String = "" 'Buffer2

        For i As Integer = 1 To Len(s)
            If Not IsEven(i) Then sBuf1 = sBuf1 & Mid(s, i, 1) 'Odd digit
            If IsEven(i) Then sBuf2 = sBuf2 & Mid(s, i, 1) 'Even digit
        Next

        sFin = sBuf1 & sBuf2
        Return sFin

    End Function
	
	'Descramble String
   Public Shared Function strDescramble(ByVal s As String) As String
        Dim sFin As String = ""
        Dim sBuf1 As String = ""
        Dim sBuf2 As String = ""

		If IsEven(Len(s)) Then 'Even number length
            sBuf1 = Strings.Left(s, (Len(s) / 2)) 'left half of string
            sBuf2 = Strings.Right(s, (Len(s) / 2)) ' right half of string
            For i As Integer = 1 To Len(sBuf1)
                'regroup char by char
                sFin = sFin & Mid(sBuf1, i, 1) & Mid(sBuf2, i, 1)
            Next
        Else 'Odd number length
            sBuf1 = Strings.Left(s, ((Len(s) + 1) / 2)) 'left half of string
            sBuf2 = Strings.Right(s, ((Len(s) - 1) / 2)) ' right half of string
            For i As Integer = 1 To Len(sBuf2)
                'regroup char by char
                sFin = sFin & Mid(sBuf1, i, 1) & Mid(sBuf2, i, 1)
            Next
            'add the last odd char
            sFin = sFin & Strings.Right(sBuf1, 1)
        End If

        Return sFin
    End Function
Reverend Jim 4,780 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster

Very inefficient way to test for odd or even. A better way is

(number And 1) = 0

This evaluates to True if number is even and False if it is odd.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.