title: How to generate a Certificate Chain to be sent as an argument to KeyStore.PrivateKeyEntry?
body: <p>I am a beginner in Java Programming. My code encrypts data extracted from a text file and stores it in another file using RSA algorithm. I want to make my Private Key password protected by using the KeyStore class (http://download.oracle.com/javase/1,5.0/docs/api/java/security/KeyStore.html) and it's nested class - KeyStore.PrivateKeyEntry (http://download.oracle.com/javase/1,5.0/docs/api/java/security/KeyStore.PrivateKeyEntry.html). </p>
  
  <p>The constructor of KeyStore.PrivateKeyEntry requires a Certificate[] array and I am not sure about how to generate this Certificate[] array.</p>
  
  <p>I am attaching my code so far along with the question:</p>
  
  <p>This is the Encryption code.</p>
  
  <pre><code>public class Fileencrypt {
  
       public static void main(String args[])  throws IOException, InvalidKeyException, java.security.InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, KeyStoreException, CertificateException, CertificateEncodingException, IllegalStateException, NoSuchProviderException, SignatureException, UnrecoverableKeyException{   
       try{ 
  
           byte[] plainData;
           byte[] encryptedData = null;
  
           KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
           kpg.initialize(2048);
           KeyPair kp = kpg.genKeyPair();
           PublicKey publicKey = kp.getPublic();
           PrivateKey privateKey = kp.getPrivate();
  
  
  
           Cipher cipher = Cipher.getInstance("RSA");
           cipher.init(Cipher.ENCRYPT_MODE, publicKey);
  
           try { 
           X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(publicKey.getEncoded());
          FileOutputStream fos = new FileOutputStream("C:\\Output\\Publickey.txt");
          fos.write(x509EncodedKeySpec.getEncoded());
          fos.close();
          // Store Private Key.
          PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(privateKey.getEncoded());
          fos = new FileOutputStream("C:\\Output\\Privatekey.txt");
          fos.write(pkcs8EncodedKeySpec.getEncoded());
          fos.close();
          } catch (IOException e) {
              System.out.println(e.getMessage());
          }
  
  
  
  
  
           File f = new File("C:\\Output\\text.txt");
           FileInputStream in = new FileInputStream(f);
           plainData = new byte[(int)f.length()];
           in.read(plainData);
  
           try {
              encryptedData = cipher.doFinal(plainData);
          } catch (IllegalBlockSizeException e) {
  
              e.printStackTrace();
          } catch (BadPaddingException e) {
  
              e.printStackTrace();
          }
  
          System.out.println(encryptedData); 
          FileOutputStream target = new FileOutputStream(new File("C:\\Output\\encrypted.txt"));
           target.write(encryptedData);
           target.close();   
       }   
       catch(IOException e){e.printStackTrace();}   
       catch(InvalidKeyException ei){ei.printStackTrace();
       }   
       }   
     }
  </code></pre>
  
  <p>This is the Decryption code.</p>
  
  <pre><code>public class Filedecrypt {
  
  public static void main(String args[]) throws IOException,
          InvalidKeyException, java.security.InvalidKeyException,
          NoSuchAlgorithmException, NoSuchPaddingException,
          BadPaddingException {
      try {
  
          byte[] plainData = null;
          byte[] encryptedData;
          File f1 = new File("C:\\Output\\Privatekey.txt");
          FileInputStream in1 = new FileInputStream(f1);
          byte[] bytekey = new byte[(int) f1.length()];
          in1.read(bytekey);
          KeyFactory keyFac = KeyFactory.getInstance("RSA");
          PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(bytekey);
  
          PrivateKey key = keyFac.generatePrivate(keySpec);
          Cipher cipher = Cipher.getInstance("RSA");
          cipher.init(Cipher.DECRYPT_MODE, key);
  
          File f = new File("C:\\Output\\encrypted.txt");
          FileInputStream in = new FileInputStream(f);
          encryptedData = new byte[(int) f.length()];
          in.read(encryptedData);
  
          try {
              plainData = cipher.doFinal(encryptedData);
          } catch (IllegalBlockSizeException e) {
  
              e.printStackTrace();
          } catch (BadPaddingException e) {
  
              e.printStackTrace();
          }
  
          FileOutputStream target = new FileOutputStream(
                  new File(
                          "C:\\Output\\text1.txt"));
          target.write(plainData);
          target.close();
      } catch (IOException e) {
          e.printStackTrace();
      } catch (InvalidKeyException ei) {
          ei.printStackTrace();
      } catch (InvalidKeySpecException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  }
  }
  </code></pre>