By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,998 Members | 2,837 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,998 IT Pros & Developers. It's quick & easy.

How to generate a "CertPath"?

P: n/a
When the generateCertPath line below is executed, I receive the
following message:

java.security.cert.CertificateException: unsupported encoding

This is a CA certificate from Verisign. What encoding should
I use?

-THanks

try {

// open an input stream to the file
FileInputStream fis = new FileInputStream(filename);

// instantiate a CertificateFactory for X.509
CertificateFactory cf = CertificateFactory.getInstance("X.509");

X509Certificate cert = (X509Certificate)cf.generateCertificate(fis);

//CertPath cp = cf.generateCertPath(fis, cert.getEncoded().toString());
CertPath cp = cf.generateCertPath(fis, "PKCS7");
}
catch (Exception e) {
e.printStackTrace();
}
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
somebody <so**@body.com> wrote in message news:<pa***************************@body.com>...
When the generateCertPath line below is executed, I receive the
following message:

java.security.cert.CertificateException: unsupported encoding

This is a CA certificate from Verisign. What encoding should
I use?

-THanks

try {

// open an input stream to the file
FileInputStream fis = new FileInputStream(filename);

// instantiate a CertificateFactory for X.509
CertificateFactory cf = CertificateFactory.getInstance("X.509");

X509Certificate cert = (X509Certificate)cf.generateCertificate(fis);

//CertPath cp = cf.generateCertPath(fis, cert.getEncoded().toString());
CertPath cp = cf.generateCertPath(fis, "PKCS7");
}
catch (Exception e) {
e.printStackTrace();
}


Hello,

I encountered a message in one of Sun's Java forums that sounds
similar to what you are experiencing. The address of that message (and
the replies) is http://forum.java.sun.com/thread.jsp...message=666032.
For your convenience, I've restated the message/replies below:

* * *

Hi,

I am trying to generate a Certificate object using CertificateFactory,
and the message "java.security.cert.CertificateException: Unsupported
encoding" happenes . The certificate type I am using is X.509. The
encoded certificate was got from the browser (begin from "-----BEGIN
CERTIFICATE-----" and end with "-----END CERTIFICATE-----"). Here is
the segment of my code. Anyone knows what the problem is? Thanks in
advance.

/** Create an instance of the certificate factory */
java.security.cert.CertificateFactory cf =
java.security.cert.CertificateFactory.getInstance( "X.509");

/** Convert base 64 encoded certificate to a byte stream */
ByteArrayInputStream bais =
new ByteArrayInputStream(encodedCert.getBytes());

/** Generate certificate */
cf.generateCertificate(bais); // exception happend here

.. . .

Hi !

If it's framed like that I would guess you need to uuDecode it first,
before you can use it.

Have a look at http://java.sun.com/security/signExample/

That might help you.

.. . .

Hi,

Another possibility is that the cert is BASE64 encoded, you can base64
decode it using

String pemcert = "..."; // with the lines containing --- stripped off
byte[] certbytes = new sun.misc.BASE64Decoder().decodeBuffer(pemcert);

then create the CertificateFactory as before.

regards

Kevin

* * *

I hope this answer is helpful to you, and have a good day.

Jeff
www.javajeff.net
Jul 17 '05 #2

P: n/a
somebody <so**@body.com> wrote in message news:<pa***************************@body.com>...
When the generateCertPath line below is executed, I receive the
following message:

java.security.cert.CertificateException: unsupported encoding

This is a CA certificate from Verisign. What encoding should
I use?

-THanks

try {

// open an input stream to the file
FileInputStream fis = new FileInputStream(filename);

// instantiate a CertificateFactory for X.509
CertificateFactory cf = CertificateFactory.getInstance("X.509");

X509Certificate cert = (X509Certificate)cf.generateCertificate(fis);

//CertPath cp = cf.generateCertPath(fis, cert.getEncoded().toString());
CertPath cp = cf.generateCertPath(fis, "PKCS7");
}
catch (Exception e) {
e.printStackTrace();
}


Hello,

I encountered a message in one of Sun's Java forums that sounds
similar to what you are experiencing. The address of that message (and
the replies) is http://forum.java.sun.com/thread.jsp...message=666032.
For your convenience, I've restated the message/replies below:

* * *

Hi,

I am trying to generate a Certificate object using CertificateFactory,
and the message "java.security.cert.CertificateException: Unsupported
encoding" happenes . The certificate type I am using is X.509. The
encoded certificate was got from the browser (begin from "-----BEGIN
CERTIFICATE-----" and end with "-----END CERTIFICATE-----"). Here is
the segment of my code. Anyone knows what the problem is? Thanks in
advance.

/** Create an instance of the certificate factory */
java.security.cert.CertificateFactory cf =
java.security.cert.CertificateFactory.getInstance( "X.509");

/** Convert base 64 encoded certificate to a byte stream */
ByteArrayInputStream bais =
new ByteArrayInputStream(encodedCert.getBytes());

/** Generate certificate */
cf.generateCertificate(bais); // exception happend here

.. . .

Hi !

If it's framed like that I would guess you need to uuDecode it first,
before you can use it.

Have a look at http://java.sun.com/security/signExample/

That might help you.

.. . .

Hi,

Another possibility is that the cert is BASE64 encoded, you can base64
decode it using

String pemcert = "..."; // with the lines containing --- stripped off
byte[] certbytes = new sun.misc.BASE64Decoder().decodeBuffer(pemcert);

then create the CertificateFactory as before.

regards

Kevin

* * *

I hope this answer is helpful to you, and have a good day.

Jeff
www.javajeff.net
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.