I have a python script trying to encrypt a file with AWS KMS using boto3. I can get the file encrypted and wrote into a file. But when I tried to decrypted the file using the second part code, it raised an error as below, could someone help on this or there are any better solutions? Many thanks.
Traceback (most recent call last): File "runtime/lib/python3.4/site-packages/botocore/client.py", line 357, in _api_call return self._make_api_call(operation_name, kwargs) File "runtime/lib/python3.4/site-packages/botocore/client.py", line 661, in _make_api_call raise error_class(parsed_response, operation_name) botocore.errorfactory.InvalidCiphertextException: An error occurred (InvalidCiphertextException) when calling the Decrypt operation:
Encrypt a file
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)
Decrypt the file encrypted before
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)