Python处理AES-CTR的iv为16位的情况

python使用aes-ctr时,如果key为16位字符,则需要拆分成8个nonce和8个initial_value。否则会直接报错,说nonce太长

import base64

from Cryptodome.Cipher import AES

def aes_decrypt(key, iv, data):
    # 将16字节IV拆分为两部分
    iv_bytes = iv.encode('utf-8')
    nonce_part = iv_bytes[:8]  # 前8字节作为nonce
    counter_part = iv_bytes[8:]  # 后8字节作为initial_value

    # 创建解密器
    decipher = AES.new(
        key=key.encode('utf-8'),
        mode=AES.MODE_CTR,
        nonce=nonce_part,
        initial_value=counter_part
    )

    decrypted = decipher.decrypt(base64.b64decode(data.encode()))
    return decrypted.decode()

print(aes_decrypt("92d6dfb94000b9d2", "569b3d27c6329bf7", "YytG6oT6vOl81kKt/NDwR6QjynyruQC7y9kpWiV7QEg="))

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注