Generating Digital Certificates

Hello folks! I need some help and I think that I'm in right place... Please help me if you have time...

I want to create an application to create and export digital certificates. I know how to use keytool command and what I need is basically that command but in a form of GUI...

Here's my code:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.io.*;

import java.util.*;

publicclass Pisca

{

public Pisca(){

final JFrame f =new JFrame("Certificate Authority");

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setIconImage(new ImageIcon("BlueLace.PNG").getImage());

f.setResizable(false);

JFrame.setDefaultLookAndFeelDecorated(true);

f.setSize(400, 300);

Dimension d = Toolkit.getDefaultToolkit().getScreenSize();

f.setLocation((d.width - f.getSize().width) / 2, (d.height - f.getSize().height) / 2);

Container cp = f.getContentPane();

cp.setLayout(new GridLayout(7,2));

JTextField tfName =new JTextField();

JTextField tfOrganizationalUnit =new JTextField();

JTextField tfOrganization =new JTextField();

JTextField tfCity =new JTextField();

JTextField tfState =new JTextField();

JTextField tfCountryCode =new JTextField();

tfCountryCode.setText("SR");

JLabel lblName =new JLabel("First and Last Name:");

JLabel lblOrganizationalUnit =new JLabel("Organizational Unit:");

JLabel lblOrganization =new JLabel("Organization:");

JLabel lblCity =new JLabel("City:");

JLabel lblState =new JLabel("State:");

JLabel lblCountryCode =new JLabel("Two-letter Contry Code:");

final JButton b =new JButton("Generisi");

b.setToolTipText("Generisite digitalni sertifikat.");

cp.add(lblName);

cp.add(tfName);

cp.add(lblOrganizationalUnit);

cp.add(tfOrganizationalUnit);

cp.add(lblOrganization);

cp.add(tfOrganization);

cp.add(lblCity);

cp.add(tfCity);

cp.add(lblState);

cp.add(tfState);

cp.add(lblCountryCode);

cp.add(tfCountryCode);

cp.add(new JLabel(""));

cp.add(b);

f.addWindowListener(new WindowAdapter(){

publicvoid windowClosing(WindowEvent e){

JOptionPane.showMessageDialog(f,"Hvala na koriscenju programa.","Milos Piscevic", JOptionPane.INFORMATION_MESSAGE);

}

});

b.addActionListener(new ActionListener(){

publicvoid actionPerformed(ActionEvent e){

try

{

Runtime runtime = Runtime.getRuntime();

String temp ="CN=Name, OU=Some, O=Etc, L=Monaco, S=Nevermind, C=LT";

Process proc = runtime.exec("keytool -genkeypair -alias some_alias -keyalg RSA -sigalg SHA1withRSA -keysize 1024 -dname " + temp +" -keypass sifra -storepass somePass -keystore some_alias.keystore" -keystoretype JKS);

Process proc2 = runtime.exec("keytool -exportcert -alias....");

}

catch (Exception ex)

{

JOptionPane.showMessageDialog(f,"Greska.","Milos Piscevic", JOptionPane.ERROR_MESSAGE);

}

}

});

f.setVisible(true);

}

publicstaticvoid main(String[] args)

{

Pisca p =new Pisca();

}

}

So what I want to do is, when user clicks on button b, to export a digital certificate in .cer format.

I've used keytool -genkeypair -alias ... -keyalg ... -sigalg ... -keystore ... command and that is ok. After I've created a keystore, then I used keytool -exportcert -alias ... -file name.cer ... command and I got my digital certificate.

In this application I want practically to simulate keytool but on a GUI basis, so when user clicks on button, I want to execute those two commands:

I tried something like this (I tried to put this code on actionPerformed event):

try

{

Runtime runtime = Runtime.getRuntime();

String temp ="CN=Name, OU=Some, O=Etc, L=Monaco, S=Nevermind, C=LT";

Process proc = runtime.exec("keytool -genkeypair -alias some_alias -keyalg RSA -sigalg SHA1withRSA -keysize 1024 -dname " + temp +" -keypass sifra -storepass somePass -keystore some_alias.keystore" -keystoretype JKS);

Process proc2 = runtime.exec("keytool -exportcert -alias....");

}

catch (IOException ex)

{

ex.printStackTrace();

}

But I got nothing!!!

How to solve this? If I can do that without using keytool, runtime, process and other classes, but something else, then what to use?

Message was edited by:

djalfirevic126

[7232 byte] By [djalfirevic126a] at [2007-11-27 6:19:26]
# 1
As the javadoc says, Runtime.exec(String command) executes the specified string command in a _separate_ process, which means the process runs parallel with your Java program. Call the waitFor() method of the returned Process object so that the process is fully executed.
wangwja at 2007-7-12 17:34:03 > top of Java-index,Security,Cryptography...
# 2

wangwj, thanks for replying but I need a little help:

Simpler example:

public class Pisca2

{

public static void main(String[] args)

{

System.out.println("Hello World!");

try

{

Runtime runtime = Runtime.getRuntime();

String temp = "CN=Djuro Alfirevic, OU=Software Developers, O=ProSoftware, L=Belgrade, S=Serbia, C=SR";

Process proc = runtime.exec("keytool -genkeypair -alias pisca -keyalg RSA -sigalg SHA1withRSA -keysize 1024 -dname " + temp + " -keypass piscaa -storepass piscaa -keystore pisca.keystore");

proc.waitFor();

}

catch (Exception ex)

{

ex.printStackTrace();

}

}

}

