I have a class that extends Application. In the onCreate () method, I start some kind of thread that calls:
KeyFactory.getInstance("RSA")
It usually works fine, but from time to time (very rarely) I get this exception:
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)
It looks like a race ... I think that the security system is not initialized at the moment when my thread calls "RSA".
Is this a known issue? Can someone give me some advice?
I get this problem on device 4.0.3 (don't know about other versions).
More details:
I tried a code like this:
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());
}
And this is what I get from time to time:
k1 is null
k2 is <key>
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
While most of the time I get all initialized k1, k2, k3 ...
, getInstance ( "RSA", "BC" ) - ( , ), , BouncyCastle Android ( ) "BC" ... .