Search code examples
javaweb-servicessoapcryptographyws-security

Java WS Security Crypto class not initializing


I am trying to create a small program in Java to create the soap security headers for an API we are working with. Everything seems to be working so far, except the Crypto class. If I create an object from that class, all of those methods are there but it throws a null exception whenever I try to call any of the methods.

Crypto is an interface that works with CryptoBase. I was wondering if someone might be able to tell me how to initialize this class so I can call methods on it. I have little experience working with interfaces, it seems easy but I do not think it works just by doing Crypto cr; and then call methods like cr.getPrivateKey(name,pass); which is what I am trying to do.

Please see reference below for source files of the crypto package: LINK


Solution

  • CryptoBase is an abstract class that offers no methods for producing a concrete instance. So you'll need to create an instance of a non-abstract subclass.

    Looking at the API docs, it seems you can choose CertificateStore or Merlin. Your code would then look something like:

    Crypto crypto = new Merlin(... constructor args here ...);
    

    Note that if you define your variable as Crypto rather than Merlin, then you can only use the methods defined in the Crypto interface. Whether that's a problem for you depends upon your use case.