2016-03-21 83 views
-1

我在想,如果這樣的例子,我可以加密的原始數據:如何加密和解密方法文本在Python

message = encrypt(raw_input("what is your message")) <---- I want this encrypted 

然後在我的服務器代碼,我希望它加密:

print unencrypt(str(message)) <----- I want this 

我想要它與此類似。我如何在Python 2.7上做到這一點?

回答

0

您可以使用下面的代碼:

import base64 
def encode(key, clear): 
    enc = [] 
    for i in range(len(clear)): 
     key_c = key[i % len(key)] 
     enc_c = chr((ord(clear[i]) + ord(key_c)) % 256) 
     enc.append(enc_c) 
    return base64.urlsafe_b64encode("".join(enc)) 

def decode(key, enc): 
    dec = [] 
    enc = base64.urlsafe_b64decode(enc) 
    for i in range(len(enc)): 
     key_c = key[i % len(key)] 
     dec_c = chr((256 + ord(enc[i]) - ord(key_c)) % 256) 
     dec.append(dec_c) 
    return "".join(dec) 

encoded_string=encode('password','text text') 
print encoded_string 
decoded_string=decode('password',encoded_string) 
print decoded_string