【问题标题】:AES - how do I convert a string(byte string) to a byte(byte string)?AES - 如何将字符串(字节字符串)转换为字节(字节字符串)?
【发布时间】:2019-09-11 05:14:33
【问题描述】:

我正在使用 python 和 AES 创建一个加密的聊天程序。我有一些从https://riptutorial.com/python/example/18926/symmetric-encryption-using-pycrypto收集的工作代码

import hashlib
import math
import os
import base64

from Crypto.Cipher import AES

IV_SIZE = 16    # 128 bit, fixed for the AES algorithm
KEY_SIZE = 32   # 256 bit meaning AES-256, can also be 128 or 192 bits
SALT_SIZE = 16  # This size is arbitrary

cleartext = b'Lorem ipsum'
password = b'highly secure encryption password'
salt = os.urandom(SALT_SIZE)
derived = hashlib.pbkdf2_hmac('sha256', password, salt, 100000,
                          dklen=IV_SIZE + KEY_SIZE)
iv = derived[0:IV_SIZE]
key = derived[IV_SIZE:]

encrypted = salt + AES.new(key, AES.MODE_CFB, iv).encrypt(cleartext)

print(encrypted)
############################################
# edit here: it is being pulled as a string instead of a byte string
encrypted = str(encrypted)
###########################################

# encrypted and enc2 should be the same, except for the salt.


encryptedString = base64.encodebytes(encrypted)

print(encryptedString) # <- this is what can be stored and fetched from mySQL

encrypted = base64.decodebytes(encryptedString) # <- get the bytes back

salt = encrypted[0:SALT_SIZE]
derived = hashlib.pbkdf2_hmac('sha256', password, salt, 100000,
                          dklen=IV_SIZE + KEY_SIZE)
iv = derived[0:IV_SIZE]
key = derived[IV_SIZE:]
cleartext = AES.new(key, AES.MODE_CFB, iv).decrypt(encrypted[SALT_SIZE:])

    print(cleartext)

当我运行上面的代码时,我得到一个 TypeError: expected bytes-like object, not str

我尝试了几种方法:

  1. 字节(加密,'utf-8') 这种方法的问题在于它最终再次加密了字符串。

  2. 加密的.encode() 这和以前做的一样。

如何将字符串(字节字符串)转换为字节(字节字符串),而无需手动复制和粘贴字符串并在其前面放置 a b?

【问题讨论】:

  • 编码不是加密。它可能看起来像胡言乱语,但事实并非如此。
  • 我不明白。我试图避免手动复制和粘贴加密的输出。如何将其作为纯代码传递?

标签: python-3.x aes


【解决方案1】:

我运行了您链接到的示例,它运行良好,因此您的挑战似乎是将二进制数据存储到字符串中并再次恢复字节。任意字节都不是有效的 unicode 字符串,因此您需要将它们转码为有效的文本表示形式,例如 Base64。

这里有一些可能接近你想要的代码:

import hashlib
import math
import os
import base64

from Crypto.Cipher import AES

IV_SIZE = 16    # 128 bit, fixed for the AES algorithm
KEY_SIZE = 32   # 256 bit meaning AES-256, can also be 128 or 192 bits
SALT_SIZE = 16  # This size is arbitrary

cleartext = b'Lorem ipsum'
password = b'highly secure encryption password'
salt = os.urandom(SALT_SIZE)
derived = hashlib.pbkdf2_hmac('sha256', password, salt, 100000,
                              dklen=IV_SIZE + KEY_SIZE)
iv = derived[0:IV_SIZE]
key = derived[IV_SIZE:]

encrypted = salt + AES.new(key, AES.MODE_CFB, iv).encrypt(cleartext)

print(encrypted)

enc2=b'J\x82K\xf6\x10j\x06\xdbK\xd6\xf2\xc2Y\x98\xb9\x16\xb7\x0f\xfa\xb9\x19?\x9ak\xa1\xd5\xc4'

# encrypted and enc2 should be the same, except for the salt.

print(enc2)

encryptedString = base64.encodebytes(enc2)

print(encryptedString) # <- this is what can be stored and fetched from mySQL

encrypted = base64.decodebytes(encryptedString) # <- get the bytes back

salt = encrypted[0:SALT_SIZE]
derived = hashlib.pbkdf2_hmac('sha256', password, salt, 100000,
                              dklen=IV_SIZE + KEY_SIZE)
iv = derived[0:IV_SIZE]
key = derived[IV_SIZE:]
cleartext = AES.new(key, AES.MODE_CFB, iv).decrypt(encrypted[SALT_SIZE:])

print(cleartext)

运行这个,我得到了预期的输出:

b'\xc2\xc6\x81\x9f1\xdb@I\x155\xab6\xe5K\xb5\xfb\xe9\x93\x9c\xd6\xfc\xe1EN?\xdd\xaa'
b'J\x82K\xf6\x10j\x06\xdbK\xd6\xf2\xc2Y\x98\xb9\x16\xb7\x0f\xfa\xb9\x19?\x9ak\xa1\xd5\xc4'
b'SoJL9hBqBttL1vLCWZi5FrcP+rkZP5prodXE\n'
b'Lorem ipsum'

前两行是原始字节,第三行是Base64编码的字符串,用于解密Lorem imsum。

【讨论】:

  • 这并不能解决问题。下面这行有问题:enc2=b'J\x82K\xf6\x10j\x06\xdbK\xd6\xf2\xc2Y\x98\xb9\x16\xb7\x0f\xfa\xb9\x19?\x9ak\xa1\ xd5\xc4' 我试图避免任何手动输入。你怎么看?
  • 您没有提供足够的上下文来解决问题。我的代码 sn-p 展示了如何获取编码为 ASCII 的二进制数据,这些数据可以存储到 SQL 字符串并从中检索。再次不确定在哪里涉及手动输入,因为缺少上下文。
  • "enc2=..." 并不是解决方案的一部分,而是证明数据仍然是二进制的,因此还没有准备好处理需要的任何操作一个字符串。 encryptedString 的打印输出表明它是一个有效的字符串。
猜你喜欢
  • 1970-01-01
  • 2014-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-26
  • 2011-12-10
  • 1970-01-01
相关资源
最近更新 更多