Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python AWS加密类型错误:can';t concat str到字节_Python_Python 3.x_Amazon Web Services_Encryption_Aws Kms - Fatal编程技术网

Python AWS加密类型错误:can';t concat str到字节

Python AWS加密类型错误:can';t concat str到字节,python,python-3.x,amazon-web-services,encryption,aws-kms,Python,Python 3.x,Amazon Web Services,Encryption,Aws Kms,我尝试编写一个代码,由谁加密我的数据,然后我尝试执行我的代码,我得到一个错误: 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)

我尝试编写一个代码,由谁加密我的数据,然后我尝试执行我的代码,我得到一个错误:

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()
这是一个错误:

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=λs:s+(32-透镜%32)*“ TypeError:无法将str转换为字节
也许谁知道哪里有问题?请建议

您的函数
PAD
用于使用
字符串
输入,您可以使用
字节
输入来调用它(
b'Hello,World!'

PAD('Hello,World!')
(没有前面的
b
)可以工作。 一种解决方案是将明文填充为
字符串
,然后将其转换为
字节
,例如:

plaintext=PAD('Hello,world!')
明文_字节=明文。编码('utf-8')


有关如何将
字符串
转换为
字节

的信息,请参阅。哦,它可以工作!!谢谢,也许你知道我需要用哪种方式加密这个字符串???cypher=AES.new(明文_key,AES.MODE_EAX)然后我使用这个,我得到:TypeError:只有字节字符串可以传递给C代码,我尝试使用许多AES.MODE,但什么都没有..:(很高兴它能工作,在这种情况下,您可以接受我的解决方案;)关于第二个问题,您可能遇到了相反的问题:当AES函数需要
字节时,您正在传递
字符串。尝试使用
明文\u key.encode('utf-8')
调用AES函数,看看这是否解决了问题。也许你知道怎么做吗?查看我之前的评论;您的错误很可能与您使用的模式无关,而是与您调用AES方法的方式有关(您应该使用
字节
,而不是
字符串
)。关于使用哪种模式,我不是专家,只能建议您在谷歌上搜索如何使用不同的AES模式(加密很难正确)。