python aes256

from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import padding
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from os import urandom as random_bytes
class main_aes256:
    def __init__(self, salt_value, pwd, plaintext=b'', ciphertext=b''):     # 初始化方法(构造函数)
        self.pwd = pwd
        kdf = PBKDF2HMAC(
            algorithm=hashes.SHA256(),
            length=32,
            salt=salt_value,
            iterations=100000,
            backend=default_backend()
        )
        self.key = kdf.derive(pwd)
        self.plaintext = plaintext
        self.ciphertext = ciphertext

    def encrypt(self):  # 加密函数
        padder = padding.PKCS7(algorithms.AES.block_size).padder()
        padded_plaintext = padder.update(self.plaintext) + padder.finalize()
        iv = random_bytes(16)
        cipher = Cipher(algorithms.AES(self.key), modes.CBC(iv), backend=default_backend())
        encryptor = cipher.encryptor()
        ciphertext = encryptor.update(padded_plaintext) + encryptor.finalize()
        return iv + ciphertext

    def decrypt(self):  # 解密函数
        iv = self.ciphertext[:16]
        ciphertext = self.ciphertext[16:]
        cipher = Cipher(algorithms.AES(self.key), modes.CBC(iv), backend=default_backend())
        decryptor = cipher.decryptor()
        padded_plaintext = decryptor.update(ciphertext) + decryptor.finalize()
        unpadder = padding.PKCS7(algorithms.AES.block_size).unpadder()
        plaintext = unpadder.update(padded_plaintext) + unpadder.finalize()
        return plaintext

password = b"your_password"
salt = b"your_salt"
plaintext1 = b"Hello, AES-256!"
aes256_en1 = main_aes256(salt, password, plaintext=plaintext1)  # 加密
ciphertext1 = aes256_en1.encrypt()
aes256_de1 = main_aes256(salt, password, ciphertext=ciphertext1)  # 解密
decrypted_plaintext1 = aes256_de1.decrypt()
print("Plaintext: ", plaintext1)
print("Ciphertext: ", ciphertext1)
print("Decrypted Plaintext: ", decrypted_plaintext1)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
如果您只是想使用Python进行AES 256位加密和解密,而不涉及具体的加密模式(如CBC、ECB等),您可以使用`cryptography`库来实现。下面是一个示例代码: ```python from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives import padding from base64 import b64encode, b64decode def aes_encrypt(key, data): backend = default_backend() iv = b'\x00' * 16 # 使用所有零的初始向量 cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=backend) encryptor = cipher.encryptor() padder = padding.PKCS7(algorithms.AES.block_size).padder() padded_data = padder.update(data.encode()) + padder.finalize() ciphertext = encryptor.update(padded_data) + encryptor.finalize() return b64encode(ciphertext).decode() def aes_decrypt(key, encrypted_data): backend = default_backend() iv = b'\x00' * 16 # 使用所有零的初始向量 cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=backend) decryptor = cipher.decryptor() ciphertext = b64decode(encrypted_data) decrypted_padded_data = decryptor.update(ciphertext) + decryptor.finalize() unpadder = padding.PKCS7(algorithms.AES.block_size).unpadder() decrypted_data = unpadder.update(decrypted_padded_data) + unpadder.finalize() return decrypted_data.decode() # 示例使用 key = b'32字节的密钥' # 256位密钥长度为32字节 data = "要加密的数据" encrypted_data = aes_encrypt(key, data) print("加密后的数据:", encrypted_data) decrypted_data = aes_decrypt(key, encrypted_data) print("解密后的数据:", decrypted_data) ``` 请注意,这里使用了所有零的初始向量(IV)和PKCS7填充(padder/unpadder)来确保数据长度满足AES块大小要求。您需要将`key`替换为您自己的256位密钥,并将`data`替换为您要加密的实际数据。 请记住,加密和解密操作所使用的密钥必须相同。如果您有其他疑问,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

·大道至简

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值