title: RC4 decryption in python hexadecimal inputs
body: <p>I have a program that successfully encrypts a piece of plain text information and outputs the encrypted data as hexadecimal. i can decrypt this data on <a href="http://www.fyneworks.com/encryption/rc4-encryption/index.asp" rel="nofollow">http://www.fyneworks.com/encryption/rc4-encryption/index.asp</a> by putting in the hex data i received from my program and the key i used (in plain text)</p>
  
  <p>the next step i am trying to accomplish is to have two inputs where i can input an encrypted message in hex and the encryption key (also in hex).</p>
  
  <p>i'm having trouble getting the decryption part of my program to work...</p>
  
  <p>If i enter the key as <code>password</code> and the ciphertext as <code>8c905b7c294a94c30422d81d552e</code> which successfully decrypts on the website above... it doesn't work.</p>
  
  <p>anyone have any ideas how i can get decryption working in RC4 with hexadecimal inputs?</p>
  
  <pre><code># Global variables
  state = [None] * 256
  p = q = None
  
  def setKey(key):
     ##RC4 Key Scheduling Algorithm (KSA)
      global p, q, state
      state = [n for n in range(256)]
      p = q = j = 0
      for i in range(256):
          if len(key) &gt; 0:
              j = (j + state[i] + key[i % len(key)]) % 256
          else:
              j = (j + state[i]) % 256
          state[i], state[j] = state[j], state[i]
  
  def byteGenerator():
      ##RC4 Pseudo-Random Generation Algorithm (PRGA)
      global p, q, state
      p = (p + 1) % 256
      q = (q + state[p]) % 256
      state[p], state[q] = state[q], state[p]
      return state[(state[p] + state[q]) % 256]
  
  def encrypt(key,inputString):
      ##Encrypt input string returning a byte list
      setKey(string_to_list(key))
      return [ord(p) ^ byteGenerator() for p in inputString]
  
  def decrypt(inputByteList):
      ##Decrypt input byte list returning a string
  
      return "".join([chr(c ^ byteGenerator()) for c in inputByteList])
  
  
  
  def intToList(inputNumber):
      ##Convert a number into a byte list
      inputString = "{:02x}".format(inputNumber)
      return [int(inputString[i:i + 2], 16) for i in range(0, len(inputString), 2)]
  
  def string_to_list(inputString):
      ##Convert a string into a byte list
      return [ord(c) for c in inputString]
  
  key = raw_input("Enter Key: ")
  ciphertext = raw_input("enter ciphertext: ")
  print decrypt(intToList(ciphertext))
  </code></pre>