title: RC4 encryption java
body: <p>Hi there I am trying to implement the RC4 algorithm in Java. I found this <a href="http://code.google.com/p/a9cipher/source/browse/src/com/a9development/a9cipher/RC4.java?r=1b5bdf2a70b3ef8cc9464f8a31332a2ec9f54e7a" rel="noreferrer">code</a> as an example that help me to understand the idea:</p>
  
  <pre><code>public class RC4 {
    private int[] S = new int[256];
    private int[] T = new int[256];
    private int keylen;
  
    public RC4(byte[] key) throws Exception {
      if (key.length &lt; 1 || key.length &gt; 256) {
        throw new Exception("key must be between 1 and 256 bytes");
      } else {
        keylen = key.length;
        for (int i = 0; i &lt; 256; i++) {
          S[i] = i;
          T[i] = key[i % keylen];
        }
        int j = 0;
        for (int i = 0; i &lt; 256; i++) {
          j = (j + S[i] + T[i]) % 256;
          S[i] ^= S[j];
          S[j] ^= S[i];
          S[i] ^= S[j];
        }
      }
    }
  
    public int[] encrypt(int[] plaintext) {
      int[] ciphertext = new int[plaintext.length];
      int i = 0, j = 0, k, t;
      for (int counter = 0; counter &lt; plaintext.length; counter++) {
        i = (i + 1) % 256;
        j = (j + S[i]) % 256;
        S[i] ^= S[j];
        S[j] ^= S[i];
        S[i] ^= S[j];
        t = (S[i] + S[j]) % 256;
        k = S[t];
        ciphertext[counter] = plaintext[counter] ^ k;
      }
      return ciphertext;
    }
  
    public int[] decrypt(int[] ciphertext) {
      return encrypt(ciphertext);
    }
  }
  </code></pre>
  
  <p>I have few question:</p>
  
  <ol>
  <li><p>Why is the plain-text an <code>int</code> array in the above code?</p></li>
  <li><p>When I test this code I get strange result, can somebody explain to me? Here my code to test:</p>
  
  <pre><code>public class RC4_Main {
  
      public static void main(String args[]) throws Exception {
          String keyword = "hello";
          byte[] keytest = keyword.getBytes(); //convert keyword to byte
  
          int[] text = {1, 2, 3, 4, 5}; // text as 12345
  
          RC4 rc4 = new RC4(keytest);
  
          System.out.print("\noriginal text: ");
          for (int i = 0; i &lt; text.length; i++) {          
              System.out.print(text[i]);          
          }    
  
          int[] cipher = rc4.encrypt(text); //encryption      
          System.out.print("\ncipher: ");
          for (int i = 0; i &lt; cipher.length; i++) {          
              System.out.print(cipher[i]);          
          }    
  
          int[] backtext = rc4.decrypt(cipher); //decryption
          System.out.print("\nback to text: ");
          for (int i = 0; i &lt; backtext.length; i++) {          
              System.out.print(backtext[i]);            
          } 
          System.out.println();
      }
  }
  </code></pre></li>
  </ol>
  
  <p>Here is the result: (original and back to text are not SAME) why???</p>
  
  <pre><code>original text: 12345
  cipher: 1483188254174
  back to text: 391501310217
  </code></pre>