title: Rebuild of a RSA Private Key from modulus & exponent fails
body: <p>I'm trying to rebuild a RSA keypair from modulus &amp; private/public exponents. The conversion works correct for the public key but fails to private key when comparing the encoded private keys.</p>
  <p>When using this rebuild private/public keypair for encryption it works (!) in Java, but when using the rebuild keypair in PHP, the decryption part fails (encryption is working), so it seems to me that the rebuild private key is something different to the &quot;original&quot; private key.</p>
  <p>Just for info: using the &quot;original&quot; keypair everything is working fine in PHP.</p>
  <p>So my question: how can I retrieve the &quot;original&quot; private key from (BigInteger) modulus &amp; private exponent?</p>
  <p><strong>Edit: see my final edit at the end</strong></p>
  <p>My sample code shows the equality of public key vs. rebuild one and that the private keys are different:</p>
  <pre><code>Rebuilding of a RSA PrivateKey from modulus &amp; exponent
  privateKey equals rebuild: false
  publicKey equals rebuild: true
  </code></pre>
  <p>code:</p>
  <pre><code>import java.math.BigInteger;
  import java.security.*;
  import java.security.interfaces.RSAPrivateKey;
  import java.security.interfaces.RSAPublicKey;
  import java.security.spec.InvalidKeySpecException;
  import java.security.spec.RSAPrivateKeySpec;
  import java.security.spec.RSAPublicKeySpec;
  import java.util.Arrays;
  
  public class RebuildRSAPrivateKey {
      public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeySpecException {
          System.out.println(&quot;Rebuilding of a RSA PrivateKey from modulus &amp; exponent&quot;);
          // rsa key generation
          KeyPairGenerator kpGen = KeyPairGenerator.getInstance(&quot;RSA&quot;);
          //kpGen.initialize(2048, new SecureRandom());
          kpGen.initialize(2048, new SecureRandom());
          KeyPair keyPair = kpGen.generateKeyPair();
          // private key
          PrivateKey privateKey = keyPair.getPrivate();
          // get modulus &amp; exponent
          RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) privateKey;
          BigInteger modulus = rsaPrivateKey.getModulus();
          BigInteger privateExponent = rsaPrivateKey.getPrivateExponent();
          // rebuild the private key
          KeyFactory keyFactory = KeyFactory.getInstance(&quot;RSA&quot;);
          RSAPrivateKeySpec rsaPrivateKeySpec = new RSAPrivateKeySpec(modulus, privateExponent);
          PrivateKey privateKeyRebuild = keyFactory.generatePrivate(rsaPrivateKeySpec);
          System.out.println(&quot;privateKey equals rebuild: &quot; + Arrays.equals(privateKey.getEncoded(), privateKeyRebuild.getEncoded()));
          // public key
          PublicKey publicKey = keyPair.getPublic();
          // get modulus &amp; exponent
          RSAPublicKey rsaPublicKey = (RSAPublicKey) publicKey;
          BigInteger modulusPub = rsaPublicKey.getModulus();
          BigInteger publicExponent = rsaPublicKey.getPublicExponent();
          // rebuild the public key
          KeyFactory keyFactoryPub = KeyFactory.getInstance(&quot;RSA&quot;);
          RSAPublicKeySpec rsaPublicKeySpec = new RSAPublicKeySpec(modulusPub, publicExponent);
          PublicKey publicKeyRebuild = keyFactory.generatePublic(rsaPublicKeySpec);
          System.out.println(&quot;publicKey equals rebuild: &quot; + Arrays.equals(publicKey.getEncoded(), publicKeyRebuild.getEncoded()));
      }
  }
  </code></pre>
  <p><strong>Edit:</strong> The following programs will show that a RSA private/public keypair derived from encoded keys can get restored and the
  encryption and decryption works in Java and PHP. The keys are <strong>insecure</strong> RSA 512 bit keys and Base64 decoded.</p>
  <p>The same keys are then derived from modulus and private/public exponents and <strong>the en-/decryption works in Java but not in PHP</strong>.</p>
  <p>That's why I'd like to get the &quot;original&quot; RSA keys from modulus and exponents, thanks for your kindly help.</p>
  <p>Result of Java program:</p>
  <pre><code>Rebuilding of a RSA PrivateKey from modulus &amp; exponent v4
  privateKey Original Base64: MIIBVgIBADANBgkqhkiG9w0BAQEFAASCAUAwggE8AgEAAkEA2wFgcni89ijJ/uijQkzCGF4JiUB1+mEJ48u4Lk0vxB7ym3/FCvOEnN2H7FLUzsGvXRhFriLBiSJlg2tOhV5eiwIDAQABAkEAkDpf4gNRrms+W/mpSshyKsoDTbh9+d5ePP601QlQI79lrsjdy2GLgk4RV1XmwYinM9Sk8G+ssyXTYHdby6A2wQIhAPcRtl6tub6PFiIE1jcuIkib/HzAdRYHZx3ZdzRTYDetAiEA4uv43xpGl5N8yG27Kv0DkRoOlr4Ch6oM24hLVw7ClhcCIFgdRAo+MQlqJH2bdf6WAHoez4x6YwepOjhmD2Jk/eK9AiEAtHgI6J5EEB56+gfS+CBa6tZ3Tcl1x6ElMp8Vk/ooJScCIQDUa3LUkcc58yjJYq8ZNQC/86+HIzd5MldTwg5buR1lpw==
  privateKey Rebuild  Base64: MIIBVgIBADANBgkqhkiG9w0BAQEFAASCAUAwggE8AgEAAkEA2wFgcni89ijJ/uijQkzCGF4JiUB1+mEJ48u4Lk0vxB7ym3/FCvOEnN2H7FLUzsGvXRhFriLBiSJlg2tOhV5eiwIDAQABAkEAkDpf4gNRrms+W/mpSshyKsoDTbh9+d5ePP601QlQI79lrsjdy2GLgk4RV1XmwYinM9Sk8G+ssyXTYHdby6A2wQIhAPcRtl6tub6PFiIE1jcuIkib/HzAdRYHZx3ZdzRTYDetAiEA4uv43xpGl5N8yG27Kv0DkRoOlr4Ch6oM24hLVw7ClhcCIFgdRAo+MQlqJH2bdf6WAHoez4x6YwepOjhmD2Jk/eK9AiEAtHgI6J5EEB56+gfS+CBa6tZ3Tcl1x6ElMp8Vk/ooJScCIQDUa3LUkcc58yjJYq8ZNQC/86+HIzd5MldTwg5buR1lpw==
  publicKey           Base64: MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANsBYHJ4vPYoyf7oo0JMwhheCYlAdfphCePLuC5NL8Qe8pt/xQrzhJzdh+xS1M7Br10YRa4iwYkiZYNrToVeXosCAwEAAQ==
  
  generate private &amp; public key via modulus and private/public exponent
  privateKey Modulus  Base64: MIGzAgEAMA0GCSqGSIb3DQEBAQUABIGeMIGbAgEAAkEA2wFgcni89ijJ/uijQkzCGF4JiUB1+mEJ48u4Lk0vxB7ym3/FCvOEnN2H7FLUzsGvXRhFriLBiSJlg2tOhV5eiwIBAAJBAJA6X+IDUa5rPlv5qUrIcirKA024ffneXjz+tNUJUCO/Za7I3cthi4JOEVdV5sGIpzPUpPBvrLMl02B3W8ugNsECAQACAQACAQACAQACAQA=
  publicKey  Modulus  Base64: MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANsBYHJ4vPYoyf7oo0JMwhheCYlAdfphCePLuC5NL8Qe8pt/xQrzhJzdh+xS1M7Br10YRa4iwYkiZYNrToVeXosCAwEAAQ==
  
  en-/decryption with original keys
  ciphertext Original   : fvFPRZ5B2GMgv9aXQjyQsxnRHK2wotfXlLV+zGea1E3nsZC6RMn+LQMOe9yvZ8IcaG2F/8wWv2NkNmBX4wuxaw==
  decryptedtext Original: this is the message to encrypt
  
  en-/decryption with keys from modulus &amp; exponent
  ciphertext Modulus    : o0tB4xQIwQRFDSsWj1WgWHexXnJOp9jeBymFPJvy+xZBvfJay2yR0XZEy+0VwaedxdTf9CoyKVvgCbn2HCohSQ==
  decryptedtext Modulus : this is the message to encrypt
  </code></pre>
  <p>Result of PHP program:</p>
  <pre><code>php version: 7.4.6 openssl version: OpenSSL 1.1.1g 21 Apr 2020
  plaintext: this is the message to encrypt
  rsa encryption with original keys
  priBase64:MIIBVgIBADANBgkqhkiG9w0BAQEFAASCAUAwggE8AgEAAkEA2wFgcni89ijJ/uijQkzCGF4JiUB1+mEJ48u4Lk0vxB7ym3/FCvOEnN2H7FLUzsGvXRhFriLBiSJlg2tOhV5eiwIDAQABAkEAkDpf4gNRrms+W/mpSshyKsoDTbh9+d5ePP601QlQI79lrsjdy2GLgk4RV1XmwYinM9Sk8G+ssyXTYHdby6A2wQIhAPcRtl6tub6PFiIE1jcuIkib/HzAdRYHZx3ZdzRTYDetAiEA4uv43xpGl5N8yG27Kv0DkRoOlr4Ch6oM24hLVw7ClhcCIFgdRAo+MQlqJH2bdf6WAHoez4x6YwepOjhmD2Jk/eK9AiEAtHgI6J5EEB56+gfS+CBa6tZ3Tcl1x6ElMp8Vk/ooJScCIQDUa3LUkcc58yjJYq8ZNQC/86+HIzd5MldTwg5buR1lpw==
  pubBase64:MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANsBYHJ4vPYoyf7oo0JMwhheCYlAdfphCePLuC5NL8Qe8pt/xQrzhJzdh+xS1M7Br10YRa4iwYkiZYNrToVeXosCAwEAAQ==
  ciphertext Base64:WmvVwqf2EHQc0yb6L4pVJ0/23pNW4QsBun3SNvYE8p/sEk+1GQSYxYpbY/mLbSGF2Lb1P5g5er+z7dWxHmodNA==
  decryptedtext: this is the message to encrypt
  
  rsa encryption with keys created via modulus &amp; exponents
  priBase64:MIGzAgEAMA0GCSqGSIb3DQEBAQUABIGeMIGbAgEAAkEA2wFgcni89ijJ/uijQkzCGF4JiUB1+mEJ48u4Lk0vxB7ym3/FCvOEnN2H7FLUzsGvXRhFriLBiSJlg2tOhV5eiwIBAAJBAJA6X+IDUa5rPlv5qUrIcirKA024ffneXjz+tNUJUCO/Za7I3cthi4JOEVdV5sGIpzPUpPBvrLMl02B3W8ugNsECAQACAQACAQACAQACAQA=
  pubBase64:MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANsBYHJ4vPYoyf7oo0JMwhheCYlAdfphCePLuC5NL8Qe8pt/xQrzhJzdh+xS1M7Br10YRa4iwYkiZYNrToVeXosCAwEAAQ==
  ciphertext Base64:kqn8aZpvfpPzr3u2NBX/XmnlFweEvOm+Qu4l2wiUSQCjA0hutQ10mbLaO55oCox7GixvMgb3VtoDBJ8hfW1zbQ==
  Cannot Decrypt error:0407109F:rsa routines:RSA_padding_check_PKCS1_type_2:pkcs decoding error
  decryptedtext:
  
  decrypt error: error:0909006C:PEM routines:get_name:no start line
  </code></pre>
  <p>Source Java:</p>
  <pre><code>import javax.crypto.BadPaddingException;
  import javax.crypto.Cipher;
  import javax.crypto.IllegalBlockSizeException;
  import javax.crypto.NoSuchPaddingException;
  import java.io.IOException;
  import java.math.BigInteger;
  import java.security.*;
  import java.security.interfaces.RSAPrivateKey;
  import java.security.interfaces.RSAPublicKey;
  import java.security.spec.*;
  import java.util.Base64;
  
  public class RebuildRSAPrivateKey4 {
      public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeySpecException, IllegalBlockSizeException, InvalidKeyException, BadPaddingException, NoSuchPaddingException, IOException {
          System.out.println(&quot;Rebuilding of a RSA PrivateKey from modulus &amp; exponent v4&quot;);
          // rsa key generation
          KeyPairGenerator kpGen = KeyPairGenerator.getInstance(&quot;RSA&quot;);
          //kpGen.initialize(2048, new SecureRandom());
          kpGen.initialize(512, new SecureRandom()); // don't use 512 bit keys as they are insecure !!
          KeyPair keyPair = kpGen.generateKeyPair();
  
          // privateKey   Base64: MIIBVgIBADANBgkqhkiG9w0BAQEFAASCAUAwggE8AgEAAkEA2wFgcni89ijJ/uijQkzCGF4JiUB1+mEJ48u4Lk0vxB7ym3/FCvOEnN2H7FLUzsGvXRhFriLBiSJlg2tOhV5eiwIDAQABAkEAkDpf4gNRrms+W/mpSshyKsoDTbh9+d5ePP601QlQI79lrsjdy2GLgk4RV1XmwYinM9Sk8G+ssyXTYHdby6A2wQIhAPcRtl6tub6PFiIE1jcuIkib/HzAdRYHZx3ZdzRTYDetAiEA4uv43xpGl5N8yG27Kv0DkRoOlr4Ch6oM24hLVw7ClhcCIFgdRAo+MQlqJH2bdf6WAHoez4x6YwepOjhmD2Jk/eK9AiEAtHgI6J5EEB56+gfS+CBa6tZ3Tcl1x6ElMp8Vk/ooJScCIQDUa3LUkcc58yjJYq8ZNQC/86+HIzd5MldTwg5buR1lpw==
          // publicKey    Base64: MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANsBYHJ4vPYoyf7oo0JMwhheCYlAdfphCePLuC5NL8Qe8pt/xQrzhJzdh+xS1M7Br10YRa4iwYkiZYNrToVeXosCAwEAAQ==
          String privateKeyBase64 = &quot;MIIBVgIBADANBgkqhkiG9w0BAQEFAASCAUAwggE8AgEAAkEA2wFgcni89ijJ/uijQkzCGF4JiUB1+mEJ48u4Lk0vxB7ym3/FCvOEnN2H7FLUzsGvXRhFriLBiSJlg2tOhV5eiwIDAQABAkEAkDpf4gNRrms+W/mpSshyKsoDTbh9+d5ePP601QlQI79lrsjdy2GLgk4RV1XmwYinM9Sk8G+ssyXTYHdby6A2wQIhAPcRtl6tub6PFiIE1jcuIkib/HzAdRYHZx3ZdzRTYDetAiEA4uv43xpGl5N8yG27Kv0DkRoOlr4Ch6oM24hLVw7ClhcCIFgdRAo+MQlqJH2bdf6WAHoez4x6YwepOjhmD2Jk/eK9AiEAtHgI6J5EEB56+gfS+CBa6tZ3Tcl1x6ElMp8Vk/ooJScCIQDUa3LUkcc58yjJYq8ZNQC/86+HIzd5MldTwg5buR1lpw==&quot;;
          String publicKeyBase64 = &quot;MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANsBYHJ4vPYoyf7oo0JMwhheCYlAdfphCePLuC5NL8Qe8pt/xQrzhJzdh+xS1M7Br10YRa4iwYkiZYNrToVeXosCAwEAAQ==&quot;;
          KeyFactory keyFactory = KeyFactory.getInstance(&quot;RSA&quot;);
          PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(privateKeyBase64));
          PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);
          X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(Base64.getDecoder().decode(publicKeyBase64));
          PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);
          System.out.println(&quot;privateKey Original Base64: &quot; + privateKeyBase64);
          System.out.println(&quot;privateKey Rebuild  Base64: &quot; + Base64.getEncoder().encodeToString(privateKey.getEncoded()));
          System.out.println(&quot;publicKey           Base64: &quot; + publicKeyBase64);
          // get modulus &amp; private exponent via RSAPrivateKey
          RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) privateKey;
          BigInteger modulus = rsaPrivateKey.getModulus();
          BigInteger privateExponent = rsaPrivateKey.getPrivateExponent();
          // rebuild the private key
          RSAPrivateKeySpec rsaPrivateKeySpec = new RSAPrivateKeySpec(modulus, privateExponent);
          PrivateKey privateKeyModulusExponent = keyFactory.generatePrivate(rsaPrivateKeySpec);
          // public key
          RSAPublicKey rsaPublicKey = (RSAPublicKey) publicKey;
          BigInteger modulusPub = rsaPublicKey.getModulus();
          BigInteger publicExponent = rsaPublicKey.getPublicExponent();
          // rebuild the public key
          RSAPublicKeySpec rsaPublicKeySpec = new RSAPublicKeySpec(modulusPub, publicExponent);
          PublicKey publicKeyModulusExponent = keyFactory.generatePublic(rsaPublicKeySpec);
          System.out.println(&quot;\ngenerate private &amp; public key via modulus and private/public exponent&quot;);
          System.out.println(&quot;privateKey Modulus  Base64: &quot; + Base64.getEncoder().encodeToString(privateKeyModulusExponent.getEncoded()));
          System.out.println(&quot;publicKey  Modulus  Base64: &quot; + Base64.getEncoder().encodeToString(publicKeyModulusExponent.getEncoded()));
          System.out.println(&quot;\nen-/decryption with original keys&quot;);
          String plaintext = &quot;this is the message to encrypt&quot;;
          String ciphertextOriginal = encrypt(publicKey, plaintext);
          String decryptedtextOriginal = decrypt(privateKey, ciphertextOriginal);
          System.out.println(&quot;ciphertext Original   : &quot; + ciphertextOriginal);
          System.out.println(&quot;decryptedtext Original: &quot; + decryptedtextOriginal);
          System.out.println(&quot;\nen-/decryption with keys from modulus &amp; exponent&quot;);
          String ciphertextModulus = encrypt(publicKeyModulusExponent, plaintext);
          String decryptedtextModulus = decrypt(privateKeyModulusExponent, ciphertextOriginal);
          System.out.println(&quot;ciphertext Modulus    : &quot; + ciphertextModulus);
          System.out.println(&quot;decryptedtext Modulus : &quot; + decryptedtextModulus);
      }
  
      private static String encrypt(PublicKey publicKey, String plaintext) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IOException, BadPaddingException, IllegalBlockSizeException {
          String ciphertext = &quot;&quot;;
          Cipher cipher = Cipher.getInstance(&quot;RSA/ECB/PKCS1Padding&quot;);
          cipher.init(Cipher.ENCRYPT_MODE, publicKey);
          byte[] ciphertextByte = cipher.doFinal(plaintext.getBytes(&quot;UTF8&quot;));
          ciphertext = Base64.getEncoder().encodeToString(ciphertextByte).replaceAll(&quot;\\r|\\n&quot;, &quot;&quot;);
          return ciphertext;
      }
  
      private static String decrypt(PrivateKey privateKey, String ciphertext) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
          Cipher cipher = Cipher.getInstance(&quot;RSA/ECB/PKCS1Padding&quot;);
          cipher.init(Cipher.DECRYPT_MODE, privateKey);
          byte[] ciphertextByte = Base64.getDecoder().decode(ciphertext);
          byte[] decryptedtextByte = cipher.doFinal(ciphertextByte);
          return new String(decryptedtextByte);
      }
      private static String bytesToHex(byte[] bytes) {
          StringBuffer result = new StringBuffer();
          for (byte b : bytes) result.append(Integer.toString((b &amp; 0xff) + 0x100, 16).substring(1));
          return result.toString();
      }
  
  }
  </code></pre>
  <p>Source PHP:</p>
  <pre><code>&lt;?php
  
  function encrypt($publicKeyBase64, $plaintext){
      $pub = base64_decode($publicKeyBase64);
      // public key conversion der to pem
      $pubPem = chunk_split(base64_encode($pub), 64, &quot;\n&quot;);
      $pubPem = &quot;-----BEGIN PUBLIC KEY-----\n&quot; . $pubPem . &quot;-----END PUBLIC KEY-----\n&quot;;
      $ublicKey = &quot;&quot;;
      $publicKey = openssl_get_publickey($pubPem);
      if (!$publicKey) {
          echo &quot;Cannot get public key&quot; . &quot;&lt;br&gt;&quot;;
      }
      $ciphertext = &quot;&quot;;
      openssl_public_encrypt($plaintext, $ciphertext, $publicKey);
      if (!empty($ciphertext)) {
          openssl_free_key($publicKey);
          //echo &quot;Encryption OK!&quot; . &quot;&lt;br&gt;&quot;;
      } else {
          echo &quot;Cannot Encrypt&quot; . &quot;&lt;br&gt;&quot;;
      }
      $ciphertextBase64 = base64_encode($ciphertext);
      return $ciphertextBase64;
  }
  
  function decrypt($privateKeyBase64, $ciphertext){
      $pri = base64_decode($privateKeyBase64);
      // private key conversion der to pem
      $priPem = chunk_split(base64_encode($pri), 64, &quot;\n&quot;);
      $priPem = &quot;-----BEGIN PRIVATE KEY-----\n&quot; . $priPem . &quot;-----END PRIVATE KEY-----\n&quot;;
      $privateKey = openssl_get_privatekey($priPem);
      $Crypted = openssl_private_decrypt($ciphertext, $decryptedtext, $privateKey);
      if (!$Crypted) {
          echo 'Cannot Decrypt ' . openssl_error_string() . '&lt;br&gt;';
      } else {
          openssl_free_key($privateKey);
          //echo &quot;decryptedtext: &quot; . $decryptedtext . &quot;&lt;br&gt;&quot;;
      }
      return $decryptedtext;
  }
  
  echo 'php version: ' . PHP_VERSION . ' openssl version: ' . OPENSSL_VERSION_TEXT . '&lt;br&gt;';
  $plaintext = &quot;this is the message to encrypt&quot;;
  echo &quot;plaintext: &quot; . $plaintext . &quot;&lt;br&gt;&quot;;
  
  // RSA 512 keys from Java GenerateKeysSo.java
  echo 'rsa encryption with original keys' . '&lt;br&gt;';
  $priBase64 = &quot;MIIBVgIBADANBgkqhkiG9w0BAQEFAASCAUAwggE8AgEAAkEA2wFgcni89ijJ/uijQkzCGF4JiUB1+mEJ48u4Lk0vxB7ym3/FCvOEnN2H7FLUzsGvXRhFriLBiSJlg2tOhV5eiwIDAQABAkEAkDpf4gNRrms+W/mpSshyKsoDTbh9+d5ePP601QlQI79lrsjdy2GLgk4RV1XmwYinM9Sk8G+ssyXTYHdby6A2wQIhAPcRtl6tub6PFiIE1jcuIkib/HzAdRYHZx3ZdzRTYDetAiEA4uv43xpGl5N8yG27Kv0DkRoOlr4Ch6oM24hLVw7ClhcCIFgdRAo+MQlqJH2bdf6WAHoez4x6YwepOjhmD2Jk/eK9AiEAtHgI6J5EEB56+gfS+CBa6tZ3Tcl1x6ElMp8Vk/ooJScCIQDUa3LUkcc58yjJYq8ZNQC/86+HIzd5MldTwg5buR1lpw==&quot;;
  $pubBase64 = &quot;MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANsBYHJ4vPYoyf7oo0JMwhheCYlAdfphCePLuC5NL8Qe8pt/xQrzhJzdh+xS1M7Br10YRa4iwYkiZYNrToVeXosCAwEAAQ==&quot;;
  echo 'priBase64:' . $priBase64 . '&lt;br&gt;';
  echo 'pubBase64:' . $pubBase64 . '&lt;br&gt;';
  $ciphertextBase64 = encrypt($pubBase64, $plaintext);
  echo 'ciphertext Base64:' . $ciphertextBase64 . '&lt;br&gt;';
  $ciphertext = base64_decode($ciphertextBase64);
  $decryptedtext = decrypt($priBase64, $ciphertext);
  echo &quot;decryptedtext: &quot; . $decryptedtext . &quot;&lt;br&gt;&lt;br&gt;&quot;;
  
  // keys created via modulus &amp; exponent
  $priBase64 = &quot;MIGzAgEAMA0GCSqGSIb3DQEBAQUABIGeMIGbAgEAAkEA2wFgcni89ijJ/uijQkzCGF4JiUB1+mEJ48u4Lk0vxB7ym3/FCvOEnN2H7FLUzsGvXRhFriLBiSJlg2tOhV5eiwIBAAJBAJA6X+IDUa5rPlv5qUrIcirKA024ffneXjz+tNUJUCO/Za7I3cthi4JOEVdV5sGIpzPUpPBvrLMl02B3W8ugNsECAQACAQACAQACAQACAQA=&quot;;
  $pubBase64 = &quot;MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANsBYHJ4vPYoyf7oo0JMwhheCYlAdfphCePLuC5NL8Qe8pt/xQrzhJzdh+xS1M7Br10YRa4iwYkiZYNrToVeXosCAwEAAQ==&quot;;
  echo 'rsa encryption with keys created via modulus &amp; exponents' . '&lt;br&gt;';
  echo 'priBase64:' . $priBase64 . '&lt;br&gt;';
  echo 'pubBase64:' . $pubBase64 . '&lt;br&gt;';
  $ciphertextBase64 = encrypt($pubBase64, $plaintext);
  echo 'ciphertext Base64:' . $ciphertextBase64 . '&lt;br&gt;';
  $ciphertext = base64_decode($ciphertextBase64);
  $decryptedtext = decrypt($priBase64, $ciphertext);
  echo &quot;decryptedtext: &quot; . $decryptedtext . &quot;&lt;br&gt;&lt;br&gt;&quot;;
  echo 'decrypt error: error:0909006C:PEM routines:get_name:no start line' . '&lt;br&gt;';
  ?&gt;
  </code></pre>
  <p><strong>Final Edit solution &amp; conclusion</strong></p>
  <p>If we want to use a RSA private-public keypair for encryption (and signing as well ?) in other systems than Java it is of importance that the private key is saved immeditaly. If we are trying to rebuild the private key from the encoded form (via  PKCS8EncodedKeySpec) some data are definitely missing. <strong>Those rebuild private keys will fail to work</strong> (here in PHP/openssl).</p>
  <p>If we need to rebuild a private key from the encoded-form (byte[]) the keys need to get enhanced via a method called &quot;createCrtKey&quot; - this method was written by President James K. Polk and all credits go to him. As links sometimes date out I marked my own answer below as accepted one because the createCrtKey-method is documented there.</p>
  <p>Thanks to @President James K. Polk, @Topaco and @michalk for guiding me into the right direction.</p>