当前位置:   article > 正文

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

aes cbc加解密

一、概要

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

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

pip install cryptography
  • 1

二、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)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51

这个示例演示了如何使用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)

    # 使用 ECB 模式解密
    decrypted_text_ecb = decrypt_text_ecb(key, ciphertext_ecb)
    print("Decrypted Text (ECB):", decrypted_text_ecb)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

在这个例子中,我使用了 modes.ECB() 来选择 ECB 模式。需要注意的是,ECB 模式在某些情况下可能不够安全,因为它没有考虑相同的明文块会得到相同的密文块,这可能导致一些安全问题。因此,在实际应用中,最好选择更安全的工作模式,比如 CBC 模式

四、AES加解密的基本流程

AES加密流程:
1、*密钥扩展(Key Expansion):将初始密钥扩展为多个轮次需要的轮密钥。
2、初始轮密钥加(AddRoundKey):将轮密钥与明文块进行按位异或(XOR)操作。
3、多轮次的轮函数(Rounds):进行多轮的代换(SubBytes)、置换(ShiftRows)、混淆(MixColumns)、轮密钥加操作。
4、最终轮(Final Round):最后一轮省略混淆操作。
AES解密流程:
1、密钥扩展(Key Expansion): 与加密相同,将初始密钥扩展为多个轮次需要的轮密钥。
2、逆初始轮密钥加(InvAddRoundKey): 与加密相同,将轮密钥与密文块进行按位异或(XOR)操作。
3、多轮次的逆轮函数(Inverse Rounds): 逆向进行代换(InvSubBytes)、逆向置换(InvShiftRows)、逆向混淆(InvMixColumns)、逆初始轮密钥加操作。
4、最终逆轮(Final Round): 最后一轮省略逆向混淆操作。

请注意,AES加解密的实际轮数取决于密钥长度。对于128比特密钥,有10轮;192比特密钥,有12轮;256比特密钥,有14轮。在每一轮中,都会使用不同的轮密钥。整个加解密过程通过多轮操作来增加安全性。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/422727
推荐阅读
相关标签
  

闽ICP备14008679号