title: Perl & Ruby exchange AES encrypted information
body: <p>What is the equivalent to Crypt::CBC in Perl for Ruby? </p>
  
  <p>Note: This problem similar to PHP/Perl at <a href="https://stackoverflow.com/questions/655691/php-pear-encrypting-with-cryptcbc-decrypting-with-perls-cbc">stackoverflow:655691</a>.</p>
  
  <p><strong>Perl Version</strong></p>
  
  <pre><code>use Crypt::CBC;
  use MIME::Base64::Perl;
  
  my $cipher = Crypt::CBC-&gt;new(
      -key    =&gt; "95A8EE8E89979B9EFDCBC6EB9797528D",
      -keysize =&gt; 32, 
      -cipher =&gt; "Crypt::OpenSSL::AES"
    );  
  
  $encypted = $cipher-&gt;encrypt("ABCDEFGH12345678");
  $base64 = encode_base64($encypted);
  
  print("$base64"); # output -&gt; U2FsdGVkX18m1jVqRTxANhcEj6aADeOn+2cccDft2eYAMfOkYCvAAkTIOv01VHc/
  
  $de_base64 = decode_base64($base64);
  $decrypted = $cipher-&gt;decrypt($de_base64);
  $c = $cipher-&gt;finish;
  print("$decrypted \n");
  </code></pre>
  
  <p>My ruby version looks like this:</p>
  
  <pre><code>require 'openssl'
  require 'base64'
  
  aes = OpenSSL::Cipher::AES128.new("CBC")
  aes.encrypt
  aes.key = "95A8EE8E89979B9EFDCBC6EB9797528D"
  
  encypted = aes.update("ABCDEFGH12345678") + aes.final
  base64 = Base64.encode64(encypted)
  
  puts base64 # outout -&gt; gx1K24LqlRUtNNTDNUJTyn7HrVKK6UkfNA9LNpNjZeE=
  </code></pre>
  
  <p>I'm pretty sure Base64 work same in Ruby and Perl. Any clue what is the right way to do?</p>
  
  <p><strong>Update (Solution)</strong> </p>
  
  <pre><code>use Crypt::CBC;
  use MIME::Base64;
  
  my $key = "95A8EE8E89979B9E";
  my $iv = "1234567890abcdef";
  
  my $cipher = Crypt::CBC-&gt;new(
                    -key        =&gt; $key,
                    -cipher     =&gt; 'Rijndael',
                    -iv         =&gt; $iv,
                    -literal_key =&gt; 1,
                    -padding =&gt; 'null',
                    -keysize =&gt; 128/8,
                    -header     =&gt; 'none'
                    );  
  my $plaintext = $cipher-&gt;encrypt("Hello");
  print encode_base64($plaintext); # output -&gt; kJCpQC0+iNF8exHGx3GLYw==
  </code></pre>
  
  <p>Ruby</p>
  
  <pre><code>require 'openssl'
  require 'base64'
  
  aes = OpenSSL::Cipher::Cipher.new("aes-128-cbc")
  aes.decrypt
  aes.key = "95A8EE8E89979B9E"
  aes.iv = "1234567890abcdef"
  aes.padding = 0
  
  base64 = Base64.decode64("kJCpQC0+iNF8exHGx3GLYw==")
  decrypted = aes.update(base64)
  decrypted &lt;&lt; aes.final
  
  puts decrypted # guess? It is "Hello"
  </code></pre>