Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Wed Oct 23, 2019 11:16 pm
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 16 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: RC4 Encryption
PostPosted: Sun Feb 03, 2008 2:20 pm 
Offline

Joined: Sun Feb 03, 2008 2:17 pm
Posts: 1
Has anyone got the code for RC4 encryption in realbasic

This is the closest I could find but I can't convert it :(
Public Function RC4(ByVal Expression As String, ByVal Password As String) As String
On Error Resume Next
Dim RB(0 To 255) As Integer, X As Long, Y As Long, Z As Long, Key() As Byte, ByteArray() As Byte, Temp As Byte
If Len(Password) = 0 Then
Exit Function
End If
If Len(Expression) = 0 Then
Exit Function
End If
If Len(Password) > 256 Then
Key() = StrConv(Left$(Password, 256), vbFromUnicode)
Else
Key() = StrConv(Password, vbFromUnicode)
End If
For X = 0 To 255
RB(X) = X
Next X
X = 0
Y = 0
Z = 0
For X = 0 To 255
Y = (Y + RB(X) + Key(X Mod Len(Password))) Mod 256
Temp = RB(X)
RB(X) = RB(Y)
RB(Y) = Temp
Next X
X = 0
Y = 0
Z = 0
ByteArray() = StrConv(Expression, vbFromUnicode)
For X = 0 To Len(Expression)
Y = (Y + 1) Mod 256
Z = (Z + RB(Y)) Mod 256
Temp = RB(Y)
RB(Y) = RB(Z)
RB(Z) = Temp
ByteArray(X) = ByteArray(X) Xor (RB((RB(Y) + RB(Z)) Mod 256))
Next X
RC4 = StrConv(ByteArray, vbUnicode)
End Function

It's visual basic


Top
 Profile  
Reply with quote  
 Post subject: Re: RC4 Encryption
PostPosted: Thu May 29, 2008 3:58 pm 
Offline
User avatar

Joined: Sun Dec 10, 2006 7:57 am
Posts: 124
Location: Addison, Texas
Sorry about the delayed reply; however, here's the source to a method I converted ALONG time ago! Works like a champ! Enjoy! :P

function rc4(strData as string, strKey as string) as string

dim memAsciiArray(255) as integer
dim memKeyArray(255) as integer
dim memJump as integer
dim memTemp as integer
dim memY as integer
dim intKeyLength as integer
dim intIndex as integer
dim intT as integer
dim intX as integer
dim strCData as string

intKeyLength = len(strKey)

for intIndex = 0 to 255
memKeyArray(intIndex) = asc(mid(strKey, ((intIndex) mod (intKeyLength)) + 1, 1))
next

for intIndex = 0 to 255
memAsciiArray(intIndex) = intIndex
next

for intIndex = 0 to 255
memJump = (memJump + memAsciiArray(intIndex) + memKeyArray(intIndex)) mod 256
memTemp = memAsciiArray(intIndex)
memAsciiArray(intIndex) = memAsciiArray(memJump)
memAsciiArray(memJump) = memTemp
next

intIndex = 0
memJump = 0

for intX = 1 to len(strData)
intIndex = (intIndex + 1) mod 256
memJump = (memJump + memAsciiArray(intIndex)) mod 256
intT = (memAsciiArray(intIndex) + memAsciiArray(memJump)) mod 256
memTemp = memAsciiArray(intIndex)
memAsciiArray(intIndex) = memAsciiArray(memJump)
memAsciiArray(memJump) = memTemp
memY = memAsciiArray(intT)
strCData = strCData + chr(bitwise.bitxor(asc(mid(strData, intX, 1)), bitwise.bitxor(memTemp,memY)))
next

return strCData

end function

_________________
Respectfully,
Eric Brown

http://www.facebook.com/ericdbrown


Top
 Profile  
Reply with quote  
 Post subject: Re: RC4 Encryption
PostPosted: Thu Feb 26, 2009 6:28 pm 
Offline

Joined: Sun Feb 22, 2009 8:08 pm
Posts: 11
Per suggestions on another post:

viewtopic.php?f=1&t=26586&p=141899#p141899

