Android. KeyFactory RSA implementation not found
<p>I have a class that extends Application. In onCreate() method I start some thread, that calls:</p>

<pre><code>KeyFactory.getInstance("RSA")
</code></pre>

<p>Normally it works perfectly, but from time to time (very rare) I get such exception:</p>

<pre><code>W/System.err(24537): java.security.NoSuchAlgorithmException: KeyFactory RSA implementation not found
W/System.err(24537):    at org.apache.harmony.security.fortress.Engine.notFound(Engine.java:177)
W/System.err(24537):    at org.apache.harmony.security.fortress.Engine.getInstance(Engine.java:151)
W/System.err(24537):    at java.security.KeyFactory.getInstance(KeyFactory.java:81)
</code></pre>

<p>It looks like a race... I think the security system is not initialized at moment my thread is calling for "RSA". </p>

<p>Is this a known issue? Can anyone provide me with some hints?</p>

<p>I get this issue on 4.0.3 device (don't know about other versions).</p>

<p><strong>More details:</strong></p>

<p>I've tried such code:</p>

<pre><code>Provider providers[] = Security.getProviders();

try {
    k1 = KeyFactory.getInstance("RSA");
}
catch(Exception e) {
    e1 = e;
}

try {
    k2 = KeyFactory.getInstance("RSA", "BC");
}
catch(Exception e) {
    e2 = e;
}

try {
    k3 = KeyFactory.getInstance("RSA");
}
catch(Exception e) {
    e3 = e;
}

if(k1 == null || k2 == null || k3 == null) {
    if(e1 != null)
        e1.printStackTrace();

    if(e2 != null)
        e2.printStackTrace();

    if(e3 != null)
        e3.printStackTrace();
}

for(Provider provider : providers) {
    System.out.println(provider.getName());
}
</code></pre>

<p>And this is what I get from time to time:</p>

<pre><code>k1 is null
k2 is &lt;key&gt;
k3 is null


e1.printStackTrace();

    04-20 22:09:33.322: W/System.err(17249): java.security.NoSuchAlgorithmException: KeyFactory RSA implementation not found
    04-20 22:09:33.322: W/System.err(17249):    at org.apache.harmony.security.fortress.Engine.notFound(Engine.java:177)
    04-20 22:09:33.322: W/System.err(17249):    at org.apache.harmony.security.fortress.Engine.getInstance(Engine.java:151)
    04-20 22:09:33.322: W/System.err(17249):    at java.security.KeyFactory.getInstance(KeyFactory.java:81)
    ...

e2 
    null

e3.printStackTrace();
    04-20 22:10:08.512: W/System.err(17249): java.security.NoSuchAlgorithmException: KeyFactory RSA implementation not found
    04-20 22:10:08.532: W/System.err(17249):    at org.apache.harmony.security.fortress.Engine.notFound(Engine.java:177)
    04-20 22:10:08.532: W/System.err(17249):    at org.apache.harmony.security.fortress.Engine.getInstance(Engine.java:151)
    04-20 22:10:08.542: W/System.err(17249):    at java.security.KeyFactory.getInstance(KeyFactory.java:81)
    ...


providers

    AndroidOpenSSL
    DRLCertFactory
    BC
    Crypto
    HarmonyJSSE
    MyProvider
</code></pre>

<p>While most of the time I get all k1, k2, k3 initialized... </p>

<p>It sounds like getInstance("RSA", "BC") is a solution (althought I don't understand why), but the problem is that BouncyCastle can be missed on some Android devices (as far as I know), so I should NOT specify "BC" in this call... So again, I have a problem.</p>
