title: How to convert java.security.PrivateKey object to org.bouncycastle.asn1.pkcs.RSAPrivateKey object
body: <p>everyone!
  When I try to convert a java.security.PrivateKey object to an org.bouncycastle.asn1.pkcs.RSAPrivateKey object, like the code show.</p>
  <pre><code>void prvKey2BCRSAPrvKey() throws Exception {
      KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(&quot;RSA&quot;);
      keyPairGenerator.initialize(2048);
      KeyPair keyPair = keyPairGenerator.genKeyPair();
      PrivateKey privateKey = keyPair.getPrivate();
      System.out.println(privateKey.getAlgorithm());
      RSAPrivateKey rsaPrivateKey = RSAPrivateKey.getInstance(privateKey.getEncoded());
      System.out.println(rsaPrivateKey.getVersion());
  }
  </code></pre>
  <p>I always get an error.</p>
  <blockquote>
  <p>java.lang.ClassCastException: org.bouncycastle.asn1.DLSequence cannot be cast to org.bouncycastle.asn1.ASN1Integer</p>
  <p>at org.bouncycastle.asn1.pkcs.RSAPrivateKey.(Unknown Source)</p>
  <p>at org.bouncycastle.asn1.pkcs.RSAPrivateKey.getInstance(Unknown Source)</p>
  </blockquote>
  <p>I know the problem is happening here.</p>
  <pre><code>RSAPrivateKey.getInstance(privateKey.getEncoded())
  </code></pre>
  <p>And more deep reason is in this function which is in org.bouncycastle.asn1.pkcs.RSAPrivateKey:</p>
  <pre><code>private RSAPrivateKey(ASN1Sequence var1) {
      Enumeration var2 = var1.getObjects();
      ASN1Integer var3 = (ASN1Integer)var2.nextElement();
      int var4 = var3.intValueExact();
      if (var4 &gt;= 0 &amp;&amp; var4 &lt;= 1) {
          this.version = var3.getValue();
          this.modulus = ((ASN1Integer)var2.nextElement()).getValue();
          this.publicExponent = ((ASN1Integer)var2.nextElement()).getValue();
          this.privateExponent = ((ASN1Integer)var2.nextElement()).getValue();
          this.prime1 = ((ASN1Integer)var2.nextElement()).getValue();
          this.prime2 = ((ASN1Integer)var2.nextElement()).getValue();
          this.exponent1 = ((ASN1Integer)var2.nextElement()).getValue();
          this.exponent2 = ((ASN1Integer)var2.nextElement()).getValue();
          this.coefficient = ((ASN1Integer)var2.nextElement()).getValue();
          if (var2.hasMoreElements()) {
              this.otherPrimeInfos = (ASN1Sequence)var2.nextElement();
          }
  
      } else {
          throw new IllegalArgumentException(&quot;wrong version for RSA private key&quot;);
      }
  }
  </code></pre>
  <p>When the code runs here, it goes wrong.</p>
  <pre><code>ASN1Integer var3 = (ASN1Integer)var2.nextElement();
  </code></pre>
  <p>But I don't know how to fix it.
  It is some code I write is wrong?
  I use java1.8 and the BC dependent libraries is</p>
  <pre><code>&lt;dependency&gt;
          &lt;groupId&gt;org.bouncycastle&lt;/groupId&gt;
          &lt;artifactId&gt;bcprov-jdk15on&lt;/artifactId&gt;
          &lt;version&gt;1.70&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.bouncycastle&lt;/groupId&gt;
          &lt;artifactId&gt;bcpkix-jdk15on&lt;/artifactId&gt;
          &lt;version&gt;1.70&lt;/version&gt;
      &lt;/dependency&gt;
  </code></pre>
  <p>Someone can help me?
  Very very thanks!!!</p>