title: How to encrypt data in a UTF-8 string using OpenSSL::Cipher?
body: <p>In a Rails 3.0 (Ruby 1.9.2) app I'm trying to encrypt some data using something like this:</p>
  
  <pre><code>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 &lt;&lt; cipher.final
  </code></pre>
  
  <p>That will go into a UTF-8 database. My problem is that</p>
  
  <pre><code>&gt; encrypted.encoding
   =&gt; #&lt;Encoding:ASCII-8BIT&gt;
  
  &gt; encrypted.encode 'utf-8'
  Encoding::UndefinedConversionError: "\xF7" from ASCII-8BIT to UTF-8
  </code></pre>
  
  <p>How can I get an UTF-8 encrypted string?</p>