python aes加密和 解密

#encoding:utf-8
from Crypto.Cipher import  AES
import  base64
class prpcrypt():
    def __init__(self, key):
        #这里密钥key 长度必须为16(AES-128)、24(AES-192)、或32(AES-256)Bytes 长度.
        #目前AES-128足够用
        if len(key)<16:
            key=key+(16-len(key))*"\0"
        self.key = key[:16]
        self.mode = AES.MODE_CBC

    #加密函数,若是text不是16的倍数【加密文本text必须为16的倍数!】,那就补足为16的倍数
    def encrypt(self, text):
        cryptor = AES.new(self.key, self.mode, IV=self.key)
        length = 16
        count = len(text)
        add=count % length
        if add:
            text = text + ('\0' * (length-add))
        self.ciphertext = cryptor.encrypt(text)
        #由于AES加密时候获得的字符串不必定是ascii字符集的,输出到终端或者保存时候可能存在问题
        #因此这里统一把加密后的字符串用base64转化
        return base64.b64encode(self.ciphertext)

    #解密后,去掉补足的'\0'用strip() 去掉
    def decrypt(self, text):
        cryptor = AES.new(self.key, self.mode, IV=self.key)
        plain_text = cryptor.decrypt(base64.b64decode(text))
        return plain_text.rstrip('\0')
if __name__ == '__main__':
    key='keys'*10
    pc = prpcrypt(key)
    e = pc.encrypt("8")
    print e
    d = pc.decrypt(e)
    print d
相关文章
相关标签/搜索