java.security.ProviderException: Initialization failed

267 Views Asked by At

I am writing a java program that needs to read a USB Token Flash Private Key to do something, but I got stuck in the first phase of this program and it throws Error like this:

Exception in thread "main" java.security.ProviderException: Initialization failed
    at sun.security.pkcs11.SunPKCS11.<init>(SunPKCS11.java:377)
    at sun.security.pkcs11.SunPKCS11.<init>(SunPKCS11.java:108)
Caused by: sun.security.pkcs11.wrapper.PKCS11Exception: CKR_TOKEN_NOT_PRESENT
    at sun.security.pkcs11.wrapper.PKCS11.C_GetTokenInfo(Native Method)
    at sun.security.pkcs11.Token.<init>(Token.java:135)
    at sun.security.pkcs11.SunPKCS11.initToken(SunPKCS11.java:969)
    at sun.security.pkcs11.SunPKCS11.<init>(SunPKCS11.java:368)
    ... 2 more

Here's the peice of code that represents the error above:


    String pkcs11Config = "name=eToken\nlibrary=C:\\Windows\\System32\\ShuttleCsp11_3003.dll";
    System.out.println("step 1");
    java.io.ByteArrayInputStream pkcs11ConfigStream = new java.io.ByteArrayInputStream(pkcs11Config.getBytes());
    Provider providerPKCS11 = new sun.security.pkcs11.SunPKCS11(pkcs11ConfigStream);
    java.security.Security.addProvider(providerPKCS11);
    System.out.println("step 2");

I tried to reinstall the program which is inside this USB flash and I could successfully login and export public key and certificate. I even restarted the pc, but none of them worked and I still got those errors. Do you think my USB Token is corrupted or damaged? Cause if it is damaged it shouldn't login successfully...

I expect to pass this line of code:

Provider providerPKCS11 = new sun.security.pkcs11.SunPKCS11(pkcs11ConfigStream);

Can someone please help me with this code?

0

There are 0 best solutions below