title: Ruby create Cipher key from preexisting key string
body: <p>With the OpenSSL CLI I can encrypt values using just about any ASCII string I can find as long as it fits the lengths required by the encryption method.</p>
  
  <p>For example:</p>
  
  <pre><code>printf 'flipflop' | openssl enc -K 2317823178123897237891232345345234524523452345 -iv 123789123789123789 -base64 -aes-256-cbc
  
  Pd4+UEBW1RyTjARy1rpndQ==
  
  printf 'Pd4+UEBW1RyTjARy1rpndQ==\n' | openssl enc -d -K 2317823178123897237891232345345234524523452345 -iv 123789123789123789 -base64 -aes-256-cbc
  
  flipflop
  </code></pre>
  
  <p>However if I take that key and we run through this with Ruby:</p>
  
  <pre><code>require 'openssl'
  
  cipher = OpenSSL::Cipher::Cipher.new 'AES-256-CBC'
  cipher.encrypt
  cipher.key = "2317823178123897237891232345345234524523452345"
  cipher.iv = "123789123789123789"
  encrypted = cipher.update "flipflop" + cipher.final
  puts encrypted
  
  cipher = OpenSSL::Cipher::Cipher.new 'AES-256-CBC'
  cipher.decrypt
  cipher.key = "2317823178123897237891232345345234524523452345"
  cipher.iv = "123789123789123789"
  plain = cipher.update(encrypted) + cipher.final
  puts plain
  
  ���ISq��Ҷ0�e�
  crypt.rb:14:in `final': bad decrypt (OpenSSL::Cipher::CipherError)
      from crypt.rb:14:in `&lt;main&gt;'
  </code></pre>
  
  <p>Also when I take the base64 from the openssl command I get the same bad decrypt:</p>
  
  <pre><code>require 'openssl'
  require 'base64'
  
  clear = Base64.decode64("Pd4+UEBW1RyTjARy1rpndQ==")
  
  cipher = OpenSSL::Cipher::Cipher.new 'AES-256-CBC'
  cipher.decrypt
  cipher.key = "2317823178123897237891232345345234524523452345"
  cipher.iv = "123789123789123789"
  plain = cipher.update(clear) + cipher.final
  puts plain
  
  crypt.rb:10:in `final': bad decrypt (OpenSSL::Cipher::CipherError)
      from crypt.rb:10:in `&lt;main&gt;'
  </code></pre>
  
  <p>What I believe is happening here is that OpenSSL is converting my cipher key and IV one way, and ruby is doing it another. For example when you create a key using PBKDF2 you get a string of a bunch of hex values i.e.: "\x00\AF...".</p>
  
  <p>Is that the issue at hand? That I need to convert my strings into a particular format? How do I do that? Is that not the issue?</p>