title: Encrypt Ruby decrypt Java
body: <p>Good afternoon. Interested in the question someone tried to decrypt data in Java that was encrypted in Ruby.
  I'm trying to encrypt a word in Ruby (using the Marshal module) and decrypt in Java. If the Marshal module is used, can it be transferred to other languages ​​or not?</p>
  <p>This is my test in Ruby:</p>
  <pre><code>let(:key) { &quot;12345678901234567890123456789012&quot; }
  let(:str) { &quot;Serhii&quot; }
  
  it &quot;encrypt_content&quot; do
    crypt = ActiveSupport::MessageEncryptor.new(key, cipher: 'aes-256-cbc')
    encrypted_content = crypt.encrypt_and_sign(str)
    encrypted_content
  end
      
  </code></pre>
  <p>The code methods are:</p>
  <pre><code>def encrypt_and_sign(value, expires_at: nil, expires_in: nil, purpose: nil)
    verifier.generate(_encrypt(value, expires_at: expires_at, expires_in: expires_in, purpose: purpose))
  end
  
  def _encrypt(value, **metadata_options)
    cipher = new_cipher
    cipher.encrypt
    cipher.key = @secret
  
    iv = cipher.random_iv
    cipher.auth_data = &quot;&quot; if aead_mode?
  
    encrypted_data = cipher.update(Messages::Metadata.wrap(@serializer.dump(value), metadata_options))
    encrypted_data &lt;&lt; cipher.final
  
    blob = &quot;#{::Base64.strict_encode64 encrypted_data}--#{::Base64.strict_encode64 iv}&quot;`enter code here`
    blob = &quot;#{blob}--#{::Base64.strict_encode64 cipher.auth_tag}&quot; if aead_mode?
    blob
  end
      
  </code></pre>
  <p>The decrypt Java is:</p>
  <pre><code>private static final String key = &quot;12345678901234567890123456789012&quot;;
  
  @SneakyThrows
  public static String decrypt(String encrypted) {
      byte[] firstByte = Base64.getDecoder().decode(encrypted.replaceAll(&quot;\n&quot;, &quot;&quot;).getBytes(StandardCharsets.UTF_8));
      String first = new String(firstByte);
      String[] parts = first.split(&quot;--&quot;);
      byte[] secondByte = Base64.getDecoder().decode(parts[0].getBytes(StandardCharsets.UTF_8));
      String second = new String(secondByte);
      String[] parts2 = second.split(&quot;--&quot;);
  
      byte[] encryptedData = Base64.getDecoder().decode(parts2[0].getBytes(StandardCharsets.UTF_8));
  
      SecretKeySpec aesKey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), &quot;AES&quot;);
  
      Cipher cipher = Cipher.getInstance(&quot;AES/CBC/PKCS5Padding&quot;);
      cipher.init(Cipher.DECRYPT_MODE, aesKey, new IvParameterSpec(new byte[16]));
      byte[] result = cipher.doFinal(encryptedData);
  
      return new String(result);
  }
  
  public static void main(String[] args) throws Exception {
      String encrypted = &quot;S3l0cVEybDRUM2sxU1hFMk5YVlhOa3A2VXpRNFEyZFFibTVwZVdRMVdEUlpN\n&quot; +
              &quot;bkkxUzBaUGNsbFJaejB0TFRWWlVtVkNVWEJXZWxselJuWkVhbFJyWlU5VmNr\n&quot; +
              &quot;RTlQUT09LS0yZDA5M2FhZTg0OTJjZmIyZjdiNDA0ZWVkNGU2ZmQ4NDQ1ZTM4\n&quot; +
              &quot;ZjIx&quot;;
      System.out.println(&quot;Decrypted: &quot; + decrypt(encrypted));
      }
  }
  </code></pre>
  <p>Result  �'��m�Qի���
  What could be the reason?</p>