Visual Basic .NET Forums  
Click here to advertise with us

Go Back   Visual Basic .NET Forums > Visual Studio .NET > Security/Obfuscation

Security/Obfuscation Security related discussion pertaining to configuration, development, and deployment etc.

VB.NET Forums Newsletter Signup:
Email address:


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 09-12-2005, 3:43 PM
jamison's Avatar
VB.NET Forum Newbie
.NET Framework: .NET 2.0 (VS 2005)
 
Join Date: Jul 2005
Age: 36
Posts: 7
Reputation: 0
jamison is on a distinguished programming path ahead
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 09-12-2005, 5:57 PM
jamison's Avatar
VB.NET Forum Newbie
.NET Framework: .NET 2.0 (VS 2005)
 
Join Date: Jul 2005
Age: 36
Posts: 7
Reputation: 0
jamison is on a distinguished programming path ahead
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





All times are GMT -4. The time now is 3:09 AM.

Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2


For advertising opportunities click here.