spikex / strongbox

Secures ActiveRecord attributes with public key encryption
http://stuff-things.net/2009/04/17/introducing-strongbox/
MIT License
398 stars 42 forks source link

Encoding::CompatibilityError #23

Open extcode opened 12 years ago

extcode commented 12 years ago

I have a problem with german umlauts in secured datafields. When I saved "grün" (english: green) in a mysql database field, and show it again, i get the following Error:

Encoding::CompatibilityError in Addressees#show

incompatible character encodings: UTF8 and ASCII-8BIT

I had to use force_encode('utf8') for each field.

It this an error in strongbox? Why are the encrypted fields decrypted in ascii-8bit?

spikex commented 12 years ago

The issue is how Ruby itself handles the decrypted field. "ASCII-8BIT" is an alias for "BINARY" and means "raw-bytes". Ruby chooses this because it doesn't know what the encoding of the field was before it was encrypted. So, yes you have to use force_encode to set the encoding if you need more than ASCII.

The fix would be for the Ruby RSA to use the default encoding and provide a way to override it, like Ruby IO does.

There's a good writeup on Ruby 1.9 encoding issues here:

http://yehudakatz.com/2010/05/05/ruby-1-9-encodings-a-primer-and-the-solution-for-rails/