jamison
Member
- Joined
- Jul 6, 2005
- Messages
- 7
- Programming Experience
- 5-10
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.
 
 
	
	
	
	
	
	
	
	
	
		
			
			
			
			
			
		
	
	
	
		
	
	
		
	
	
		
			
		
		
	
				
			Anybody have one? I've started but it's not working out.
Thanks,
Jamie
My current work algo, comes from a 4guys article.
			
				VB.NET:
			
		
		
			''' -----------------------------------------------------------------------------
	''' <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: 
			
		
	
								
								
									
	
		
			
		
		
	
	
	
		
			
		
		
	
								
							
							 
	 
 
		