title: RC4 decryption with key in Python
body: <p>I pulled this code from here for asp <a href="http://bytes.com/topic/access/insights/906671-rc4-encryption-algorithm-vba-vbscript" rel="nofollow">http://bytes.com/topic/access/insights/906671-rc4-encryption-algorithm-vba-vbscript</a>, which i then run thru base64.</p>
  
  <p>I am wonder if anyone can help me figure out how to write the decryption piece but in Python. As the decryption will happen on my Python Server Page.</p>
  
  <p>Found this <a href="http://www.id-snippet.com/20801/python-rc4-cipher/" rel="nofollow">http://www.id-snippet.com/20801/python-rc4-cipher/</a>, but it doesn't decrypt the RC4 asp from the first link.</p>
  
  <p>-Jim</p>
  
  <p>%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  ASP page</p>
  
  <pre><code>'Base64
  Function Base64Encode(inData)
  Const Base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
  Dim cOut, sOut, I
  
  'For each group of 3 bytes
  For I = 1 To Len(inData) Step 3
      Dim nGroup, pOut, sGroup
  
  'Create one long from this 3 bytes.
  nGroup = &amp;H10000 * Asc(Mid(inData, I, 1)) + _
    &amp;H100 * MyASC(Mid(inData, I + 1, 1)) + MyASC(Mid(inData, I + 2, 1))
  
  'Oct splits the long To 8 groups with 3 bits
  nGroup = Oct(nGroup)
  
  'Add leading zeros
  nGroup = String(8 - Len(nGroup), "0") &amp; nGroup
  
  'Convert To base64
  pOut = Mid(Base64, CLng("&amp;o" &amp; Mid(nGroup, 1, 2)) + 1, 1) + _
      Mid(Base64, CLng("&amp;o" &amp; Mid(nGroup, 3, 2)) + 1, 1) + _
      Mid(Base64, CLng("&amp;o" &amp; Mid(nGroup, 5, 2)) + 1, 1) + _
      Mid(Base64, CLng("&amp;o" &amp; Mid(nGroup, 7, 2)) + 1, 1)
  'Add the part To OutPut string
  sOut = sOut + pOut
  
  'Add a new line For Each 76 chars In dest (76*3/4 = 57)
  'If (I + 2) Mod 57 = 0 Then sOut = sOut + vbCrLf
  Next
  Select Case Len(inData) Mod 3
  Case 1: '8 bit final
    sOut = Left(sOut, Len(sOut) - 2) + "=="
  Case 2: '16 bit final
    sOut = Left(sOut, Len(sOut) - 1) + "="
  End Select
  Base64Encode = sOut
  End Function
  
  Function MyASC(OneChar)
    If OneChar = "" Then MyASC = 0 Else MyASC = Asc(OneChar)
  End Function
  
  'RC4
  Function RunRC4(sMessage, strKey)
  Dim kLen, x, y, i, j, temp
  Dim s(256), k(256)
  'Init keystream
  klen = Len(strKey)
  For i = 0 To 255
      s(i) = i
      k(i) = Asc(Mid(strKey, (i Mod klen) + 1, 1))
  Next
  j = 0
  For i = 0 To 255
      j = (j + k(i) + s(i)) Mod 255
      temp = s(i)
      s(i) = s(j)
      s(j) = temp
  Next
  'Drop n bytes from keystream
  x = 0
  y = 0
  For i = 1 To 3072
      x = (x + 1) Mod 255
      y = (y + s(x)) Mod 255
      temp = s(x)
      s(x) = s(y)
      s(y) = temp
  Next
  'Encode/Decode
  For i = 1 To Len(sMessage)
      x = (x + 1) Mod 255
      y = (y + s(x)) Mod 255
      temp = s(x)
      s(x) = s(y)
      s(y) = temp
      RunRC4 = RunRC4 &amp; Chr(s((s(x) + s(y)) Mod 255) Xor Asc(Mid(sMessage, i, 1)))
  Next
  End Function
  encStr = Base64Encode(RunRC4(username,"1234"))
  </code></pre>
  
  <p>Python Server Page</p>
  
  <pre><code> def decode64(in_str):
      import base64
       decodedStr = base64.b64decode(in_str)
   return decodedStr
  
  def rc4crypt(data, key):
      x = 0
      box = range(256)
      for i in range(256):
          x = (x + box[i] + ord(key[i % len(key)])) % 256
          box[i], box[x] = box[x], box[i]
      x,y = 0, 0
      out = []
      for char in data:
          x = (x + 1) % 256
          y = (y + box[x]) % 256
          box[x], box[y] = box[y], box[x]
          out.append(chr(ord(char) ^ box[(box[x] + box[y]) % 256]))
      return ''.join(out)
   decStr = rc4crypt(decode64(encStr), "1234")
  </code></pre>
  
  <p>Can't figure out why python decrypt doesn't render the original string when using the same key "1234"</p>