+ Reply to Thread
Results 1 to 2 of 2

Thread: Function to Decode a byte array with RC4

  1. #1
    jamison's Avatar
    jamison is offline VB.NET Forum Newbie jamison is on a distinguished programming path ahead
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Jul 2005
    Age
    37
    Posts
    7
    Reputation
    0

    Thumbs up Function to Decode a byte array with RC4

    I need to decode a byte array using RC4. My current function handles strings fine, but not byte arrays.

    Anybody have one? I've started but it's not working out.

    Thanks,
    Jamie


    My current work algo, comes from a 4guys article.


    Code:
     
    	''' -----------------------------------------------------------------------------
    	''' <summary>
    	''' RC4 encryption and decryption.
    	''' </summary>
    	''' <param name="plaintxt"></param>
    	''' <param name="password"></param>
    	''' <returns></returns>
    	''' <remarks>
    	''' </remarks>
    	''' <history>
    	''' [jamie] 8/18/2005 Created
    	''' </history>
    	''' -----------------------------------------------------------------------------
    	Public Shared Function RC4EnDeCrypt(ByVal plaintxt As String, ByVal password As String) As String
    		Dim temp As Int32
    		Dim a, i, j, k As Int32
    		Dim cipherby As Int32
    		Dim cipher As String
    		Dim sbox As Int32()
    		Dim key As Int32()
    		i = 0
    		j = 0
    		RC4Initialize(password, sbox, key)
    		For a = 1 To Len(plaintxt)
    			i = (i + 1) Mod 256
    			j = (j + sbox(i)) Mod 256
    			temp = sbox(i)
    			sbox(i) = sbox(j)
    			sbox(j) = temp
    			k = sbox((sbox(i) + sbox(j)) Mod 256)
    			cipherby = Asc(Mid(plaintxt, a, 1)) Xor k
    			cipher = cipher & Chr(cipherby)
    		Next
    		Return cipher
    	End Function
    	''' -----------------------------------------------------------------------------
    	''' <summary>
    	''' This routine called by EnDeCrypt function. Initializes the
    	''' sbox and the key array)
    	''' </summary>
    	''' <param name="password"></param>
    	''' <param name="sbox"></param>
    	''' <param name="key"></param>
    	''' <remarks>
    	''' </remarks>
    	''' <history>
    	''' [jamie] 8/18/2005 Created
    	''' </history>
    	''' -----------------------------------------------------------------------------
    	Protected Shared Sub RC4Initialize(ByVal password As String, ByRef sbox() As Int32, ByRef key() As Int32)
    		Dim tempSwap As Int32
    		Dim intLength, a, b As Int32
    		intLength = Len(password)
    		ReDim sbox(255)
    		ReDim key(255)
    		For a = 0 To 255
    			key(a) = Asc(Mid(password, (a Mod intLength) + 1, 1))
    			sbox(a) = a
    		Next
    		b = 0
    		For a = 0 To 255
    			b = (b + sbox(a) + key(a)) Mod 256
    			tempSwap = sbox(a)
    			sbox(a) = sbox(b)
    			sbox(b) = tempSwap
    		Next
    	End Sub
    Last edited by jamison; 09-12-2005 at 5:58 PM.

  2. #2
    jamison's Avatar
    jamison is offline VB.NET Forum Newbie jamison is on a distinguished programming path ahead
    .NET Framework
    .NET 2.0 (VS 2005)
    Join Date
    Jul 2005
    Age
    37
    Posts
    7
    Reputation
    0

    Red face

    Sweet, I think I got it.

    Code:
      
    
    	''' -----------------------------------------------------------------------------
    	''' <summary>
    	''' RC4 encryption and decryption.
    	''' </summary>
    	''' <param name="plaintxt"></param>
    	''' <param name="password"></param>
    	''' <returns></returns>
    	''' <remarks>
    	''' </remarks>
    	''' <history>
    	''' [jamie] 8/18/2005 Created
    	''' </history>
    	''' -----------------------------------------------------------------------------
    	Public Shared Function RC4EnDeCrypt(ByVal plaintxt As Byte(), ByVal password As Byte()) As Byte()
    		Dim k As Int32
    		Dim kByte As Byte()
    		Dim a, i, j As Int32
    		Dim cipherby As Int32
    		Dim cipher As Byte()
    		ReDim cipher(plaintxt.Length)
    
    		Dim sbox(256) As Int32
    		Dim temp As Int32
    
    		i = 0
    		j = 0
    		RC4Initialize(password, sbox)
    		For a = 0 To plaintxt.Length - 1
    			i = (i + 1) Mod 256
    			j = (j + sbox(i)) Mod 256
    			'Swap
    			temp = sbox(i)
    			sbox(i) = sbox(j)
    			sbox(j) = temp
    			'Get the output
    			k = sbox((sbox(i) + sbox(j)) Mod 256)
    			plaintxt(a) = plaintxt(a) Xor Convert.ToByte(k)
    		Next
    
    		Return plaintxt
    	End Function
    
    	''' -----------------------------------------------------------------------------
    	''' <summary>
    	''' This routine called by EnDeCrypt function. Initializes the
    	''' sbox and the key array)
    	''' </summary>
    	''' <param name="password"></param>
    	''' <param name="sbox"></param>
    	''' <remarks>
    	''' </remarks>
    	''' <history>
    	'''  [jamie] 8/18/2005 Created
    	''' </history>
    	''' -----------------------------------------------------------------------------
    	Protected Shared Sub RC4Initialize(ByVal key As Byte(), ByRef sbox As Int32())
    		Dim tempSwap As Int32
    		Dim i, j, l As Int32
    		l = key.Length
    
    		For i = 0 To 255
    			sbox(i) = i
    		Next
    		j = 0
    		For i = 0 To 255
    			j = (j + sbox(i) + key(i Mod l)) Mod 256
    			tempSwap = sbox(i)
    			sbox(i) = sbox(j)
    			sbox(j) = tempSwap
    		Next
    	End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Bookmarks

Posting Permissions

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