Advertisement
rijarob

ARC4 FC XML

May 15th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. # Thanks, Wikipedia: http://en.wikipedia.org/wiki/RC4#Implementation
  2. class ARC4:
  3. def __init__(self, key = None):
  4. self.state = range(256) # Initialize state array with values 0 .. 255
  5. self.x = self.y = 0 # Our indexes. x, y instead of i, j
  6.  
  7. if key is not None:
  8. self.init(key)
  9.  
  10. # KSA
  11. def init(self, key):
  12. for i in range(256):
  13. self.x = (ord(key[i % len(key)]) + self.state[i] + self.x) & 0xFF
  14. self.state[i], self.state[self.x] = self.state[self.x], self.state[i]
  15. self.x = 0
  16.  
  17. # PRGA
  18. def crypt(self, input):
  19. output = [None]*len(input)
  20. for i in xrange(len(input)):
  21. self.x = (self.x + 1) & 0xFF
  22. self.y = (self.state[self.x] + self.y) & 0xFF
  23. self.state[self.x], self.state[self.y] = self.state[self.y], self.state[self.x]
  24. r = self.state[(self.state[self.x] + self.state[self.y]) & 0xFF]
  25. output[i] = chr(ord(input[i]) ^ r)
  26. return ''.join(output)
  27.  
  28. @classmethod
  29. def from_crypted_str(self, data_str, crypt_key):
  30. a = ARC4(crypt_key)
  31. return FoxyData.from_str(a.crypt(data_str))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement