AES加密解密CBC模式与ECB模式_aes cbc加解密

一、概要

AES(Advanced Encryption Standard)是一种对称加密算法,广泛应用于信息安全领域。AES支持多种密钥长度,包括128比特、192比特和256比特。在AES加密和解密中,同一个密钥用于两个过程。

下面是一个简单的Python实例,演示如何使用AES加密和解密文本。这里使用的是Python标准库中的 cryptography 模块,确保你已经安装该模块:

pip install cryptography

二、CBC模式

接下来,演示一个简单的AES加解密案例:

from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from base64 import b64encode, b64decode

def encrypt_text(key, text):
    # 选择AES算法和工作模式(这里选择CBC模式)
    cipher = Cipher(algorithms.AES(key), modes.CBC(b"\x00" * 16), backend=default_backend())

    # 创建加密器
    encryptor = cipher.encryptor()

    # 对文本进行填充
    text_padded = text + " " * (16 - len(text) % 16)

    # 加密文本
    ciphertext = encryptor.update(text_padded.encode()) + encryptor.finalize()

    # 返回Base64编码后的密文
    return b64encode(ciphertext).decode()

def decrypt_text(key, ciphertext):
    # 解码Base64得到二进制密文
    ciphertext = b64decode(ciphertext)

    # 选择AES算法和工作模式(这里选择CBC模式)
    cipher = Cipher(algorithms.AES(key), modes.CBC(b"\x00" * 16), backend=default_backend())

    # 创建解密器
    decryptor = cipher.decryptor()

    # 解密并去除填充
    text_padded = decryptor.update(ciphertext) + decryptor.finalize()
    text = text_padded.rstrip(b" ").decode()

    return text

if __name__ == "\_\_main\_\_":
    # 128比特的密钥(16字节)
    key = b"this\_is\_a\_secret"

    # 要加密的文本
    plaintext = "Hello, AES encryption and decryption!"

    # 加密
    ciphertext = encrypt_text(key, plaintext)
    print("Encrypted Text:", ciphertext)

    # 解密
    decrypted_text = decrypt_text(key, ciphertext)
    print("Decrypted Text:", decrypted_text)


这个示例演示了如何使用AES对文本进行加密和解密。请注意以下几点:

  • 选择AES算法和工作模式(这里使用CBC模式)。
  • 对文本进行填充,以适应AES块大小。
  • 使用Base64编码转换二进制数据,以便在不丢失数据的情况下进行文本表示。
  • 在解密时,要去除填充以还原原始文本。

在实际应用中,请谨慎保存和处理密钥。密钥的安全性对于AES的安全性至关重要。

三、ECB模式

当使用 AES 加密时,工作模式是影响加密结果的一个重要因素。ECB(Electronic Codebook)模式是最简单的工作模式,它将明文分割成块,并对每个块独立加密。这里给出一个在 ECB 模式下的 AES 加解密的例子:

from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from base64 import b64encode, b64decode

def encrypt_text_ecb(key, text):
    cipher = Cipher(algorithms.AES(key), modes.ECB(), backend=default_backend())
    encryptor = cipher.encryptor()
    text_padded = text + " " * (16 - len(text) % 16)
    ciphertext = encryptor.update(text_padded.encode()) + encryptor.finalize()
    return b64encode(ciphertext).decode()

def decrypt_text_ecb(key, ciphertext):
    ciphertext = b64decode(ciphertext)
    cipher = Cipher(algorithms.AES(key), modes.ECB(), backend=default_backend())
    decryptor = cipher.decryptor()
    text_padded = decryptor.update(ciphertext) + decryptor.finalize()
    text = text_padded.rstrip(b" ").decode()
    return text

if __name__ == "\_\_main\_\_":
    key = b"this\_is\_a\_secret"
    plaintext = "Hello, AES encryption and decryption!"

    # 使用 ECB 模式加密
    ciphertext_ecb = encrypt_text_ecb(key, plaintext)
    print("Encrypted Text (ECB):", ciphertext_ecb)

你可能感兴趣的:(python,开发语言)