How to fix or avoid "RSA engine faulty decryption/signing detected" error when signing in java

363 Views Asked by At

I am trying to sign a string using basic RSA code with SpongyCastle on android:

Security.insertProviderAt(new org.spongycastle.jce.provider.BouncyCastleProvider(), 1);
...


    Signature sign = Signature.getInstance("SHA256withRSA");
    RSAPrivateKey privateKey = parsePrivateKey( pk_pem_string );
    sign.initSign(privateKey, new SecureRandom());
    sign.update(content.getBytes("UTF-8"));
    byte[] signature = sign.sign();

But it fails with this error:

E/AndroidRuntime( 3392): Caused by: java.security.SignatureException: java.lang.IllegalStateException: RSA engine faulty decryption/signing detected
E/AndroidRuntime( 3392):    at org.spongycastle.jcajce.provider.asymmetric.rsa.DigestSignatureSpi.engineSign(DigestSignatureSpi.java:142)
E/AndroidRuntime( 3392):    at java.security.Signature$Delegate.engineSign(Signature.java:1418)
E/AndroidRuntime( 3392):    at java.security.Signature.sign(Signature.java:739)

I have found that this error is generated to prevent RSA-CRT Attacks, but there is no more info on how to fix or prevent the problem. I Need to Sign with SHA256withRSA because I need to interface with an external API that expects the content signed with that algorithm.

If I use the default Android Security Provider, the sign fails with:

Caused by: java.lang.RuntimeException: error:04000044:RSA routines:OPENSSL_internal:internal error
0

There are 0 best solutions below