I've modified this code to use memoryblocks, which greatly speeds up the code (i.e., from over 4 minutes to less than 3 seconds for 120,000 characters). Of course this time will vary according to your computer.

Dim MM as MemoryBlock = strData
Dim MM2 as New MemoryBlock(LenB(strData))

intKeyLength = len(strKey)

for intIndex = 0 to 255
memKeyArray(intIndex) = asc(mid(strKey, ((intIndex) mod (intKeyLength)) + 1, 1))
next

for intIndex = 0 to 255
memAsciiArray(intIndex) = intIndex
next

for intIndex = 0 to 255
memJump = (memJump + memAsciiArray(intIndex) + memKeyArray(intIndex)) mod 256
memTemp = memAsciiArray(intIndex)
memAsciiArray(intIndex) = memAsciiArray(memJump)
memAsciiArray(memJump) = memTemp
next

intIndex = 0
memJump = 0

for intX = 1 to theLen
intIndex = (intIndex + 1) mod 256
memJump = (memJump + memAsciiArray(intIndex)) mod 256
intT = (memAsciiArray(intIndex) + memAsciiArray(memJump)) mod 256
memTemp = memAsciiArray(intIndex)
memAsciiArray(intIndex) = memAsciiArray(memJump)
memAsciiArray(memJump) = memTemp
memY = memAsciiArray(intT)
mm2.Byte(intX - 1) = bitwise.bitxor(val("&h" + hex(MM.byte(IntX - 1))), bitwise.bitxor(memTemp,memY))
next

return MM2


Top
 Profile  
Reply with quote  
 Post subject: Re: RC4 Encryption
PostPosted: Wed Dec 22, 2010 9:48 pm 
Offline
User avatar

Joined: Sun Aug 22, 2010 10:27 pm
Posts: 108
edb wrote:
Sorry about the delayed reply; however, here's the source to a method I converted ALONG time ago! Works like a champ! Enjoy! :P

function rc4(strData as string, strKey as string) as string

dim memAsciiArray(255) as integer
dim memKeyArray(255) as integer
dim memJump as integer
dim memTemp as integer
dim memY as integer
dim intKeyLength as integer
dim intIndex as integer
dim intT as integer
dim intX as integer
dim strCData as string

intKeyLength = len(strKey)

for intIndex = 0 to 255
memKeyArray(intIndex) = asc(mid(strKey, ((intIndex) mod (intKeyLength)) + 1, 1))
next

for intIndex = 0 to 255
memAsciiArray(intIndex) = intIndex
next

for intIndex = 0 to 255
memJump = (memJump + memAsciiArray(intIndex) + memKeyArray(intIndex)) mod 256
memTemp = memAsciiArray(intIndex)
memAsciiArray(intIndex) = memAsciiArray(memJump)
memAsciiArray(memJump) = memTemp
next

intIndex = 0
memJump = 0

for intX = 1 to len(strData)
intIndex = (intIndex + 1) mod 256
memJump = (memJump + memAsciiArray(intIndex)) mod 256
intT = (memAsciiArray(intIndex) + memAsciiArray(memJump)) mod 256
memTemp = memAsciiArray(intIndex)
memAsciiArray(intIndex) = memAsciiArray(memJump)
memAsciiArray(memJump) = memTemp
memY = memAsciiArray(intT)
strCData = strCData + chr(bitwise.bitxor(asc(mid(strData, intX, 1)), bitwise.bitxor(memTemp,memY)))
next

return strCData

end function


Thank you, but where is the corresponding decrypt function?

_________________
Zero-Null-Bit Software.
Code Generator for RealBasic: http://rb.znb.cc


Top
 Profile  
Reply with quote  
 Post subject: Re: RC4 Encryption
PostPosted: Thu Dec 23, 2010 4:03 am 
Offline
User avatar

Joined: Mon Apr 02, 2007 2:08 am
Posts: 1225
Location: San Francisco, CA, USA
RC4 is symmetric. That means you use the same function to decrypt as to encrypt.

Here's that RC4 function again, only this time it'll compile :lol:

Function rc4(strData as string, strKey as String) As String
Dim MM as MemoryBlock = strData
Dim MM2 as New MemoryBlock(LenB(strData))
dim memAsciiArray(255) as integer
dim memKeyArray(255) as integer
dim memJump as integer
dim memTemp as integer
dim memY as integer
dim intKeyLength as integer
dim intIndex as integer
dim intT as integer
dim intX as integer

intKeyLength = len(strKey)

for intIndex = 0 to 255
memKeyArray(intIndex) = asc(mid(strKey, ((intIndex) mod (intKeyLength)) + 1, 1))
next

for intIndex = 0 to 255
memAsciiArray(intIndex) = intIndex
next

for intIndex = 0 to 255
memJump = (memJump + memAsciiArray(intIndex) + memKeyArray(intIndex)) mod 256
memTemp = memAsciiArray(intIndex)
memAsciiArray(intIndex) = memAsciiArray(memJump)
memAsciiArray(memJump) = memTemp
next

intIndex = 0
memJump = 0

for intX = 1 to MM2.Size
intIndex = (intIndex + 1) mod 256
memJump = (memJump + memAsciiArray(intIndex)) mod 256
intT = (memAsciiArray(intIndex) + memAsciiArray(memJump)) mod 256
memTemp = memAsciiArray(intIndex)
memAsciiArray(intIndex) = memAsciiArray(memJump)
memAsciiArray(memJump) = memTemp
memY = memAsciiArray(intT)
mm2.Byte(intX - 1) = bitwise.bitxor(val("&h" + hex(MM.byte(IntX - 1))), bitwise.bitxor(memTemp,memY))
next

return MM2
End Function

_________________
Boredom Software


Top
 Profile  
Reply with quote  
 Post subject: Re: RC4 Encryption
PostPosted: Thu Dec 23, 2010 4:25 am 
Offline
User avatar

Joined: Sun Aug 22, 2010 10:27 pm
Posts: 108
Oh, i got it.

Thank you . :mrgreen:

_________________
Zero-Null-Bit Software.
Code Generator for RealBasic: http://rb.znb.cc


Top
 Profile  
Reply with quote  
 Post subject: Re: RC4 Encryption
PostPosted: Tue Jun 14, 2011 12:23 am 
Offline

Joined: Thu Dec 02, 2010 10:17 pm
Posts: 75
Hi,
I actually have been doing some tests on this and im wondering why does it take so long to encrypt larger streams?


Top
 Profile  
Reply with quote  
 Post subject: Re: RC4 Encryption
PostPosted: Tue Jun 14, 2011 12:29 pm 
Offline
User avatar

Joined: Mon Apr 02, 2007 2:08 am
Posts: 1225
Location: San Francisco, CA, USA
Because they're large?


How large are we talking about here?

_________________
Boredom Software


Top
 Profile  
Reply with quote  
 Post subject: Re: RC4 Encryption
PostPosted: Fri May 04, 2012 11:55 am 
Offline

Joined: Mon Dec 12, 2011 7:25 pm
Posts: 186
Location: Grand Rapids MI Area
Being new to Real Studio I have to ask how to Decrypt using RC4

I got it to Encrypt/Encode but can't seem to figure out how to Decrypt/Decode
a string.

Can anyone help with this ?

_________________
iMac 27" (2.8GHz Intel Core i5, 16GB RAM, 1TB Drive) OS X Mountain Lion 10.8.3 and Windows 7
Toshiba Laptop Running Windows 7, WinXP SP3 and Windows 8
REAL Studio Enterprise Edition 2012r2.1
http://www.theultimatecampgroundlog.com
Being rewritten for the Mac


Top
 Profile  
Reply with quote  
 Post subject: Re: RC4 Encryption
PostPosted: Fri May 04, 2012 12:15 pm 
Offline
User avatar

Joined: Mon Apr 02, 2007 2:08 am
Posts: 1225
Location: San Francisco, CA, USA
RC4 is symmetric. That is to say you do precisely the same thing to decrypt as you did to encrypt.

If M is a plaintext message, C is the enciphered version of M, and RC4 is the cipher function, then:

RC4(M) = C
and
RC4(C) = M

_________________
Boredom Software


Top
 Profile  
Reply with quote  
 Post subject: Re: RC4 Encryption
