AES加密代码:
输入的加密字符必须是16的倍数,php的默认补零,解密时需要rtrim掉零。
python没有自动做这件事情,所以要自己补零。
专题教程:
AES加密解密算法与实现代码
python aes加密解密算法与模块用法教程
1、python版本 实现AES加密。
 
复制代码 代码示例:
#!/usr/bin/env python
#
class MyCrypt():
    def __init__(self, key):
        self.key = key
        self.mode = AES.MODE_CBC
        self.padding = ' '
 
    def encrypt(self, text):
        cryptor = AES.new(self.key, self.mode)
        length = 16
        count = text.count('')
        if count < length:
            add = (length - count) + 1
            text += (self.padding * add)
        elif count > length:
            add = (length - (count % length)) + 1
            text += (self.padding * add)
        self.ciphertext = cryptor.encrypt(text)
        return self.ciphertext
 
    def decrypt(self, text):
        cryptor = AES.new(self.key, self.mode)
        plain_text = cryptor.decrypt(text)
        return plain_text.rstrip("