我正在使用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:预期的类似字节的对象,而不是str
我尝试了几种方法:
个字节(已加密,“ utf-8”) 这种方法的问题在于它最终再次加密了字符串。
encrypted.encode() 这和以前一样。
如何将字符串(字节字符串)转换为字节(字节字符串)而无需手动复制并粘贴字符串并将b放在字符串前面?
答案 0 :(得分:0)
我运行了您链接到的示例,它运行良好,因此看来您面临的挑战是将二进制数据存储到字符串中并再次恢复字节。任意字节将不是有效的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。