title: Digital Signature Certificate set issuer name
body: <p>By using java x509certificate i am generating a p12 certificate. After digitally signing a certificate its displaying Subject and Issuer as same. Can any one please help me in setting Subject as my name and issuer as company name.</p>
  
  <p>P12 file its self has issuer and subject as same.</p>
  
  <h2><a href="https://i.stack.imgur.com/rm01U.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rm01U.png" alt="Image 1"></a></h2>
  
  <p><a href="https://i.stack.imgur.com/WYuyf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/WYuyf.png" alt="Image 2"></a></p>
  
  <p>Below is my code for generating the certificate</p>
  
  <pre><code>package com.dynamic;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.security.InvalidKeyException;
  import java.security.Key;
  import java.security.KeyStore;
  import java.security.NoSuchAlgorithmException;
  import java.security.NoSuchProviderException;
  import java.security.Principal;
  import java.security.SignatureException;
  import java.security.cert.CertificateException;
  import java.security.cert.X509Certificate;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  import sun.security.x509.CertAndKeyGen;
  import sun.security.x509.X500Name;
  public class Dynamic { 
      public static void main(String[] args) {
          try{           
              Dynamic di=new Dynamic();
              di.createPFXFile();
          } catch (Exception ex){
              ex.printStackTrace();
          }
      }    
  public boolean createPFXFile() throws IOException, NoSuchAlgorithmException, CertificateException, InvalidKeyException, SignatureException, NoSuchProviderException
      {
          boolean flag=false;
          try {
              X509Certificate cert=null;
              KeyStore keyStore = KeyStore.getInstance("PKCS12");
              keyStore.load(null, null);
              CertAndKeyGen gen = new CertAndKeyGen("RSA","SHA1WithRSA");
              gen.generate(2048);
              Key key=gen.getPrivateKey();
              System.out.println("private key" + key);             
              cert=gen.getSelfCertificate(new X500Name("name","ccc","ddd","eee"), (long)365*24*3600);           
              X509Certificate[] chain = new X509Certificate[1];
              chain[0]=cert;
              System.out.println("chain[0] :"+chain[0]);
              keyStore.setKeyEntry("private", key, "password".toCharArray(), chain); 
              keyStore.store(new FileOutputStream("D:/"+uid+".p12"), "password".toCharArray()); 
              return flag=true;
          } catch (Exception ex) {
              Logger.getLogger(Dynamic.class.getName()).log(Level.SEVERE, null, ex);
          }
          return flag;
      }  
  }
  </code></pre>