Certificate Signer : Certificate « Security « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » Security » CertificateScreenshots 
Certificate Signer


/**
 @version 1.00 1999-10-23
 @author Cay Horstmann
 */

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PushbackReader;
import java.security.KeyStore;
import java.security.Principal;
import java.security.PrivateKey;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.Arrays;

import sun.security.x509.CertificateIssuerName;
import sun.security.x509.X500Name;
import sun.security.x509.X509CertImpl;
import sun.security.x509.X509CertInfo;

public class CertificateSigner {
  public static void main(String[] args) {
    String ksname = null// the keystore name
    String alias = null// the private key alias
    String inname = null// the input file name
    String outname = null// the output file name
    for (int i = 0; i < args.length; i += 2) {
      if (args[i].equals("-keystore"))
        ksname = args[i + 1];
      else if (args[i].equals("-alias"))
        alias = args[i + 1];
      else if (args[i].equals("-infile"))
        inname = args[i + 1];
      else if (args[i].equals("-outfile"))
        outname = args[i + 1];
      else
        usage();
    }

    if (ksname == null || alias == null || inname == null
        || outname == null)
      usage();

    try {
      PushbackReader console = new PushbackReader(new InputStreamReader(
          System.in));

      KeyStore store = KeyStore.getInstance("JKS""SUN");
      InputStream in = new FileInputStream(ksname);
      char[] password = readPassword(console, "Keystore password");
      store.load(in, password);
      Arrays.fill(password, ' ');
      in.close();

      char[] keyPassword = readPassword(console, "Key password for "
          + alias);
      PrivateKey issuerPrivateKey = (PrivateKeystore.getKey(alias,
          keyPassword);
      Arrays.fill(keyPassword, ' ');

      if (issuerPrivateKey == null)
        error("No such private key");

      in = new FileInputStream(inname);

      CertificateFactory factory = CertificateFactory
          .getInstance("X.509");

      X509Certificate inCert = (X509Certificatefactory
          .generateCertificate(in);
      in.close();
      byte[] inCertBytes = inCert.getTBSCertificate();

      X509Certificate issuerCert = (X509Certificatestore
          .getCertificate(alias);
      Principal issuer = issuerCert.getSubjectDN();
      String issuerSigAlg = issuerCert.getSigAlgName();

      FileOutputStream out = new FileOutputStream(outname);

      X509CertInfo info = new X509CertInfo(inCertBytes);
      info.set(X509CertInfo.ISSUER, new CertificateIssuerName(
          (X500Nameissuer));

      X509CertImpl outCert = new X509CertImpl(info);
      outCert.sign(issuerPrivateKey, issuerSigAlg);
      outCert.derEncode(out);

      out.close();
    catch (Exception exception) {
      System.out.println(exception);
    }
  }

  public static char[] readPassword(PushbackReader in, String prompt)
      throws IOException {
    System.out.print(prompt + ": ");
    System.out.flush();
    final int MAX_PASSWORD_LENGTH = 100;
    int length = 0;
    char[] buffer = new char[MAX_PASSWORD_LENGTH];

    while (true) {
      int ch = in.read();
      if (ch == '\r' || ch == '\n' || ch == -1
          || length == MAX_PASSWORD_LENGTH) {
        if (ch == '\r'// handle DOS "\r\n" line ends
        {
          ch = in.read();
          if (ch != '\n' && ch != -1)
            in.unread(ch);
        }
        char[] password = new char[length];
        System.arraycopy(buffer, 0, password, 0, length);
        Arrays.fill(buffer, ' ');
        return password;
      else {
        buffer[length(charch;
        length++;
      }
    }
  }

  public static void error(String message) {
    System.out.println(message);
    System.exit(1);
  }

  public static void usage() {
    System.out.println("Usage: java CertificateSigner"
        " -keystore keyStore -alias issuerKeyAlias"
        " -infile inputFile -outfile outputFile");
    System.exit(1);
  }
}



           
       
Related examples in the same category
1. Signature Test
2. Security Manager TestSecurity Manager Test
3. Permission TestPermission Test
ww_w___.__ja___v___a___2__s__.___c___om_ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.