CertificateParsingException: invalid DER-encoded certificate data
2014-04-09 11:16
357 查看
要做一个证书登录的实验。通过后台生成的证书,在web页面进行登录。在后台验证读取证书文件(.cer)时报错,"CertificateParsingException: invalid DER-encoded certificate data!"
证书生成后的导出证书文件的代码如下:
public static void exportCert(X509Certificate cert, String name, String path)
throws CertificateEncodingException, IOException {
BASE64Encoder base64 = new BASE64Encoder();
File dir = new File(path);
if (!dir.exists()) {
dir.mkdir();
}
File certFile = new File(path + File.separator + name + ".cer");
FileOutputStream fos = new FileOutputStream(certFile);
base64.encodeBuffer(cert.getEncoded(), fos);
fos.close();
}
读取证书文件(.cer)的代码的代码如下:
public static Certificate importCer(File certFile) {
Certificate cert = null;
try {
CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
FileInputStream fis = new FileInputStream(certFile);
cert = certFactory.generateCertificate(fis);
fis.close();
} catch (Exception exception) {
exception.printStackTrace();
}
return cert;
}
从打印出来的exception来看,应该是证书文件的格式不对。然后,我就检查了一遍代码(导出的代码是网上download的。。),发现在导出时进行了base64的加密处理,
base64.encodeBuffer(cert.getEncoded(), fos);
我估计是这出来问题。然后就把导出证书文件的代码改写了一下,
public static void exportCert(X509Certificate cert, String name, String path)
throws CertificateEncodingException, IOException {
File dir = new File(path);
if (!dir.exists()) {
dir.mkdir();
}
File certFile = new File(path + File.separator + name + ".cer");
FileOutputStream fos = new FileOutputStream(certFile);
fos.write(cert.getEncoded());
fos.close();
}
之后,就不报错了。
证书生成后的导出证书文件的代码如下:
public static void exportCert(X509Certificate cert, String name, String path)
throws CertificateEncodingException, IOException {
BASE64Encoder base64 = new BASE64Encoder();
File dir = new File(path);
if (!dir.exists()) {
dir.mkdir();
}
File certFile = new File(path + File.separator + name + ".cer");
FileOutputStream fos = new FileOutputStream(certFile);
base64.encodeBuffer(cert.getEncoded(), fos);
fos.close();
}
读取证书文件(.cer)的代码的代码如下:
public static Certificate importCer(File certFile) {
Certificate cert = null;
try {
CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
FileInputStream fis = new FileInputStream(certFile);
cert = certFactory.generateCertificate(fis);
fis.close();
} catch (Exception exception) {
exception.printStackTrace();
}
return cert;
}
从打印出来的exception来看,应该是证书文件的格式不对。然后,我就检查了一遍代码(导出的代码是网上download的。。),发现在导出时进行了base64的加密处理,
base64.encodeBuffer(cert.getEncoded(), fos);
我估计是这出来问题。然后就把导出证书文件的代码改写了一下,
public static void exportCert(X509Certificate cert, String name, String path)
throws CertificateEncodingException, IOException {
File dir = new File(path);
if (!dir.exists()) {
dir.mkdir();
}
File certFile = new File(path + File.separator + name + ".cer");
FileOutputStream fos = new FileOutputStream(certFile);
fos.write(cert.getEncoded());
fos.close();
}
之后,就不报错了。
相关文章推荐
- org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read
- InvalidDataAccessApiUsageException 和 Write operations are not allowed in read-only mode 解决方法
- java.util.zip.DataFormatException: invalid distance too far back
- org.springframework.dao.InvalidDataAccessApiUsageException
- org.springframework.dao.InvalidDataAccessApiUsageException: ERR value is not an integer or out of ra
- org.springframework.dao.InvalidDataAccessApiUsageException
- org.springframework.dao.InvalidDataAccessApiUsageException问题解决与分析
- org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query;
- org.springframework.dao.InvalidDataAccessApiUsageException: (开发错误记录)
- InvalidDataAccessApiUsageException异常的处理办法
- org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query; nested exc
- org.springframework.dao.InvalidDataAccessResourceUsageException: could not get next sequence value
- 解决InvalidDataAccessApiUsageException: Executing an update/delete query
- hibernate框架学习错误集锦-org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.MANUAL)
- OpenSessionInViewFilter与org.springframework.dao.InvalidDataAccessApiUsageException
- Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: Multiple representations of the same entity解决方法
- C#:操作 Excel 异常:Old format or invalid type library. (Exception from HRESULT: 0x80028018 (TYPE_E_INVDATAREAD))
- InvalidDataAccessApiUsageException异常的三个处理方
- An exception occurred: org.springframework.dao.InvalidDataAccessApiUsageException
- Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageExce