AES pitón cifrado y descifrado

import sys 
from Crypto.Cipher import AES
from binascii import b2a_hex, a2b_hex
 
class prpcrypt():
    def __init__(self, key):
        self.key = key
        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))
        return plain_text.rstrip('\0')
 
if __name__ == '__main__':
    pc = PrpCrypt('keys')  
    e = pc.encrypt("tests")  
    d = pc.decrypt(e)
    print ('加密:'+ e)
    print ('解密:'+ d)
Publicado 12 artículos originales · ganado elogios 0 · Vistas 716

Supongo que te gusta

Origin blog.csdn.net/weixin_43252204/article/details/104161331
Recomendado
Clasificación