Register / Login  |  Desktop view  |  Jump to bottom of page

General Issues » Exporting keys to a file

Author: Francky, Visitor
04/12/2007 16:14:42
Hi guys,

I use the evaluation version and I grant you for the great job. I can access the MS Keystore but I haven't found how to export the public keys to a file, typically a cer. I succeedeed in loading the KeyStore and i get Certificate objects by their alias but what should I do then to export public keys into files ?

Greetings.

Author: tommy, Visitor
05/12/2007 00:15:00
Hi Francky,

That kind of operation is already supported by SUN's Java class X509Certificate i.e. it's not a JCAPI specific operation.

Here's an example of how to export the binary (ASN.1/DER) representation of an X.509 certificate:

import java.security.*;
import java.security.cert.*;
import java.io.*;
import java.util.*;

import com.pheox.jcapi.*;

public class ExportCert
{
static public void main(String[] args)
{
try {
export();
} catch(Throwable t) {
t.printStackTrace();
}
}

static private void export()
throws Exception
{
System.out.println("Export certificate to file cert.cer");

Security.addProvider(new JCAPIProvider());
KeyStore ks = KeyStore.getInstance("msks", "JCAPI");
ks.load(null, null);

//Get first certificate found.
Enumeration aliases = ks.aliases();
String alias = (String)aliases.nextElement();
if(alias == null)
throw new Exception("No certificate found.");
X509Certificate cert = (X509Certificate)ks.getCertificate(alias);

//Export/write certificate to file (X.509 ASN.1/DER).
FileOutputStream fos = new FileOutputStream("cert.cer");
try {
fos.write(cert.getEncoded());
} finally {
fos.close();
}

System.out.println("Done!");
}
}


Regards,
Tommy




Register / Login  |  Desktop view  |  Jump to top of page