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

Python调用模块实现AES加解密

程序员文章站 2022-09-14 08:31:36
测试输入:plaintext1234567 1234567891234567预期输出:b’e232fb646d5d820620ffa609ba56200a’b’plaintext1234567’测试输入:1234567891234567 plaintext1234567预期输出:b’a49e9a7c354a9f64486b16e601fdccd7’b’1234567891234567’在这里插入代码片from Crypto.Cipher import AESfrom binascii...

测试输入:
plaintext1234567 1234567891234567
预期输出:
b’e232fb646d5d820620ffa609ba56200a’
b’plaintext1234567’

测试输入:
1234567891234567 plaintext1234567
预期输出:
b’a49e9a7c354a9f64486b16e601fdccd7’
b’1234567891234567’

在这里插入代码片
from Crypto.Cipher import AES
from binascii import b2a_hex, a2b_hex

#ECB模式
class aestest():
#***********Begin**************

    def __init__(self, key):
        self.key = key.encode('utf-8')
        self.mode = AES.MODE_ECB
    def en(self, text):
        ptor = AES.new(self.key, self.mode)
        text = text.encode('utf-8')
        self.ciphertext = ptor.encrypt(text)
        text = b2a_hex(self.ciphertext)
        return text
    def de(self, text):
        ptor = AES.new(self.key, self.mode)
        plain = ptor.decrypt(a2b_hex(text))
        return plain
#************End***************

def Evidence(text,key):
    # 要求key长度为16
    aes = aestest(key)
    enc = aes.en(text)
    print(enc)
    detext = aes.de(enc)
    print(detext)

本文地址:https://blog.csdn.net/xxthedd/article/details/107167147