[JAVA - Method]RC4 Encryption 02-13-2012, 05:50 PM
#1
I took an open source VB.net RC4 module and well. Converted it into a Java method. (for those of you who dont know java, FUNCTIONS are called METHODS in java)
To use this, just C/P this method into another class(if you want to use object orientation). If not, then just c/p it into your main class and then call if like this.
Here is the code for the encryption![Smile Smile](https://sinister.ly/images/smilies/set/smile.png)
To use this, just C/P this method into another class(if you want to use object orientation). If not, then just c/p it into your main class and then call if like this.
Code:
rc4("message, variable or whatever you want encrypted here", "the password you want it to protect over");
Here is the code for the encryption
![Smile Smile](https://sinister.ly/images/smilies/set/smile.png)
Code:
public static String rc4(String message, String password) {
int i = 0;
int j = 0;
StringBuilder cipher = new StringBuilder();
String returnCipher = "";
int[] sbox = new int[257];
int[] key = new int[257];
int intLength = password.length();
int a = 0;
while (a <= 255) {
char ctmp = (password.substring((a % intLength), (a % intLength) + 1).toCharArray()[0]);
key[a] = Integer.valueOf(ctmp);
sbox[a] = a;
Math.max(System.Threading.Interlocked.Increment(a), a - 1);
}
int x = 0;
int b = 0;
while (b <= 255) {
x = (x + sbox[b] + key[b]) % 256;
int tempSwap = sbox[b];
sbox[b] = sbox[x];
sbox[x] = tempSwap;
Math.max(System.Threading.Interlocked.Increment(b), b - 1);
}
a = 1;
while (a <= message.length()) {
int itmp = 0;
i = (i + 1) % 256;
j = (j + sbox[i]) % 256;
itmp = sbox[i];
sbox[i] = sbox[j];
sbox[j] = itmp;
int k = sbox[(sbox[i] + sbox[j]) % 256];
char ctmp = message.substring(a - 1, a - 1 + 1).toCharArray()[0];
itmp = Integer.valueOf(ctmp);
int cipherby = itmp ^ k;
cipher.append((char)(cipherby));
Math.max(System.Threading.Interlocked.Increment(a), a - 1);
}
returnCipher = cipher.toString();
cipher.setLength(0);
return returnCipher;
}