rc4 encryption module for android

Monkey Programming Forums/User Modules/rc4 encryption module for android

Sub_Zero(Posted 2013) [#1]
Hi

A small but neat encryption module for android:

Create a folder called "rc4" in the modules folder.

Then name this file "rc4.monkey" and place it there:
format_codebox('
Strict

Private

#If( LANG <> "java" )
#Error "RC4 is currently not available for language '${LANG}'."
#End If

Import "native/rc4.${TARGET}.${LANG}"

Public

Extern

Class Rc4 = "RC4"
Method encrypt:Int[](clearText:String, key:String)
Method decrypt:String(cipherText:Int[], key:String)
End Class
')

Then, create n folder called "native" in the rc4 folder.
Name this file "rc4.android.java" and place it there:

format_codebox('
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

class RC4 {
public int[] encrypt ( String clearText, String key ) {
byte[] clearText_;
byte[] cipherText;
int[] returnText = new int[clearText.length()];
try {
Cipher rc4 = Cipher.getInstance("RC4");
SecretKeySpec rc4Key = new SecretKeySpec(key.getBytes("ASCII"), "RC4");
rc4.init(Cipher.ENCRYPT_MODE, rc4Key);
cipherText = rc4.update(clearText.getBytes("ASCII"));
int counter = 0;
while (counter < cipherText.length) {
returnText[counter] = cipherText[counter];
counter++;
}
return returnText;
} catch (Exception e) { return null; }
}

public String decrypt ( int[] ciphertext, String key ) {
byte[] clearText;
byte[] cipherText = new byte[ciphertext.length];
try {
int counter = 0;
while (counter < ciphertext.length) {
cipherText[counter] = (byte)ciphertext[counter];
counter++;
}
Cipher rc4 = Cipher.getInstance("RC4");
SecretKeySpec rc4Key = new SecretKeySpec(key.getBytes("ASCII"), "RC4");
rc4.init(Cipher.DECRYPT_MODE, rc4Key);
clearText = rc4.update(cipherText);
System.out.println(new String(clearText, "ASCII"));
return new String(clearText, "ASCII");
} catch (Exception e) { return ""; }

}

}
')

There, the module is created...

Now for a example:
format_codebox('
Strict

Import mojo
Import rc4

Function Main:Int()
New Game()
Return 0
End

Class Game Extends App
Field x:Float, y:Float
Field s:Rc4
Field status:Bool
Field ip:String
Field html:String
Field hstring:String
Field text:Int[2]
Field text2:String[]
Field javel:Int[4]
Field return1:Int[]
Field return2:Int[]
Field result:String
Field message:String
Field z:Int
Field db1:Int, db2:Int

Method OnCreate:Int()
x = 100
y = 100
SetUpdateRate(60)
s = New Rc4()
return1 = s.encrypt("We are crypting this text...", "123456789")
hstring = IntArrayToHexString(return1)
return2 = HexStringToIntArray(hstring)
html = s.decrypt(return2, "123456789")
Return 0
End

Method OnUpdate:Int()
If KeyHit(KEY_ESCAPE) 'press back button
Error "" 'quit application
End
Return 0
End

Method OnRender:Int()
Cls
DrawText(ip, 0, 0)
DrawText(hstring, 0, 20)
DrawText(html, 0, 40)
DrawText(result, 0, 60)
Return 0
End

Function IntArrayToHexString:String(i:Int[])
Local counter:Int
Local outString:String
Local tmpString:String
While counter < i.Length
tmpString = ToHex(i[counter])
outString += tmpString[6..8]
' outString += tmpString[7]
counter += 1
End
Return outString
End
Function HexStringToIntArray:Int[](str:String)
Local counter:Int
Local counter2:Int
Local outInt:Int[(str.Length/2)]
Local tmpInt:Int
While counter < (str.Length)
tmpInt = HexToDec(str[counter..(counter+2)])
outInt[counter2] = tmpInt
counter += 2
counter2 += 1
End
Return outInt
End

Function ToHex:String(i:Int)

''p=32-bit
Local r%=i, s%, p%=32, n:Int[p/4+1]

While (p>0)

s = (r&$f)+48
If s>57 Then s+=7

p-=4
n[p Shr 2] = s
r = r Shr 4

Wend

Return String.FromChars(n)

End

Function HexToDec:Int( Hex:String )
Local Value:Int = 0
Local Char:Int =0

If Hex.Length() <8
'add 0's to beginning of thing so FF becomes 000000FF
Local TempHex:String = ""
For Char =Hex.Length() To 7
TempHex += "0"
Next
TempHex += Hex
Hex = TempHex
Endif

Local Conversion:Int = 1 'starts at 1 then 16, 256, 4096,
'now the characters are 'eight bytes' now begin conversion
For Char=7 To 0 Step -1
If Hex[ Char ]<58
Value += ( ( Hex[ Char ] -48 ) *Conversion )
Else
Value += ( ( Hex[ Char ] -55 ) *Conversion )
Endif
Conversion *= 16 'multiply conversion by 16 for next byte
Next

Return Value
End
End
')


Enjoy :)


ImmutableOctet(SKNG)(Posted 2013) [#2]
A long while back RepeatUntil implemented a simple RC4 function in BlitzMax(You can find it in the BlitzMax code archives), here's a very direct port of it:

This function is similar to the pseudo-code implementation on wikipedia.
format_codebox('
' Not exactly efficient, but here it is:
Function RC4:String(inp:String, key:String)
Local S:Int[256]
Local K:Int[256]

Local i:Int, j:Int, t:Int, temp:Int, y:Int
Local Output:String

For i = 0 To 255
S[i] = i
Next

j = 1

For i = 0 To 255
If (j > key.Length()) Then
j = 1
Endif

K[i] = Asc(Mid(key,j,1))
j += 1
Next

j = 0

For i = 0 To 255
j = ( j + S[i] + K[i] ) & 255

temp = S[i]
S[i] = S[j]
S[j] = temp
Next

i = 0
j = 0

For Local x:Int = 1 To inp.Length()
i = (i + 1) & 255
j = (j + S[i]) & 255
temp = S[i]
S[i] = S[j]
S[j] = temp
t = (S[i] + (S[j] & 255)) & 255
y = S[t]

Output += Chr(Asc(Mid(inp,x,1)) ~ y)
Next

Return Output
End Function
')

If anyone's interested in using this, you'll need to either modify the function, or use a wrapper for BlitzMax's procedural string-commands (Which came from Blitz Basic); I made Retro Strings a while ago, and it should do the job.

I use this myself (Well, more or less), and the only thing you really need to know is that this function both encrypts, and decrypts.

RC4 altogether is a somewhat basic, and fast encryption method; however, it has been cracked before, so you shouldn't rely on it too heavily. However, if you just want a quick and easy encryption method for strings, this'll work just fine.


Sub_Zero(Posted 2013) [#3]
A question: How and where was it cracked? :)

I thought it was somewhat secure, except how it was used in WEP encryption. Atleast the wikipedia page suggest so.

Just curious :)

Wikipedia: It is especially vulnerable when the beginning of the output keystream is not discarded, or when nonrandom or related keys are used; some ways of using RC4 can lead to very insecure cryptosystems such as WEP


http://en.wikipedia.org/wiki/Rc4

By the way: it has proved to be very secure in WPA.

EDIT: I removed something I thought was right, but was wrong....

Actually WPA and TLS isn't too secure anymore due to new attacks from 2013.

So without discarding the first bytes of the keystream and randomly padding the cleartext before encryption, rc4 won't be very secure. <-----EDIT! When it comes to using it as networking encryption where very much data is being transmitted back and forth...

When it comes to one-time encryptions (like encrypting your game-data or some piece of text) it is actually extremely secure!