0
我尝试写一个代码,其中我的资料加密,然后我尝试执行我的代码我得到一个错误:AWS加密类型错误:不能Concat的字符串str的字节
import base64
import boto3
from Crypto.Cipher import AES
PAD = lambda s: s + (32 - len(s) % 32) * ' '
def get_arn(aws_data):
return 'arn:aws:kms:{region}:{account_number}:key/{key_id}'.format(**aws_data)
def encrypt_data(aws_data, plaintext_message):
kms_client = boto3.client(
'kms',
region_name=aws_data['region'])
data_key = kms_client.generate_data_key(
KeyId=aws_data['key_id'],
KeySpec='AES_256')
cipher_text_blob = data_key.get('CiphertextBlob')
plaintext_key = data_key.get('Plaintext')
# Note, does not use IV or specify mode... for demo purposes only.
cypher = AES.new(plaintext_key, AES.MODE_EAX)
encrypted_data = base64.b64encode(cypher.encrypt(PAD(plaintext_message)))
# Need to preserve both of these data elements
return encrypted_data, cipher_text_blob
def main():
# Add your account number/region/KMS Key ID here.
aws_data = {
'region': 'eu-west-1',
'account_number': '70117777xxxx',
'key_id': 'xxxxxxx-83ac-4b5e-93d4-xxxxxxxx',
}
# And your super secret message to envelope encrypt...
plaintext = b'Hello, World!'
# Store encrypted_data & cipher_text_blob in your persistent storage. You will need them both later.
encrypted_data, cipher_text_blob = encrypt_data(aws_data, plaintext)
print(encrypted_data)
if __name__ == '__main__':
main()
这是一个错误:
PAD = lambda s:s +(32-len(s)%32)*'' TypeError:无法将字节转换为字节 可能谁知道问题出在哪里?请建议
哦,它的工作原理!谢谢,也许你知道我需要使用哪种模式来加密这个字符串? cypher = AES.new(plaintext_key,AES.MODE_EAX)然后我使用这个,我得到:TypeError:只有字节字符串可以传递给C代码,我尝试使用许多aes.mode但没有.. :( – Andrej
快乐它的工作原理,在这种情况下,你可以接受我的解决方案;)关于你的第二个问题,你可能会遇到相反的问题:当AES函数需要'bytes'时,你传递一个'string'。尝试用'plaintext_key.encode('utf-8')'调用AES函数来查看这是否解决了这个问题。 – pills
接受!你可以用我的模式给我看看吗? – Andrej