Advertisement
opexxx

xor.py

May 22nd, 2015
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.72 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: UTF-8 -*-
  3.  
  4.  
  5. string = "something something something"
  6. key = "cheese"
  7.  
  8. def crypt(string, key):
  9.     data = ''
  10.     for i in range(len(string)):
  11.         # XOR
  12.         xc = ord(string[i]) ^ ord(key[i%len(key)])
  13.         # Convert XOR to base 2
  14.         xc = bin(xc)[2:].zfill(8)
  15.         data += str( xc )
  16.  
  17.         #print string[i]
  18.     return data
  19.  
  20. def decrypt(string, key):
  21.     data = ''
  22.     for i in range( (len(string)+1) / 8):
  23.         # Convert each 8 bits to base 10
  24.         x = int(  string[((i+1)*8)-8:(i+1)*8], 2  )
  25.         # XOR with key
  26.         data += chr(  x ^ ord(key[i%len(key)])  )
  27.     return data
  28.  
  29. print "String: %s - Key: %s \n \n%s\n" % (string, key, "="*10)
  30.  
  31. x = crypt(string, key)
  32. print x + "\n"
  33.  
  34. y= decrypt(x, key)
  35. print y + "\n"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement