Results 1 to 3 of 3

Thread: VB6 - Scramble Text

  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.

  2. #2
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: VB6 - Scramble Text

    surely you'd use a Byte array here, rather than Mid-ing the string.

  3. #3

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

    Re: VB6 - Scramble Text

    Quote Originally Posted by bushmobile
    surely you'd use a Byte array here, rather than Mid-ing the string.
    If you insist
    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
    14.  
    15. Private Function ScrambleString(ByVal Str As String, Optional SeekInit As Long = 0) As String
    16.     '
    17.     '  Made by Michael Ciurescu (CVMichael from vbforums.com)
    18.     '  Original thread: [url]http://www.vbforums.com/showthread.php?t=415267[/url]
    19.     '
    20.  
    21.     Dim K As Long, Sk As Long, Tmp As Byte, RndPos As Long
    22.     Dim Data() As Byte, UB As Long
    23.    
    24.     Data = StrConv(Str, vbFromUnicode)
    25.    
    26.     Sk = SeekInit
    27.     UB = UBound(Data)
    28.    
    29.     For K = 0 To UB
    30.         Sk = Sk + Data(K)
    31.     Next K
    32.    
    33.     Rnd -1
    34.     Randomize Sk
    35.    
    36.     For K = 0 To UB
    37.         RndPos = Fix((UB + 1) * Rnd)
    38.        
    39.         ' SWAP Chars
    40.         Tmp = Data(K)
    41.         Data(K) = Data(RndPos)
    42.         Data(RndPos) = Tmp
    43.     Next K
    44.    
    45.     ScrambleString = StrConv(Data, vbUnicode)
    46. End Function
    47.  
    48. Private Function UnscrambleString(ByVal Str As String, Optional SeekInit As Long = 0) As String
    49.     '
    50.     '  Made by Michael Ciurescu (CVMichael from vbforums.com)
    51.     '  Original thread: [url]http://www.vbforums.com/showthread.php?t=415267[/url]
    52.     '
    53.  
    54.     Dim K As Long, Sk As Long, Tmp As Byte, RndPos As Long
    55.     Dim RndArr() As Long, Data() As Byte, UB As Long
    56.    
    57.     Data = StrConv(Str, vbFromUnicode)
    58.    
    59.     Sk = SeekInit
    60.     UB = UBound(Data)
    61.    
    62.     For K = 0 To UB
    63.         Sk = Sk + Data(K)
    64.     Next K
    65.    
    66.     Rnd -1
    67.     Randomize Sk
    68.    
    69.     ReDim RndArr(UB)
    70.     For K = 0 To UB
    71.         RndArr(K) = Fix((UB + 1) * Rnd)
    72.     Next K
    73.    
    74.     For K = UB To 0 Step -1
    75.         RndPos = RndArr(K)
    76.        
    77.         ' SWAP Chars
    78.         Tmp = Data(K)
    79.         Data(K) = Data(RndPos)
    80.         Data(RndPos) = Tmp
    81.     Next K
    82.    
    83.     UnscrambleString = StrConv(Data, vbUnicode)
    84. End Function

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