PostPosted: Fri May 04, 2012 2:38 pm 
Offline

Joined: Mon Dec 12, 2011 7:25 pm
Posts: 186
Location: Grand Rapids MI Area
I guess I am missing something because it does not work for me.
Below is the code I've tried and am not sure I have it right.
The RC4 method requires 2 bits of information so I pass it
C and M.

Where am I screwing this up ?

Dim C, M, Data1, Data2 As string
C = "Password"
M = "11111111-22222222"

'This seems to encrypt fine
'Call to RC4 method
Data1 = RC4(M, C)
MsgBox Data1

'This just encrypts it again (not decrypting)
'Call to RC4 method
Data2 = RC4(C, M)
MsgBox Data2

_________________
iMac 27" (2.8GHz Intel Core i5, 16GB RAM, 1TB Drive) OS X Mountain Lion 10.8.3 and Windows 7
Toshiba Laptop Running Windows 7, WinXP SP3 and Windows 8
REAL Studio Enterprise Edition 2012r2.1
http://www.theultimatecampgroundlog.com
Being rewritten for the Mac


Top
 Profile  
Reply with quote  
 Post subject: Re: RC4 Encryption
PostPosted: Fri May 04, 2012 4:22 pm 
Offline

Joined: Mon Dec 12, 2011 7:25 pm
Posts: 186
Location: Grand Rapids MI Area
Found the error of my ways.

Thanks for the reply...

Dim C, M, Data1, Data2 As string
C = "Password"
M = "11111111-22222222"

'This seems to encrypt fine
'Call to RC4 method
Data1 = RC4(M, C)
MsgBox Data1

'This Now Decrypts fine
'Call to RC4 method
Data2 = RC4(Data1, C)
MsgBox Data2

_________________
iMac 27" (2.8GHz Intel Core i5, 16GB RAM, 1TB Drive) OS X Mountain Lion 10.8.3 and Windows 7
Toshiba Laptop Running Windows 7, WinXP SP3 and Windows 8
REAL Studio Enterprise Edition 2012r2.1
http://www.theultimatecampgroundlog.com
Being rewritten for the Mac


Top
 Profile  
Reply with quote  
 Post subject: Re: RC4 Encryption
PostPosted: Mon May 07, 2012 8:45 pm 
Offline
User avatar

Joined: Wed Sep 20, 2006 5:12 pm
Posts: 680
Location: Puerto Rico
you can look to sql to fined the encrypt data in the SqliteDB

_________________
Thanks for any contribution and God blesses all (I start Using RS in 10/12/2010)
Mac Mini with Lion v10.7.5 and Real Studio 2012r1 Pro And Windows 7 RS2012r1 Personal and Linux RS2012r1 Personal Register user
my Hobbies R/C Turbines Jets Models


Top
 Profile  
Reply with quote  
 Post subject: Re: RC4 Encryption
PostPosted: Tue May 08, 2012 1:28 am 
Offline
User avatar

Joined: Mon Jan 02, 2012 1:51 am
Posts: 382
Location: India
Hi jerryab,
jerryab wrote:
Found the error of my ways.

Thanks for the reply...

Dim C, M, Data1, Data2 As string
C = "Password"
M = "11111111-22222222"

'This seems to encrypt fine
'Call to RC4 method
Data1 = RC4(M, C)
MsgBox Data1

'This Now Decrypts fine
'Call to RC4 method
Data2 = RC4(Data1, C)
MsgBox Data2


I have also tried that function. That method working fine with a string. But when I store the "Data1" in a textfield, surprisingly In textfield I got the value different from "Data1".

I can't understand why this things happening. Does anyone have an idea about it?

thanks

_________________
Amitava Karan
Real Studio 2012 Release 1
Windows 7 (32 bit)
Mac OS X Version 10.7


Top
 Profile  
Reply with quote  
 Post subject: Re: RC4 Encryption
PostPosted: Fri May 11, 2012 11:06 am 
Offline

Joined: Thu Dec 02, 2010 10:17 pm
Posts: 75
The text field probably has some invisible characters at the end of the stream that are being encrypted too so it appears to be different.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 16 posts ]  Go to page 1, 2  Next

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group