繁体   English   中英

Object型<class 'tuple'>在 django 中加密文件时无法传递给 C 代码</class>

[英]Object type <class 'tuple'> cannot be passed to C code when encrypting file in django

所以我想用我输入的关键字加密 file.txt。 但我收到错误消息

Object 类型 <class 'tuple'> 不能传递给 C 代码

我的问题是这段代码:

aes = AES.new(key.encode('utf8'), AES.MODE_CTR, counter=ctr)

如果我将编码添加到 ctr,那么我收到错误消息'dict' object has no attribute 'encode'

如果我删除不向键和 ctr 添加任何编码,那么我收到错误消息Object 类型 <class 'str'> 无法传递给 C 代码

有人可以帮我解决吗? 我正在使用 django 使用方法 CTR 使用 AES 128 进行加密。 或者也许有人可以用另一种方法给我示例 aes 加密,但可以在 django 中运行。 这是我的完整 function 代码:

# AES supports multiple key sizes: 16 (AES128), 24 (AES192), or 32 (AES256).
key_bytes = 16
# Takes as input a 32-byte key and an arbitrary-length plaintext and returns a
# pair (iv, ciphtertext). "iv" stands for initialization vector.
def encrypt(key, testpass):
    assert len(key) == key_bytes
    print(testpass)
    print(key)
    # Choose a random, 16-byte IV.
    iv = Random.new().read(AES.block_size)

    # Convert the IV to a Python integer.
    iv_int = int(binascii.hexlify(iv), 16)

    # Create a new Counter object with IV = iv_int.
    ctr = Counter.new(AES.block_size * 8, initial_value=iv_int)
    print(ctr)

    # Create AES-CTR cipher.
    aes = AES.new(key.encode('utf8'), AES.MODE_CTR, counter=ctr)

    # Encrypt and return IV and ciphertext.
    ciphertext = aes.encrypt(testpass)
    print(iv)
    print(ciphertext)
    return (iv, ciphertext)

这就是我所说的function:

testpass = Audio_store.objects.all().values_list('password').last()
enkripsi = encrypt("testingtesting11", testpass)

当我打印 testpass 时,它包含('testpass_3kEMV2T.txt',)

但是当我打印 testpass.encode("utf-8") 时,它什么也没显示

您的测试数据是('testpass_3kEMV2T.txt',) ,您需要从tuple中提取元素并将其作为bytes传递给您的encrypt方法,因为AES.encrypt方法需要bytes | bytearray | memoryview bytes | bytearray | memoryview bytes | bytearray | memoryview作为它的论据。 这是您的encrypt的一个版本,它确定您传递的参数是否为str并将其编码为 bytes (但它不检查是否为bytes ,如果您想要更严格的检查,我将其作为练习留给您)。

import binascii


from Crypto.Cipher import AES
from Crypto.Util import Counter
from Crypto import Random

key_bytes = 16

# here's a rudimentary change that accepts bytes or
# str
def encrypt(key, plaintext):
    if isinstance(plaintext, str):
        plaintext = plaintext.encode("utf-8")

    assert len(key) == key_bytes
    # Choose a random, 16-byte IV.
    iv = Random.new().read(AES.block_size)

    # Convert the IV to a Python integer.
    iv_int = int(binascii.hexlify(iv), 16)

    # Create a new Counter object with IV = iv_int.
    ctr = Counter.new(AES.block_size * 8, initial_value=iv_int)
    print(ctr)

    # Create AES-CTR cipher.
    aes = AES.new(key.encode('utf8'), AES.MODE_CTR, counter=ctr)

    # Encrypt and return IV and ciphertext.
    ciphertext = aes.encrypt(plaintext)
    return (iv, ciphertext)

# for now, since you're getting your data from the db which is a 1-tuple, you have to pull out the first elem
testpass = ('testpass_3kEMV2T.txt',)
print(encrypt("a"*16, testpass[0]))

Output:

{'counter_len': 16, 'prefix': b'', 'suffix': b'', 'initial_value': 302861312392273214314458272471454024973, 'little_endian': False}
(b'\xe3\xd8\xf7\x90\xcd\x96m\xcb\xa5g)\xd1\xda\xc3\x85\r', b'-F)\x83\x9a\xf9\xe1\xc3\xfb\xfa_<^\x1c:q\x07\xa1@\xbb')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM