Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kms#decrypt throws 'utf8' codec error #31

Closed
bilalaslam opened this issue Nov 26, 2014 · 7 comments
Closed

kms#decrypt throws 'utf8' codec error #31

bilalaslam opened this issue Nov 26, 2014 · 7 comments
Labels
bug This issue is a confirmed bug. dependencies This issue is a problem in a dependency. pending-release This issue will be fixed by an approved PR that hasn't been released yet.

Comments

@bilalaslam
Copy link

kms#decrypt throws this error every time:

UnicodeDecodeError: 'utf8' codec can't decode byte 0xf2 in position 2: invalid continuation byte

Repro steps:

import boto3
kms = boto3.client('kms')
result = kms.encrypt(KeyId='some_key_id_redacted',Plaintext='my name is bob')
kms.decrypt(CiphertextBlob=result['CiphertextBlob'])

May be related to this issue

@danielgtaylor danielgtaylor added bug This issue is a confirmed bug. dependencies This issue is a problem in a dependency. labels Nov 26, 2014
@danielgtaylor
Copy link
Member

@bilalaslam this appears to be a bug in Botocore where the CiphertextBlob should automatically be base64-encoded. For example, this appears to work for me:

import base64

kms.decrypt(CiphertextBlob=base64.b64encode(result['CiphertextBlob']).decode('utf-8'))

You can use that as a workaround for now, but once we make the underlying fix you'll need to remove the manual base64-encoding.

@spgta
Copy link

spgta commented Nov 28, 2014

I can confirm the bug on my end too.

@danielgtaylor
Copy link
Member

@spgta does the workaround work for you? This should get fixed in Botocore soon.

@jstnblau
Copy link

jstnblau commented Dec 4, 2014

The workaround worked for me, but I had to base64 encode the Plaintext as well.

encrypted=kms.encrypt(KeyId='', Plaintext='Test')
decrypted=kms.decrypt(CiphertextBlob=base64.b64encode(encrypted['CiphertextBlob']).decode('utf-8'))
base64.b64encode(decrypted['Plaintext']).decode('utf-8')

danielgtaylor added a commit to danielgtaylor/botocore that referenced this issue Dec 4, 2014
This pull request modifies the code added in ffcef5f to also support
arguments passed in as bytes, such as those you might encounter when
using KMS.

See this related issue: boto/boto3#31

Since the JSON data for serialization tests doesn't support specifying
bytes as input, I've added a new test class to cover this case.
@spgta
Copy link

spgta commented Dec 5, 2014

@danielgtaylor

This works for me:
encrypt:

    kms = boto3.client("kms")   
    e = kms.encrypt(KeyId=self.keyid,EncryptionContext={""},Plaintext=encoded_file)
    self.objectBody = base64.b64encode(e['CiphertextBlob']).decode("utf-8")

decrypt:

kms = boto3.client("kms")
decrypted_file = kms.decrypt(CiphertextBlob=enc_string,EncryptionContext="")

@danielgtaylor
Copy link
Member

@spgta, @jstnblau, @bilalaslam this should be fixed upstream with boto/botocore#397, which was just merged. It'll go out with the next release.

@danielgtaylor danielgtaylor added the pending-release This issue will be fixed by an approved PR that hasn't been released yet. label Dec 5, 2014
@danielgtaylor
Copy link
Member

Released in version 0.0.5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a confirmed bug. dependencies This issue is a problem in a dependency. pending-release This issue will be fixed by an approved PR that hasn't been released yet.
Projects
None yet
Development

No branches or pull requests

4 participants