function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
@GM@GM 

Getting error : java.security.InvalidKeyException: No installed provider supports this key: (null)

Hi All,

I have created conneted app in one of our SFDC sandbox and tring to get the token but ended up with below error.
Please suggest.

Follewd the below link :
https://help.salesforce.com/HTViewHelpDoc?id=remoteaccess_oauth_jwt_flow.htm#create_token


Error : java.security.InvalidKeyException: No installed provider supports this key: (null)
    at java.security.Signature$Delegate.chooseProvider(Unknown Source)
    at java.security.Signature$Delegate.engineInitSign(Unknown Source)
    at java.security.Signature.initSign(Unknown Source)
    at JWTExample.main(JWTExample.java:45)


JAVA Class  code :
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.BaseNCodec;
import java.io.*;
import java.security.*;
import java.text.MessageFormat;  

public class JWTExample {

  public static void main(String[] args) {

    String header = "{\"alg\":\"RS256\"}";
    String claimTemplate = "'{'\"iss\": \"{0}\", \"sub\": \"{1}\", \"aud\": \"{2}\", \"exp\": \"{3}\"'}'";

    try {
      StringBuffer token = new StringBuffer();

      //Encode the JWT Header and add it to our string to sign
      token.append(Base64.encodeBase64URLSafeString(header.getBytes("UTF-8")));

      //Separate with a period
      token.append(".");

      //Create the JWT Claims Object
      String[] claimArray = new String[4];
      claimArray[0] = "client_id";
      claimArray[1] = "username";
      claimArray[2] = "https://test.salesforce.com";
      claimArray[3] = Long.toString( ( System.currentTimeMillis()/1000 ) + 300);
      MessageFormat claims;
      claims = new MessageFormat(claimTemplate);
      String payload = claims.format(claimArray);

      //Add the encoded claims object
      token.append(Base64.encodeBase64URLSafeString(payload.getBytes("UTF-8")));

      //Load the private key from a keystore
      KeyStore keystore = KeyStore.getInstance("JKS");
      keystore.load(new FileInputStream("C:/Users/gm/Downloads/Backup/00DQ000000GKQwv.jks"), "password1".toCharArray());
      PrivateKey privateKey = (PrivateKey) keystore.getKey("00DQ000000GKQwv.jks", "password1".toCharArray());

      //Sign the JWT Header + "." + JWT Claims Object
      //Signature signature = Signature.getInstance("SHA256withRSA");
      Signature signature = Signature.getInstance("SHA256withRSA");
      
      signature.initSign(privateKey);
      signature.update(token.toString().getBytes("UTF-8"));
      String signedPayload = Base64.encodeBase64URLSafeString(signature.sign());

      //Separate with a period
      token.append(".");

      //Add the encoded signature
      token.append(signedPayload);

      System.out.println(token.toString());

    } catch (Exception e) {
        e.printStackTrace();
    }
  }
}


Thanks,
GM
Bechir OueslatiBechir Oueslati

Hi,

You should specify your alias instead of your keystore file name (00DQ000000GKQwv.jks) when you get the key from the keystore

PrivateKey privateKey = (PrivateKey) keystore.getKey("YourAlias", "password1".toCharArray());
Hope that helps