首先python引用AES加密
from Crypto.Cipher import AES
需要先安装 Crypto 模块, 可以使用 easy_install 进行安装 会自动去官网进行搜索安装
其中代码示例:
aes 加密 需要进行加密数据的处理,要求数据长度必须是16的倍数,不足时,在后边补0
class MyCrypt(): def __init__(self, key): self.key = key self.mode = AES.MODE_CBC def myencrypt(self, text): length = 16 count = len(text) print count if count < length: add = length - count text= text + (‘\0‘ * add) elif count > length: add = (length -(count % length)) text= text + (‘\0‘ * add) cryptor = AES.new(self.key, self.mode, self.key) self.ciphertext = cryptor.encrypt(text) return b2a_hex(self.ciphertext) def mydecrypt(self, text): cryptor = AES.new(self.key, self.mode, self.key) plain_text = cryptor.decrypt(text) return plain_text.rstrip(‘\0‘)
关于base64 加密 比较简单
import base64 即可
>>> dir(base64) [‘EMPTYSTRING‘, ‘MAXBINSIZE‘, ‘MAXLINESIZE‘, ‘__all__‘, ‘__builtins__‘, ‘__doc__‘, ‘__file__‘, ‘__name__‘, ‘__package__‘, ‘_b32alphabet‘, ‘_b32rev‘, ‘_b32tab‘, ‘_translate‘, ‘_translation‘, ‘_urlsafe_decode_translation‘, ‘_urlsafe_encode_translation‘, ‘_x‘, ‘b16decode‘, ‘b16encode‘, ‘b32decode‘, ‘b32encode‘, ‘b64decode‘, ‘b64encode‘, ‘binascii‘, ‘decode‘, ‘decodestring‘, ‘encode‘, ‘encodestring‘, ‘k‘,‘re‘, ‘standard_b64decode‘, ‘standard_b64encode‘, ‘string‘, ‘struct‘, ‘test‘, ‘test1‘, ‘urlsafe_b64decode‘, ‘urlsafe_b64encode‘, ‘v‘]
其中有 urlsafe 加密解密 , 可以不用在base64基础上进行再次处理。
时间: 2024-10-11 06:24:35