#----------------------------------------- # # Swap Values # # (primitive Way for some Kind of ANSI-C # Compatibility when comparing the Listing) # def swap(X,Y): return Y, X # ----------------------------------------- # # Global Varaiable Definition # KeyWord = bytearray(256) Nonce = bytearray(256) KeyLen = int(0) NonceLen = int(0) # Secret State Arrays z = bytearray(256) x = bytearray(256) # Global Carry on Array Indices a = int(0) b = int(0) #----------------------------------------- # # RC4 Reference Implemantation # def KSA(): global KeyWord, KeyLen global z, x, a, b i = int(0) # Prefill the Arrays for i in range(256): x[i] = i; k = j = int(0) for i in range(256): k = (i % KeyLen) j = (j + x[i] + KeyWord[k]) % 256 x[i], x[j] = swap(x[i], x[j]) a = b = int(0) def PRGA(): global a, b t = int(0) # Update the global Carry on Array Indices a = (a + 1) % 256 b = (b + x[a]) % 256 x[a], x[b] = swap(x[a], x[b]) # XOR Plaintext/Ciphertext t = (x[a] + x[b]) % 256 return x[t] # # /*------------------------------------------------------------------ # # RC4 Test vectors # #------------------------------------------------------------------ # Key Keystream Plaintext Ciphertext # -------------------------------------------------------------------- # Key EB9F7781B734CA72A719... Plaintext BBF316E8D940AF0AD3 # # Wiki 6044DB6D41B7... pedia 1021BF0420 # # Secret 04D46B053CA87B59... Attack at dawn 45A01F645FC35B383552544B9BF5 # -------------------------------------------------------------------*/ #