[英]"TypeError: Object type <class 'str'> cannot be passed to C code when encrypting an image
[英]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.