Browse files

add xor sample

  • Loading branch information...
1 parent 24873b9 commit a2d9cb707e2b2514cbfa29edd2b05273af4b2b6d @stanfeldman committed Apr 20, 2012
Showing with 30 additions and 0 deletions.
  1. +30 −0 python/crypto1.py
View
30 python/crypto1.py
@@ -0,0 +1,30 @@
+import binascii
+
+def str_to_bin(s):
+ return bin(int(binascii.hexlify(s), 16))[2:]
+
+def bin_to_str(s):
+ n = int("0b"+s, 2)
+ return binascii.unhexlify('%x' % n)
+
+def xor(s1_b, s2_b):
+ if len(s1_b) > len(s2_b):
+ s2_b += "0"*(len(s1_b) - len(s2_b))
+ else:
+ s1_b += "0"*(len(s2_b) - len(s1_b))
+ res = ""
+ for i in xrange(len(s1_b)):
+ res += str(int(s1_b[i]) ^ int(s2_b[i]))
+ return res
+
+def encode(message, key):
+ return xor(str_to_bin(message), str_to_bin(key))
+
+def decode(cipher, key):
+ return bin_to_str(xor(cipher, str_to_bin(key)))
+
+message = "hello, stas!"
+key = "secret"
+cipher = encode(message, key)
+message2 = decode(cipher, key)
+print message == message2

0 comments on commit a2d9cb7

Please sign in to comment.