import base64
from urllib import unquote


def xor_decode(clear_text, cypher):
    key = []
    
    for c1, c2 in zip(clear_text, cypher):
        #print c1 , " : ", c2
        key.append(chr (ord(c1) ^ ord(c2) ))
    
    key_str = "".join(str(x) for x in  key)
    
    #list1 = [1 , 2 , 3]
    #list2 = [4 , 5]

    #for n1 , n2 in zip(list1,list2):
       #print n1 , " : ", n2
    return key_str

def xor_encode(clear_text, key):
    cypher = ""
    key_length = len(key)
    text_length = len(clear_text)
    index = 0 
    key_list = list(key)
     
    for ch in clear_text : 
        #print ch , " ^ ", key_list[index % key_length], " = ", chr(ord(ch) ^ ord(key_list[index % key_length] ) ),  index 
        cypher = cypher + chr (ord(ch) ^ ord(key_list[index % key_length]) )
        index = index + 1
        
    #print
    
    return cypher

url = 'ClVLIh4ASCsCBE8lAxMacFMZV2hdVVotEhhUJQNVAmhSEV4sFxFeaAw%3D'
print "Length of URL : ", len(url) 
clear_text ='{"showpassword":"no","bgcolor":"#ffffff"}'
print "Length of Base64 : ", len( unquote(url).decode('utf8') ) 
cypher = base64.b64decode (unquote(url).decode('utf8')) 
print "Length of Base64 Decode : ", len(cypher) 
print "Length of clear_text : ", len(clear_text) 

key = xor_decode(clear_text, cypher)
print key
print "----------------------------------------------------"
clear_text ='{"showpassword":"yes","bgcolor":"#ffffff"}'
print " clear_text length = " , len(clear_text)
print " key length = " , len(key)
print "----------------------------------------------------"

print base64.b64encode(xor_encode(clear_text, key ))
#cypher_1 = xor_encode(clear_text,key)

key = "qw8J"
print base64.b64encode(xor_encode(clear_text, key ))
#cypher_2 = xor_encode(clear_text,key)

#if cypher_1 == cypher_2:
#    print "equal"
#else :
#    print "false"

