package rc4.generator;
public class RC4_Generator {
private int[] S;
private int[] T;
private int keylen;
public RC4_Generator(byte[] key) throws Exception
{
if (key.length < 1 || key.length > 256)
throw new Exception("The key should be between 1 to 256 Byte!");
else
// The initialization of both S[] and T[].
{
keylen = key.length;
for (int i = 0; i < 256; i++) {
S[i] = i;
T[i] = key[i % keylen];
}
// The S permutation.
int j = 0;
for (int i = 0; i < 256; i++)
{
j = (j + S[i] + T[i]) % 256;
S[i] = S[i] ^ S[j];
S[j] = S[j] ^ S[i];
S[i] = S[i] ^ S[j];
}
}
}
public int[] encryption(int[] plaintext)
{
int[] ciphertext = new int[plaintext.length];
int i = 0, j = 0, k, t;
for (int c = 0; c < plaintext.length; c++) {
i = (i + 1) % 256;
j = (j + S[i]) % 256;
S[i] = S[i] ^ S[j];
S[j] = S[j] ^ S[i];
S[i] = S[i] ^ S[j];
t = (S[i] + S[j]) % 256;
k = S[t];
//ciphertext[c] = plaintext[c] ^ k;
}
return ciphertext;
}
public static void main(String[] args){
byte[] k ={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30};
RC4_Generator rc4= new RC4_Generator(k);
}
}