Pheox - Forums
  [Search] Search   [Recent Topics] Recent Topics   [Hottest Topics] Hottest Topics   [Top Downloads] Top Downloads   [Groups] Back to home page 
[Register] Register /  [Login] Login 

Exporting keys to a file RSS feed
Forum Index » General Issues
Author Message
Francky

Visitor

Joined: Dec 3, 2007
Messages: 1
Offline
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.
tommy

Visitor

Joined: May 30, 2005
Messages: 148
Offline
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
 
Forum Index » General Issues
Go to:   
Mobile view
Powered by JForum 2.8.3 © 2023 JForum Team • Maintained by Andowson Chang and Ulf Dittmer