title: How to create dynamically a BKS keystore + certificate in Android
body: <p>i have a problem with creating dynamically (without using keytool) a BKS keystore+certificate in android.</p>
  
  <p>I already have created a BKS keystore with BouncyCastle in my desktop app: 
  SEE:</p>
  
  <pre><code>public class KeyStoreGenerator {
  
  public static void main(String[] args){
      generateKeyStore("1234567", "Burcu Cinarci", "TU Dortmund", "Informatik", "Dortmund", "NRW", "DE");
  }
  public static void generateKeyStore(String password, String cn, String o, String ou, String l, String st, String c) {
      try {
          Security.addProvider(new BouncyCastleProvider()); 
          final java.security.KeyPairGenerator rsaKeyPairGenerator = java.security.KeyPairGenerator.getInstance("RSA");
          rsaKeyPairGenerator.initialize(2048);
          final KeyPair rsaKeyPair = 
  rsaKeyPairGenerator.generateKeyPair();
  
          // Generate the key store de type JCEKS
          Provider[] ps = Security.getProviders();
          for (int i = 0; i &lt; ps.length; i++)
              System.out.println("" + ps[i].getName());
  
          final KeyStore ks = KeyStore.getInstance("BKS");
          ks.load(null);
  
          final RSAPublicKey rsaPublicKey = (RSAPublicKey) rsaKeyPair.getPublic();
  
          System.out.println("LOG: format "+rsaPublicKey.getFormat());
          char[] pw = password.toCharArray();
  
          final RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) rsaKeyPair.getPrivate();
          final java.security.cert.X509Certificate certificate = makeCertificate(rsaPrivateKey, rsaPublicKey, cn, o, ou, l, st, c);
          final java.security.cert.X509Certificate[] certificateChain = { certificate };
  
  certificateChain);
          ks.setKeyEntry("operator", rsaKeyPair.getPrivate(), pw, certificateChain);
  
          File keyStoreFile= new File("keyStore.ks");
          final FileOutputStream fos = new FileOutputStream(
                  keyStoreFile);
          ks.store(fos, pw);
          fos.close();
          System.out.println(keyStoreFile.getAbsolutePath());
  
          System.setProperty("javax.net.ssl.keyStore",
                  keyStoreFile.getAbsolutePath());
          System.setProperty("javax.net.ssl.keyStorePassword", "1234567");
      } catch (Exception e) {
          e.printStackTrace();
      }
  }
  
  public static X509Certificate makeCertificate(PrivateKey issuerPrivateKey,
          PublicKey subjectPublicKey, String cn, String o, String ou, String l, String st, String c) throws Exception {
  
      final org.bouncycastle.asn1.x509.X509Name issuerDN = new org.bouncycastle.asn1.x509.X509Name(
              "CN="+cn+", OU="+ou+", O="+o+", L="+l+", ST="+st+", C="+c);
  
      final org.bouncycastle.asn1.x509.X509Name subjectDN = new org.bouncycastle.asn1.x509.X509Name(
              "CN="+cn+", OU="+ou+", O="+o+", L="+l+", ST="+st+", C="+c);
      final int daysTillExpiry = 10 * 365;
  
      final Calendar expiry = Calendar.getInstance();
      expiry.add(Calendar.DAY_OF_YEAR, daysTillExpiry);
  
      final org.bouncycastle.x509.X509V3CertificateGenerator certificateGenerator = new org.bouncycastle.x509.X509V3CertificateGenerator();
  
      certificateGenerator.setSerialNumber(java.math.BigInteger
              .valueOf(System.currentTimeMillis()));
      certificateGenerator.setIssuerDN(issuerDN);
  
      certificateGenerator.setSubjectDN(subjectDN);
      certificateGenerator.setPublicKey(subjectPublicKey);
      certificateGenerator.setNotBefore(new Date());
      certificateGenerator.setNotAfter(expiry.getTime());
  
      certificateGenerator.setSignatureAlgorithm("MD5WithRSA");
  
      return certificateGenerator.generate(issuerPrivateKey);
  }
  
  }
  </code></pre>
  
  <p>But in my android sdk, i can`t add the bouncycastle-jar file, because it is already included in android. (bouncycastle as provider exists already)</p>
  
  <p>But why can't I find the "org.bouncycastle..." package?</p>
  
  <p>The following code in the methode makeCertificate, which generates the certificate dynamically, does not work because eclipse cant find the specific package:</p>
  
  <pre><code>final org.bouncycastle.asn1.x509.X509Name subjectDN = new    org.bouncycastle.asn1.x509.X509Name("CN="+cn+", OU="+ou+", O="+o+", L="+l+", 
  ST="+st+", C="+c);
  </code></pre>
  
  <p>I have tried to add additionally the bouncycastle.jar file, but it doesn`t work, because of redundance of bouncycastle.</p>
  
  <hr>
  
  <p>thank you for your post.. the problem is still, that android don`t know the classes
  X509V3CertificateGenerator and 
  X509Principal.</p>
  
  <p>Android don't know any classes of bouncycastle. I can change the class X509Principal to X500Principal, which exists in the package javax.security.auth.x500 but i can't replace any class for the CertificateGenerator</p>
  
  <p>thx</p>