【发布时间】:2019-07-24 01:35:39
【问题描述】:
我有一个 python 脚本尝试使用 boto3 使用 AWS KMS 加密文件。我可以将文件加密并写入文件。但是当我尝试使用第二部分代码解密文件时,它引发了如下错误,有人可以帮助解决这个问题还是有更好的解决方案?非常感谢。
Traceback(最近一次调用最后一次): _api_call 中的文件“runtime/lib/python3.4/site-packages/botocore/client.py”,第 357 行 return self._make_api_call(operation_name, kwargs) _make_api_call 中的文件“runtime/lib/python3.4/site-packages/botocore/client.py”,第 661 行 raise error_class(parsed_response, operation_name) botocore.errorfactory.InvalidCiphertextException:调用解密操作时发生错误(InvalidCiphertextException):
加密文件
client = boto3.client(
'kms',
region_name='us-east-1',
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key
)
with open(src_file, 'rb') as infile :
with open(ret_file, 'wb') as outfile :
while True:
chunk = infile.read(chunk_size)
if not chunk :
break
resp = client.encrypt(KeyId=kms_id, Plaintext=chunk)['CiphertextBlob']
outfile.write(resp)
解密之前加密的文件
client = boto3.client(
'kms',
region_name='us-east-1',
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key
)
with open(src_file, 'rb') as infile :
with open(ret_file, 'wb') as outfile :
while True:
chunk = infile.read(chunk_size)
if not chunk :
break
resp = client.decrypt(CiphertextBlob=chunk)['Plaintext']
outfile.write(resp)
【问题讨论】: