reidmorrison / symmetric-encryption

Symmetric Encryption for Ruby Projects using OpenSSL
https://logger.rocketjob.github.io/
Apache License 2.0
475 stars 92 forks source link

Using key with pgcrypto #39

Closed techthumb closed 10 years ago

techthumb commented 10 years ago

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 commented 10 years ago

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 commented 10 years ago

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 commented 10 years ago

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.