好吧,没有`pycryptodome`库,我们就要从零开始了。这可是个挑战,不过没关系,我们能搞定。为了保持代码的简洁,我会用纯Python实现一个简单的AES算法的基本操作,并展示如何在CBC和OFB模式下加密和解密。
1. **密钥扩展(Key Expansion)**:从原始密钥生成一系列子密钥。
2. **初始轮(Initial Round)**:用第一个子密钥对数据块进行XOR操作。
3. **主要轮(Main Rounds)**:包括子字节替换(S-Box 字节替换)、行移位、列混淆和轮密钥加。
4. **最后一轮(Final Round)**:与主要轮类似,但没有列混淆步骤。
为了简洁起见,我会提供一个基于32字节密钥(256位)和16字节块(128位)的简化版AES算法。其中具体的S-box、密钥扩展和其他细节就不展开了,以示例为主。
import numpy as np
# 定义S-Box和逆S-Box
S_BOX = np.array([...], dtype=np.uint8) # 填入实际的S-Box值
INV_S_BOX = np.array([...], dtype=np.uint8) # 填入实际的逆S-Box值
# 密钥扩展(简化版,实际需要更多轮)
def key_expansion(key):
# 简化版密钥扩展,只做简单处理
return [key] * 11
# 添加轮密钥
def add_round_key(state, round_key):
return np.bitwise_xor(state, round_key)
# SubBytes 步骤
def sub_bytes(state, sbox):
return sbox[state]
# ShiftRows 步骤
def shift_rows(state):
# 简化版移位操作
return state
# MixColumns 步骤(省略,简化版不做处理)
# AES加密单轮
def aes_encrypt_round(state, round_key):
state = sub_bytes(state, S_BOX)
state = shift_rows(state)
# state = mix_columns(state) # 混淆列步骤,简化版省略
state = add_round_key(state, round_key)
return state
# AES加密(简化版)
def aes_encrypt(plain_text, key):
state = np.array(list(plain_text), dtype=np.uint8).reshape((4, 4))
keys = key_expansion(key)
state = add_round_key(state, keys[0])
for round_key in keys[1:]:
state = aes_encrypt_round(state, round_key)
return state.tobytes()
# AES解密(简化版)
def aes_decrypt(cipher_text, key):
state = np.array(list(cipher_text), dtype=np.uint8).reshape((4, 4))
keys = key_expansion(key)
for round_key in reversed(keys[1:]):
state = aes_encrypt_round(state, round_key) # 使用相同的加密轮函数
state = add_round_key(state, keys[0])
return state.tobytes()
# CBC模式下加密
def encrypt_aes_cbc(plain_text, key, iv):
block_size = 16
plain_text = pad(plain_text, block_size)
cipher_text = b''
prev_block = iv
for i in range(0, len(plain_text), block_size):
block = plain_text[i:i + block_size]
block = np.bitwise_xor(block, prev_block)
encrypted_block = aes_encrypt(block, key)
cipher_text += encrypted_block
prev_block = encrypted_block # CBC模式使用当前密文块作为下一轮的IV
return cipher_text
# CBC模式下解密
def decrypt_aes_cbc(cipher_text, key, iv):
block_size = 16
plain_text = b''
prev_block = iv
for i in range(0, len(cipher_text), block_size):
block = cipher_text[i:i + block_size]
decrypted_block = aes_decrypt(block, key)
decrypted_block = np.bitwise_xor(decrypted_block, prev_block)
plain_text += decrypted_block
prev_block = block # CBC模式使用当前密文块作为下一轮的IV
return unpad(plain_text)
# OFB模式下加密和解密(对称操作)
def encrypt_aes_ofb(plain_text, key, iv):
block_size = 16
cipher_text = b''
prev_block = iv
for i in range(0, len(plain_text), block_size):
encrypted_iv = aes_encrypt(prev_block, key)
block = plain_text[i:i + block_size]
cipher_text_block = np.bitwise_xor(block, encrypted_iv)
cipher_text += cipher_text_block
prev_block = encrypted_iv # OFB模式使用当前的加密IV
return cipher_text
def decrypt_aes_ofb(cipher_text, key, iv):
return encrypt_aes_ofb(cipher_text, key, iv) # OFB解密与加密操作相同
def pad(text, block_size):
padding_len = block_size - len(text) % block_size
padding = bytes([padding_len] * padding_len)
return text + padding
def unpad(padded_text):
padding_len = padded_text[-1]
return padded_text[:-padding_len]
if __name__ == "__main__":
key = np.random.randint(0, 256, 16, dtype=np.uint8)
iv = np.random.randint(0, 256, 16, dtype=np.uint8)
# CBC模式
print("CBC Mode:")
encrypted_cbc = encrypt_aes_cbc(b"Hello1234567890", key, iv)
print(f"Encrypted (CBC): {encrypted_cbc}")
decrypted_cbc = decrypt_aes_cbc(encrypted_cbc, key, iv)
print(f"Decrypted (CBC): {decrypted_cbc}")
# OFB模式
print("\nOFB Mode:")
encrypted_ofb = encrypt_aes_ofb(b"Hello1234567890", key, iv)
print(f"Encrypted (OFB): {encrypted_ofb}")
decrypted_ofb = decrypt_aes_ofb(encrypted_ofb, key, iv)
print(f"Decrypted (OFB): {decrypted_ofb}")
注意,这里的`S_BOX`和`INV_S_BOX`需要你去填充实际的值。另外,真正的AES算法要比这个复杂得多,这里只是一个简化版本来演示基本思路。如果你真的需要一个完整的AES实现,可能需要阅读相关标准文档(比如FIPS-197)和更多的密码学知识。