欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python aes加解密

程序员文章站 2024-03-14 18:18:40
...
#coding: utf8
import sys
from Crypto.Cipher import AES
from binascii import b2a_hex, a2b_hex
from const import *
 
class AESHelper():
    def __init__(self):
        self.key = encode_secret
        self.mode = AES.MODE_CBC
     
    def encrypt(self, text):
        cryptor = AES.new(self.key, self.mode, self.key)
        length = 16
        count = len(text)
	if(count % length != 0) :
        	add = length - (count % length)
	else:
		add = 0
        text = text + ('\0' * add)
        self.ciphertext = cryptor.encrypt(text)
        return b2a_hex(self.ciphertext)
     
    def decrypt(self, text):
        cryptor = AES.new(self.key, self.mode, self.key)
        plain_text = cryptor.decrypt(a2b_hex(text))
        string = plain_text.rstrip('\0')
        result = ''
        strings = string.split(encode_salt)
        if len(strings) > 0:
            result = strings[0]
        return result