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

Using key with pgcrypto #39

Closed
techthumb opened this issue Jun 23, 2014 · 3 comments
Closed

Using key with pgcrypto #39

techthumb opened this issue Jun 23, 2014 · 3 comments
Labels

Comments

@techthumb
Copy link

Hi,

I have a scenario where we have data that is already encrypted using a different key.

I now want to migrate this data to use SymmetricEncryption & the key generated by SymmetricEncryption

I am using the pgcrypto package in postgres to migrate this data.
The decrypt function works okay as the data was encrypted using pgp_sym_encrypt

My migration decrypts the column and then tries to encrypt the column using SymmetricEncryption.cipher.key

When the migration runs, I get the following error

PG::CharacterNotInRepertoire: ERROR:  invalid byte sequence for encoding "UTF8": 0x96

My key is: \v\x96\x1C\xAE\xAD\xD6r\x96f\xD4kJT\xEB#\x01\xDC\x06\x98\xD3f\xA0+z\xF3?qg\x18\xE1\x1D\xD8

I know my questing is vague, but can you suggest some pointers to how you might perform this migration?

Thanks,

Kunal

@reidmorrison
Copy link
Owner

I am not familiar with pgcrypto in Postgres. The problem though sounds like a general encoding issue, where the data is being marked as UTF8, but contains invalid UTF-8 data.
You can try converting the data to binary before decrypting it.

"non-utf8-string".force_encoding("ASCII-8BIT")

@techthumb
Copy link
Author

Thanks for the quick response.

Yes, I agree that this is an encoding issue.

I think it's mostly having problem decoding the SymmetricEncryption key.

Running the following command produces weird errors on the screen.

rails runner 'puts SymmetricEncryption.cipher.send(:key)'

����r�f�kJT�#���f�+z�?qg��

Is there a way that I can convert this key to a plain text equivalent?

@reidmorrison
Copy link
Owner

It is not an error, it is displaying binary data. The encryption key is made up of the entire binary range based on it's key length.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants