title: Get this "RSA/ECB/OAEPWithSHA-256AndMGF1Padding" combination for ruby
body: <p>I have this java code for encryption and decryption, which I want to change/convert to Ruby code. I looked up in OpenSSL gem but dint find the &quot;RSA/ECB/OAEPWithSHA-256AndMGF1Padding&quot; combination available for ruby. How do I implement it?</p>
  <pre><code>public class EncryptDecryptService {
      
      public String encryptRequestObject(RequestObject requestObject) throws UnsupportedEncodingException, FileNotFoundException, CertificateException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
          PublicKey publicKey = getPublicKey(requestObject.getKeyFilename());
          byte[] message = requestObject.getString().getBytes(&quot;UTF-8&quot;);
          byte[] secret = encrypt(publicKey, message);
          return Base64.encodeBase64String(secret);
      }
      
      public String decryptRequestObject(RequestObject requestObject) throws UnrecoverableKeyException, KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, InvalidKeyException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
          PrivateKey privateKey = getPrivateKey(requestObject.getKeyFilename(), requestObject.getKeyPassword());
          byte[] cipherText = Base64.decodeBase64(requestObject.getString());
          byte[] decrypted = decrypt(privateKey, cipherText);
          return new String(decrypted, &quot;UTF-8&quot;);
      }
      
      private PublicKey getPublicKey(String filename) throws FileNotFoundException, CertificateException {
          FileInputStream fin = new FileInputStream(filename);
          CertificateFactory factory = CertificateFactory.getInstance(&quot;X.509&quot;);
          X509Certificate certificate = (X509Certificate) factory.generateCertificate(fin);
          PublicKey publicKey = certificate.getPublicKey();
          return publicKey;
      }
      
      private PrivateKey getPrivateKey(String filename, String password) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, UnrecoverableKeyException {
          FileInputStream fin = new FileInputStream(filename);
          KeyStore ks = KeyStore.getInstance(&quot;pkcs12&quot;);
          ks.load(fin, password.toCharArray());
          String str = ks.aliases().nextElement();
          PrivateKey privateKey = (PrivateKey) ks.getKey(str, password.toCharArray());
          return privateKey;
      }
      
      private byte[] encrypt(PublicKey key, byte[] plainText) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
          Cipher cipher = Cipher.getInstance(&quot;RSA/ECB/OAEPWithSHA-256AndMGF1Padding&quot;);
          cipher.init(Cipher.ENCRYPT_MODE, key);
          return cipher.doFinal(plainText);
      }
      
      private byte[] decrypt(PrivateKey key, byte[] cipherText) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
          Cipher cipher = Cipher.getInstance(&quot;RSA/ECB/OAEPWithSHA-256AndMGF1Padding&quot;);
          cipher.init(Cipher.DECRYPT_MODE, key);
          return cipher.doFinal(cipherText);
      }
  }
  </code></pre>