python XOR encrypt/decrypt

pylist 617

python XOR encrypt/decrypt

#
# NB : this is not secure
# from http://code.activestate.com/recipes/266586-simple-xor-keyword-encryption/
# added base64 encoding for simple querystring :)
#

def xor_crypt_string(data, key='awesomepassword', encode=False, decode=False):
    from itertools import izip, cycle
    import base64
    if decode:
        data = base64.decodestring(data)
    xored = ''.join(chr(ord(x) ^ ord(y)) for (x,y) in izip(data, cycle(key)))
    if encode:
        return base64.encodestring(xored).strip()
    return xored


secret_data = "239054"
print xor_crypt_string(secret_data, encode=True)
print xor_crypt_string(xor_crypt_string(secret_data, encode=True), decode=True)

出自 http://code.activestate.com/recipes/266586-simple-xor-keyword-encryption/

另一段:

# -*- coding:utf-8 -*-

from pyDes import triple_des, ECB, PAD_PKCS5
import base64
import datetime

input_str = "test string"

key_base64_str = base64.b64decode("NDg5MDY2NjczMxxxxXXXXXyNzUzNTg22", "utf-8")
key_bytes = key_base64_str.encode('utf-8')
k = triple_des(key_bytes, ECB, pad=None, padmode=PAD_PKCS5)
d = base64.b64encode(k.encrypt(input_str))
print d

登录发表评论