在 Rails 3.0 (Ruby 1.9.2) 应用程序中,我尝试使用以下方式加密一些数据:
cipher = OpenSSL::Cipher.new 'aes-256-cbc'
cipher.encrypt
cipher.key = cipher.random_key
cipher.iv = cipher.random_iv
encrypted = cipher.update 'most secret data in the world'
encrypted << cipher.final
这将进入一个 UTF-8 数据库。我的问题是
> encrypted.encoding
=> #<Encoding:ASCII-8BIT>
> encrypted.encode 'utf-8'
Encoding::UndefinedConversionError: "\xF7" from ASCII-8BIT to UTF-8
如何获得 UTF-8 加密字符串?