title: Java encryption / decryption to Ruby
body: <p>I have a snippet of code in Java responsible for Encryption and Decryption and need to convert it to Ruby. Before posting here I went through 4 Link but had no luck.</p>
  <p><a href="https://stackoverflow.com/questions/18462243/aes-cbc-pkcs5padding-encrypt-in-java-decrypt-in-ruby">aes-cbc-pkcs5padding-encrypt-in-java-decrypt-in-ruby</a></p>
  <p><a href="https://stackoverflow.com/questions/15580065/aes-cbc-pkcs5padding-implementation-in-ruby-for-rails">aes-cbc-pkcs5padding-implementation-in-ruby-for-rails</a></p>
  <p><a href="https://gist.github.com/byu/99651" rel="nofollow noreferrer">gist.github.com</a></p>
  <p><a href="https://medium.com/@Bakku1505/playing-with-symmetric-encryption-algorithms-in-ruby-8652f105341e" rel="nofollow noreferrer">symmetric encryption algorithms in Ruby</a></p>
  <pre><code>* 
      /**
       * 
       */
      package in.bets.gsm.util;
      
      import javax.crypto.Cipher;
      import javax.crypto.spec.IvParameterSpec;
      import javax.crypto.spec.SecretKeySpec;
      
      import org.apache.commons.codec.binary.Base64;
      
      /**
       * @author VKatz
       *
       */
      public class SecurePath {
      
          /**
           * 
           */
          public SecurePath() {
              // TODO Auto-generated constructor stub
          }
          
          public static String key = &quot;Bar12345Bar12345&quot;;
          public static String initVector = &quot;RandomInitVector&quot;; 
          
          public static String encrypt(String value) {
              try {
                  IvParameterSpec iv = new IvParameterSpec(initVector.getBytes(&quot;UTF-8&quot;));
                  SecretKeySpec [skeySpec][4] = new SecretKeySpec(key.getBytes(&quot;UTF-8&quot;), &quot;AES&quot;);
      
                  Cipher cipher = Cipher.getInstance(&quot;AES/CBC/PKCS5PADDING&quot;);
                  cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
      
                  byte[] encrypted = cipher.doFinal(value.getBytes());
                  System.out.println(&quot;encrypted string: &quot;
                          + Base64.encodeBase64String(encrypted));
      
                  return Base64.encodeBase64String(encrypted);
              } catch (Exception ex) {
                  ex.printStackTrace();
              }
      
              return null;
          }
      
          public static String decrypt(String encrypted) {
              try {
                  IvParameterSpec iv = new IvParameterSpec(initVector.getBytes(&quot;UTF-8&quot;));
                  SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(&quot;UTF-8&quot;), &quot;AES&quot;);
      
                  Cipher cipher = Cipher.getInstance(&quot;AES/CBC/PKCS5PADDING&quot;);
                  cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
      
                  byte[] original = cipher.doFinal(Base64.decodeBase64(encrypted));
      
                  return new String(original);
              } catch (Exception ex) {
                  ex.printStackTrace();
              }
      
              return null;
          }
          
      public static void main(String[] args) {
              
              String encText = encrypt(&quot;abceeffslaj&quot;);
             
              System.out.println(&quot;Decripted text ::  &quot; + decrypt(&quot;XZy6gJinORmH+LOiZL6/Jw==&quot;));
          }
      
      }
  
  
  
  Output: 
  Simple Text ::  abceeffslaj
  Encrypted  text ::  XZy6gJinORmH+LOiZL6/Jw==
  Decripted Text ::  abceeffslaj
  </code></pre>
  <p>In order to get the same result I wrote below code</p>
  <p>My effort: Ruby</p>
  <pre><code>require &quot;openssl&quot;
  require &quot;base64&quot;
  require 'byebug'
  
  include Base64
  
  plain_text = &quot;abceeffslaj&quot;
  
  cipher = OpenSSL::Cipher::AES128.new(:CBC)
  cipher.encrypt
  key = cipher.random_key
  iv = cipher.random_iv
  cipher_text = cipher.update(plain_text) + cipher.final
  
  cipher = OpenSSL::Cipher::AES128.new(:CBC)
  cipher.decrypt
  cipher.key = key
  cipher.iv = iv
  decrypted_plain_text = cipher.update(cipher_text) + cipher.final
  
  puts &quot;AES128 in CBC mode&quot;
  puts &quot;Key: &quot; + urlsafe_encode64(key)
  puts &quot;Iv: &quot; + urlsafe_encode64(iv)
  puts &quot;Plain text: &quot; + plain_text
  puts &quot;Cipher text: &quot; + urlsafe_encode64(cipher_text)
  puts &quot;Decrypted plain text: &quot; + decrypted_plain_text
  </code></pre>
  <p>Output:</p>
  <pre><code>AES128 in CBC mode
  Key: CJ-SNuUllNKl1vAllEazKg==
  Iv: ZMb2W6K07oaAXuvoL8Ckpg==
  Plain text: abceeffslaj
  Cipher text: jyutt1ljXW9Xn-HFxpvcEg==
  Decrypted plain text: abceeffslaj
  </code></pre>
  <p>As we might can notice here cipher text is difference from Java code.</p>
  <p>Any help would be highly appreciate!</p>