Python加密AES

安装

pycrypto、pycrytodome和crypto是一个东西,crypto在python上面的名字是pycrypto,它是一个第三方库,但是已经停止更新三年了,所以不建议安装这个库;
这个时候pycryptodome就来了,它是pycrypto的延伸版本,用法和pycrypto是一模一样的;
所以,我现在告诉大家一种解决方法–直接安装:pip install pycryptodome
但是,在使用的时候导入模块是有问题的,这个时候只要修改一个文件夹的名称就可以完美解决这个问题,
将文件夹叫做crypto,将小写c改成大写C就ok了。
参考:windows下安装crypto

from Crypto.Cipher import AES
from binascii import b2a_hex, a2b_hex



class BaseCrypt(object):

    def __init__(self, *args, key=None, **kwargs):
        self.key = key

    def encrypt(self, text):
        raise NotImplementedError

    def decrypt(self, text):
        raise NotImplementedError


class AESCrypt(BaseCrypt):

    def __init__(self, key=None):
        super().__init__(key=key)
        self.key = key if key else '4141863181f34d80'
        self.key = self.key.encode('utf-8')
        self.mode = AES.MODE_CBC

    # 加密函数,如果text不足16位就用空格补足为16位,
    # 如果大于16当时不是16的倍数,那就补足为16的倍数。
    def encrypt(self, text):
        if not text:
            text = ''
        if not isinstance(text, str):
            text = str(text)
        text = text.encode('utf-8')
        cryptor = AES.new(self.key, self.mode, b'0000000000000000')
        # 这里密钥key 长度必须为16(AES-128),
        # 24(AES-192),或者32 (AES-256)Bytes 长度
        # 目前AES-128 足够目前使用
        length = 16
        count = len(text)
        if count < length:
            add = (length - count)
            text = text + ('\0' * add).encode('utf-8')
        elif count > length:
            add = (length - (count % length))
            text = text + ('\0' * add).encode('utf-8')
        self.ciphertext = cryptor.encrypt(text)
        # 因为AES加密时候得到的字符串不一定是ascii字符集的,输出到终端或者保存时候可能存在问题
        # 所以这里统一把加密后的字符串转化为16进制字符串
        return str(b2a_hex(self.ciphertext), encoding='utf-8')

    # 解密后,去掉补足的空格用strip() 去掉
    def decrypt(self, text):
        if type(text) == str:
            text = bytes(text, 'utf-8')
        cryptor = AES.new(self.key, self.mode, b'0000000000000000')
        plain_text = cryptor.decrypt(a2b_hex(text))
        return bytes.decode(plain_text).rstrip('\0')

if __name__ == '__main__':
    aes = AESCrypt()
    a = aes.encrypt('123')

    b = aes.decrypt(a)
    print(123)

发布了23 篇原创文章 · 获赞 0 · 访问量 354

猜你喜欢

转载自blog.csdn.net/Aerkui/article/details/104058887
今日推荐