'bytes'对象在解密 AES CTR 中没有属性'encode'



所以我有使用AES CTR加密和解密的功能。我用的是python 3。这个想法来自于tweaksp

当我尝试解密以获得明文时,我得到错误消息:

'bytes'对象没有'encode'属性

后我检查另一个stackoverflow,我发现如果.encode('hex')不工作在python 3。X以这个为底下面是我的加密代码:

key_bytes = 16
def encrypt(key, pt):
plaintext = read_file(pt)
if isinstance(plaintext, str):
pt= plaintext.encode("utf-8")
if len(key) <= key_bytes:
for x in range(len(key),key_bytes):
key = key + "0"
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)
# Create AES-CTR cipher.
aes = AES.new(key.encode('utf8'), AES.MODE_CTR, counter=ctr)
# Encrypt and return IV and ciphertext.
ciphertext = aes.encrypt(pt)
return (iv, ciphertext)

我为解密输入的内容

key: maru000000000000

四:b 'he xf5 xba x9bf xf4 xacfA xa6 xc7 xce xd0 x90j '

密文:011010000110010111110101101101100110110110100101100011001100100010001101001101100011111001110110100001001000001101010

和我的解密码:

def decrypt(key, iv, ciphertext):
assert len(key) == key_bytes
print(type(iv))
# Initialize counter for decryption. iv should be the same as the output of
# encrypt().
iv_int = int(iv.encode('hex'), 16) 
ctr = Counter.new(AES.block_size * 8, initial_value=iv_int)
# Create AES-CTR cipher.
aes = AES.new(key, AES.MODE_CTR, counter=ctr)
# Decrypt and return the plaintext.
plaintext = aes.decrypt(ciphertext)
return plaintext

误差

好了,我找到问题了。我注释了有问题的代码,并将其替换为iv_int = int(binascii.hexlify(iv), 16)。这是更新后的代码:

from Crypto import Random
from Crypto.Cipher import AES
from Crypto.Util import Counter
import binascii
key_bytes = 16
def encrypt(key, plaintext):
if isinstance(plaintext, str):
pt= plaintext.encode("utf-8")
if len(key) <= key_bytes:
for x in range(len(key),key_bytes):
key = key + "0"
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)
# Create AES-CTR cipher.
aes = AES.new(key.encode('utf8'), AES.MODE_CTR, counter=ctr)
# Encrypt and return IV and ciphertext.
ciphertext = aes.encrypt(pt)
return (iv, ciphertext)
def decrypt(key, iv, ciphertext):
assert len(key) == key_bytes
# Initialize counter for decryption. iv should be the same as the output of
# encrypt().
#iv_int = int(iv.encode('hex'), 16) 
iv_int = int(binascii.hexlify(iv), 16)
ctr = Counter.new(AES.block_size * 8, initial_value=iv_int)
# Create AES-CTR cipher.
aes = AES.new(key, AES.MODE_CTR, counter=ctr)
# Decrypt and return the plaintext.
plaintext = aes.decrypt(ciphertext)
return plaintext
plaintext = 'This is just a test'
key = 'maru000000000000'
iv, ciphertext = encrypt(key, plaintext)
print(ciphertext)
decrypted_text = decrypt(key, iv, ciphertext)
print(decrypted_text)

下面给出与int(binascii.hexlify(iv), 16)

相同的结果
iv_int = int.from_bytes(iv, byteorder='little', signed=False)

最新更新