Did you meant proc.waitFor() call?

Still it's not working...

djalfirevic126a at 2007-7-12 17:34:03 > top of Java-index,Security,Cryptography...
# 3

The dname in your command line has too many spaces inside, it's broken into more strings and keytool thinks that's not a correct format.

You'll have to use the Runtime.exec(String[]) version here. I have a trick:

String ss ="keytool -genkeypair -alias pisca -keyalg RSA -sigalg SHA1withRSA -keysize 1024 -dname PLACEHOLDER -keypass piscaa -storepass piscaa -keystore pisca.keystore";

String[] array = ss.split(" ");

array[11] = "CN=Djuro Alfirevic, OU=Software Developers, O=ProSoftware, L=Belgrade, S=Serbia, C=SR";

runtime.exec(array).waitFor();

wangwja at 2007-7-12 17:34:03 > top of Java-index,Security,Cryptography...
# 4

> The dname in your command line has too many spaces

> inside, it's broken into more strings and keytool

> thinks that's not a correct format.

>

> You'll have to use the Runtime.exec(String[]) version

> here. I have a trick:

>

> String ss ="keytool -genkeypair -alias

> pisca -keyalg RSA -sigalg SHA1withRSA -keysize 1024

> -dname PLACEHOLDER -keypass piscaa -storepass piscaa

> -keystore pisca.keystore";

>String[] array = ss.split(" ");

> array[11] = "CN=Djuro Alfirevic, OU=Software

> Developers, O=ProSoftware, L=Belgrade, S=Serbia,

> C=SR";

>runtime.exec(array).waitFor();

>

That's it! Thanks man!

Here is the whole source now:

/**

interfejsom u okviru Jave

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.io.*;

import java.util.*;

public class Pisca

{

public Pisca(){

final JFrame f = new JFrame("Certificate Authority");

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setIconImage(new ImageIcon("BlueLace.PNG").getImage());

f.setResizable(false);

JFrame.setDefaultLookAndFeelDecorated(true);

f.setSize(400, 300);

Dimension d = Toolkit.getDefaultToolkit().getScreenSize();

f.setLocation((d.width - f.getSize().width) / 2, (d.height - f.getSize().height) / 2);

Container cp = f.getContentPane();

cp.setLayout(new GridLayout(7,2));

final JTextField tfName = new JTextField();

final JTextField tfOrganizationalUnit = new JTextField();

final JTextField tfOrganization = new JTextField();

final JTextField tfCity = new JTextField();

final JTextField tfState = new JTextField();

final JTextField tfCountryCode = new JTextField();

tfCountryCode.setText("SR");

JLabel lblName = new JLabel("First and Last Name:");

JLabel lblOrganizationalUnit = new JLabel("Organizational Unit:");

JLabel lblOrganization = new JLabel("Organization:");

JLabel lblCity = new JLabel("City:");

JLabel lblState = new JLabel("State:");

JLabel lblCountryCode = new JLabel("Two-letter Contry Code:");

final JButton b = new JButton("Generisi");

b.setToolTipText("Generisite digitalni sertifikat.");

cp.add(lblName);

cp.add(tfName);

cp.add(lblOrganizationalUnit);

cp.add(tfOrganizationalUnit);

cp.add(lblOrganization);

cp.add(tfOrganization);

cp.add(lblCity);

cp.add(tfCity);

cp.add(lblState);

cp.add(tfState);

cp.add(lblCountryCode);

cp.add(tfCountryCode);

cp.add(new JLabel(""));

cp.add(b);

f.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e){

JOptionPane.showMessageDialog(f, "Hvala na koriscenju programa.", "Milos Piscevic", JOptionPane.INFORMATION_MESSAGE);

}

});

b.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e){

try

{

Runtime runtime = Runtime.getRuntime();

String ss ="keytool -genkeypair -alias pisca -keyalg RSA -sigalg SHA1withRSA -keysize 1024 -dname PLACEHOLDER -keypass piscaa -storepass piscaa -keystore pisca.keystore";

String[] array = ss.split(" ");

array[11] = "CN="+tfName.getText()+", OU="+tfOrganizationalUnit.getText()+", O="+tfOrganization.getText()+", L="+tfCity.getText()+", S="+tfState.getText()+", C="+tfCountryCode.getText();

runtime.exec(array).waitFor();

String ss1 ="keytool -exportcert -alias pisca -file pisca.cer -keypass piscaa -storepass piscaa -keystore pisca.keystore";

String[] array1 = ss1.split(" ");

runtime.exec(array1).waitFor();

}

catch (Exception ex)

{

JOptionPane.showMessageDialog(f, "Greska.", "Milos Piscevic", JOptionPane.ERROR_MESSAGE);

}

}

});

f.setVisible(true);

}

public static void main(String[] args)

{

Pisca p = new Pisca();

}

}

You'll maybe need it sometimes... :)

Thanks again wangwj. Grazie.

Message was edited by:

djalfirevic126

djalfirevic126a at 2007-7-12 17:34:03 > top of Java-index,Security,Cryptography...