Search code examples
javasecuritybouncycastlejce

SHA2RSA encryption with BouncyCastle


I have been trying to encrypt some data using BouncyCastle's JCE provider. I'm trying "SHA256withRSA" and I'm getting a "noSuchAlgorithmException". Am I doing something wrong? Can someone help? Thanks

Specifically I'm trying

Signature.getInstance("SHA256withRSA", new BouncyCastleProvider());

As mentioned here - http://www.bouncycastle.org/wiki/display/JA1/X.509+Public+Key+Certificate+and+Certification+Request+Generation


Solution

  • Try this instead:

    Signature.getInstance("SHA256withRSA", "BC");
    

    In most of the examples I have seen, the second param has a string being passed in vs. the provider itself.

    The getInstance method seems to support having a provider passed in, but perhaps just doing

     new BouncyCastleProvier()
    

    does not construct it properly, resulting in missing algorithms. I suspect by passing the "BC" string instead, it will use the already constructed provider in JCA/JCE.

    Ref: http://docs.oracle.com/javase/6/docs/technotes/guides/security/crypto/CryptoSpec.html#ProviderImplReq