title: Bouncy Castle Keystore (BKS) : java.io.IOException: Wrong version of key store
body: <p>I have to connect to a REST based webservice.</p>
  
  <p>(<a href="https://someurl.com/api/lookup/jobfunction/lang/EN">https://someurl.com/api/lookup/jobfunction/lang/EN</a>)</p>
  
  <p>In IE or chrome browser when I try to access this URL, I get a certificate that I have to trust and accept to continue
  After that I have to enter username and password and then I get JSON response.</p>
  
  <p>Same thing I have to do it programatically for an android app.</p>
  
  <ol>
  <li><p>Tried with custom EasySSLSocketFactory and EasyX509TrustManager , Didnt work.
  I got the following error : 
  java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.</p></li>
  <li><p>Used the BKS keystore, 
  please note that mykeystore.bks is an empty file before i executed the below commands</p>
  
  <pre><code>keytool -importcert -v -trustcacerts -file "test.crt" -alias IntermediateCA -keystore   "mykeystore.bks" -provider org.bouncycastle.jce.provider.BouncyCastleProvider -providerpath   "bcprov-jdk15on-148.jar" -storetype BKS -storepass abcd1234
  
  
  keytool -list -keystore "mykeystore.bks" -provider org.bouncycastle.jce.provider.BouncyCastleProvider  -providerpath "bcprov-jdk15on-148.jar" -storetype BKS -storepass abcd1234
  </code></pre></li>
  </ol>
  
  <p>MyHTTPClient.java looks like below :  </p>
  
  <pre><code>public class MyHttpClient extends DefaultHttpClient { 
  
  final Context context; 
  
  public MyHttpClient(Context context) { 
      this.context = context; 
  } 
  
  @Override
  protected ClientConnectionManager createClientConnectionManager() { 
      SchemeRegistry registry = new SchemeRegistry(); 
      registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); 
      // Register for port 443 our SSLSocketFactory with our keystore 
      // to the ConnectionManager 
      registry.register(new Scheme("https", newSslSocketFactory(), 443)); 
      return new SingleClientConnManager(getParams(), registry); 
  } 
  
  private SSLSocketFactory newSslSocketFactory() { 
      try { 
          // Get an instance of the Bouncy Castle KeyStore format 
          KeyStore trusted = KeyStore.getInstance("BKS"); 
          // Get the raw resource, which contains the keystore with 
          // your trusted certificates (root and any intermediate certs) 
          InputStream in = context.getResources().openRawResource(R.raw.mykeystore); 
          try { 
              // Initialize the keystore with the provided trusted certificates 
              // Also provide the password of the keystore 
              trusted.load(in, "abcd1234".toCharArray()); 
          } finally { 
              in.close(); 
          } 
          // Pass the keystore to the SSLSocketFactory. The factory is responsible 
          // for the verification of the server certificate. 
          SSLSocketFactory sf = new SSLSocketFactory(trusted); 
          // Hostname verification from certificate 
          // http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html#d4e506 
          sf.setHostnameVerifier(SSLSocketFactory.STRICT_HOSTNAME_VERIFIER); 
          return sf; 
      } catch (Exception e) { 
          throw new AssertionError(e); 
      } 
  } 
  </code></pre>
  
  <p>When I call the webservice , I am getting the below error :
  Caused by: java.lang.AssertionError: java.io.IOException: Wrong version of key store</p>
  
  <p>Please tell me what I have to do to connect to HTTPS based rest webservice which has username and passwd credentials.
  ......</p>