Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP General > 4guys RC4 encryption demo

Reply
Thread Tools

4guys RC4 encryption demo

 
 
Wilk Teverbaugh
Guest
Posts: n/a
 
      01-13-2004

I am using 4guys RC4 demo and am a little lost on how to store my encrypted
strings. In the demo it shows how to encrypt a string into binary and then
use URLencode to encode it. I'm not sure how to decode the string into
binary so I can then unencrypt my string... I am storing my strings in SQL
server.

Is it possible to just store my strings as binary or do I need to encode
them first? If I need to encode them how can I decode them so I can then
decrypt them?

Any help would be greatly appreciated as I am a little lost.



Dim sbox(255)
Dim key(255)
Sub RC4Initialize(strPwd)
dim tempSwap
dim a
dim b
intLength = len(strPwd)
For a = 0 To 255
key(a) = asc(mid(strpwd, (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
Function EnDeCrypt(plaintxt, psw)
dim temp
dim a
dim i
dim j
dim k
dim cipherby
dim cipher
i = 0
j = 0
RC4Initialize psw
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
EnDeCrypt = cipher
End Function
dim etime, stime
dim psw, txt
dim strTemp
dim x
etime = 0
stime = 0

psw = "passphrase"
cString = "test string.."
stime = timer
strTemp = EnDeCrypt(cString, psw)
etime = cdbl(timer - stime)

function decode(myStr)
dim k,s1,s2
while inStr(myStr,"&#")
k = inStr(myStr,"&#")
s1= Mid(myStr,k,6)
s2= Mid(s1,3,3)
myStr = replace(myStr,s1,chr(s2))
wend
decode=myStr
end function


for x = 1 to len(strTemp)
newString = newString & right(string(2,"0") & hex(asc(mid(strTemp, x,
1))),2)
next


test1 = EnDeCrypt(cString, psw)
response.write "Binary string = " & test1 & "<br>"
encodedStr = server.urlencode(test1)
response.write "URL encoded string = " & encodedStr & "<br>"
response.write "Decrypted String = " & EnDeCrypt(test1, psw)


 
Reply With Quote
 
 
 
 
swp
Guest
Posts: n/a
 
      01-15-2004
"Wilk Teverbaugh" <(E-Mail Removed)> wrote in message news:<#(E-Mail Removed)>...
> I am using 4guys RC4 demo and am a little lost on how to store my encrypted
> strings. In the demo it shows how to encrypt a string into binary and then
> use URLencode to encode it. I'm not sure how to decode the string into
> binary so I can then unencrypt my string... I am storing my strings in SQL
> server.
>
> Is it possible to just store my strings as binary or do I need to encode
> them first? If I need to encode them how can I decode them so I can then
> decrypt them?
>
> Any help would be greatly appreciated as I am a little lost.
>
>
>
> Dim sbox(255)
> Dim key(255)
> Sub RC4Initialize(strPwd)
> dim tempSwap
> dim a
> dim b
> intLength = len(strPwd)
> For a = 0 To 255
> key(a) = asc(mid(strpwd, (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
> Function EnDeCrypt(plaintxt, psw)
> dim temp
> dim a
> dim i
> dim j
> dim k
> dim cipherby
> dim cipher
> i = 0
> j = 0
> RC4Initialize psw
> 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
> EnDeCrypt = cipher
> End Function
> dim etime, stime
> dim psw, txt
> dim strTemp
> dim x
> etime = 0
> stime = 0
>
> psw = "passphrase"
> cString = "test string.."
> stime = timer
> strTemp = EnDeCrypt(cString, psw)
> etime = cdbl(timer - stime)
>
> function decode(myStr)
> dim k,s1,s2
> while inStr(myStr,"&#")
> k = inStr(myStr,"&#")
> s1= Mid(myStr,k,6)
> s2= Mid(s1,3,3)
> myStr = replace(myStr,s1,chr(s2))
> wend
> decode=myStr
> end function
>
>
> for x = 1 to len(strTemp)
> newString = newString & right(string(2,"0") & hex(asc(mid(strTemp, x,
> 1))),2)
> next
>
>
> test1 = EnDeCrypt(cString, psw)
> response.write "Binary string = " & test1 & "<br>"
> encodedStr = server.urlencode(test1)
> response.write "URL encoded string = " & encodedStr & "<br>"
> response.write "Decrypted String = " & EnDeCrypt(test1, psw)


I use this function to decode URLs that were URLencoded:

function URLDecode(e)
Dim r, s, t
Dim p
s = replace(e, "+", " ")
for p = 1 to len(s)
t = mid(s, p, 1)
if ((t = "%") and (mid(s, p+1, 1) < "A")) then
if p + 2 < len(s) then
r = r & chr(cint("&H" & mid(s, p + 1, 2)))
p = p + 2
end if
else
r = r & t
end if
next
URLDecode = r
end function

hope it helps

swp
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using NAnt RC4 to build .Net 1.1 Code SenthilVel ASP .Net 0 06-12-2006 06:49 AM
wxPython demo /Process does not open new demo Andy Leszczynski Python 1 02-18-2005 06:03 PM
PyOpenGL demo in wxPython demo crashes F. GEIGER Python 9 05-03-2004 04:43 PM
ANN: Leo 4.1 rc4: An outlining editor Edward K. Ream Python 0 02-04-2004 01:26 AM
RC4 Implementation Boogie El Aceitoso C++ 3 09-03-2003 02:31 PM



Advertisments