Results 1 to 3 of 3

Thread: VB6 - Scramble Text

Threaded View

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    VB6 - Scramble Text

    This is an idea I got while helping for one of the threads in General VB forum.
    It was asked to "jumble up messages", so I got the idea to randomly swap the letters in the string, but of course, if you use random numbers, then you have to repeate the same sequance again in order to decode.
    So then I remembered I already did something simmilar

    So, here's the code:
    VB Code:
    1. Private Function ScrambleString(ByVal Str As String, Optional SeekInit As Long = 0) As String
    2.     '
    3.     '  Made by Michael Ciurescu (CVMichael from vbforums.com)
    4.     '  Original thread: [url]http://www.vbforums.com/showthread.php?t=415267[/url]
    5.     '
    6.  
    7.     Dim K As Long, Sk As Long, Tmp As String, RndPos As Long
    8.    
    9.     Sk = SeekInit
    10.     For K = 1 To Len(Str)
    11.         Sk = Sk + Asc(Mid$(Str, K, 1))
    12.     Next K
    13.    
    14.     Rnd -1
    15.     Randomize Sk
    16.    
    17.     For K = 1 To Len(Str)
    18.         RndPos = 1 + Fix(Len(Str) * Rnd)
    19.        
    20.         ' SWAP Chars
    21.         Tmp = Mid$(Str, K, 1)
    22.         Mid$(Str, K, 1) = Mid$(Str, RndPos, 1)
    23.         Mid$(Str, RndPos, 1) = Tmp
    24.     Next K
    25.    
    26.     ScrambleString = Str
    27. End Function
    28.  
    29. Private Function UnscrambleString(ByVal Str As String, Optional SeekInit As Long = 0) As String
    30.     '
    31.     '  Made by Michael Ciurescu (CVMichael from vbforums.com)
    32.     '  Original thread: [url]http://www.vbforums.com/showthread.php?t=415267[/url]
    33.     '
    34.  
    35.     Dim K As Long, Sk As Long, Tmp As String, RndPos As Long
    36.     Dim RndArr() As Long
    37.    
    38.     Sk = SeekInit
    39.     For K = 1 To Len(Str)
    40.         Sk = Sk + Asc(Mid$(Str, K, 1))
    41.     Next K
    42.    
    43.     Rnd -1
    44.     Randomize Sk
    45.    
    46.     ReDim RndArr(Len(Str) - 1)
    47.     For K = 1 To Len(Str)
    48.         RndArr(K - 1) = 1 + Fix(Len(Str) * Rnd)
    49.     Next K
    50.    
    51.     For K = Len(Str) To 1 Step -1
    52.         RndPos = RndArr(K - 1)
    53.        
    54.         ' SWAP Chars
    55.         Tmp = Mid$(Str, K, 1)
    56.         Mid$(Str, K, 1) = Mid$(Str, RndPos, 1)
    57.         Mid$(Str, RndPos, 1) = Tmp
    58.     Next K
    59.    
    60.     UnscrambleString = Str
    61. End Function
    This is an example how to use the functions:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     Dim S As String
    5.    
    6.     S = ScrambleString("testing 1 2 3 4 5 6 7 8 9 0 hello there... blah blah")
    7.    
    8.     Debug.Print S
    9.    
    10.     S = UnscrambleString(S)
    11.    
    12.     Debug.Print S
    13. End Sub
    Last edited by CVMichael; Jul 6th, 2006 at 09:49 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width