显示db的证书内容

时间:2014-06-30 14:23:39

标签: java mysql

我对之前的代码进行了一些更改并取得了一些进展,但仍然存在一些变量问题。

以下是我的更改内容:

<%@ page import="java.io.*%>
<%@ page import="java.sql.*%>
<%@ page import="javax.security.cert.X509Certificate"%>

String qrycert = "select convert((hex(cert)) using ascii) as cert from table where      record_id=1";   
ResultSet rs = sqlstatement.executeQuery(qrycert);

while (rs.next()) {
byte[] certs = rs.getBytes("cert");
String cert = X509Certificate.getInstance(certs).toString();  //doesn't like this line
 }


欢迎评论!

==================做了新的更改并得到了新的错误============     
       &lt;%
        声明sqlStatement = conn.createStatement();
        字符串qrycerts =“选择转换((hex(证书))使用ascii)作为来自
的证书      dbtable where record_id = 1“;
        ResultSet rs = sqlStatement.executeQuery(qrycerts);
         while(rs.next()){
         byte [] certEntryBytes = rs.getBytes(“certificate”);
         InputStream in = new ByteArrayInputStream(certEntryBytes);
        CertificateFactory certFactory = CertificateFactory.getInstance(“X509”);
         X509Certificate certs =(X509Certificate)certFactory.generateCertificate(in);
        }         %GT;      
     
    =====================错误=========================== < / p>

HTTP Status 500 - 

type Exception report 

message 

 description The server encountered an internal error () that prevented it from fulfilling 
 this request. 

 exception 

 org.apache.jasper.JasperException: An exception occurred processing JSP page test.jsp at line     40 

 40: X509Certificate certs = (X509Certificate)certFactory.generateCertificate(in); 

Stacktrace: 
 org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510 
 ) 
 org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:401) 
 org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313) 
 org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260) 
 javax.servlet.http.HttpServlet.service(HttpServlet.java:717) 

  root cause 

 javax.servlet.ServletException: java.security.cert.CertificateException: Could not parse 
 certificate: java.io.IOException: Empty input 
 org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:862) 

1 个答案:

答案 0 :(得分:0)

首先尝试使用

java.security.cert.X509Certificate

检查您的输入流,可能是您在以有效格式从表中获取数据时遇到问题...此示例从文件读取流并转换为字符串证书

import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
.....
    private static String loadCertificate(String name) throws Exception {
        CertificateFactory cf = CertificateFactory.getInstance("X509");
        InputStream in = new FileInputStream(new File( name));
        X509Certificate cert =(X509Certificate ) cf.generateCertificate(in);
        in.close();
        return cert.toString();
    }

并获得此输出

[
[
  Version: V3
  Subject: EMAILADDRESS=rccc@fdf.com, CN=bbbbbb, OU=aaaaaa, O=sdf, L=dsfds, ST=dfdsfds, C=ru
  Signature Algorithm: SHA1withRSA, OID = 1.2.840.113549.1.1.5

  Key:  Sun RSA public key, 512 bits
  modulus: 11090156790849751989871641780144834867548923295657430529721486102526989110037249630264155267535661372201070535894269904535819938776022933439845923773693937
  public exponent: 65537
  Validity: [From: Mon Jul 07 22:20:44 GST 2014,
               To: Wed Aug 06 22:20:44 GST 2014]
  Issuer: EMAILADDRESS=rccc@fdf.com, CN=bbbbbb, OU=aaaaaa, O=sdf, L=dsfds, ST=dfdsfds, C=ru
  SerialNumber: [    f1e018f6 09819775]

Certificate Extensions: 3
[1]: ObjectId: 2.5.29.35 Criticality=false
AuthorityKeyIdentifier [
KeyIdentifier [
0000: 7A 97 21 07 6E 4C 81 18   8F F8 92 05 22 F2 BE D5  z.!.nL......"...
0010: 98 22 01 00                                        ."..
]

]

[2]: ObjectId: 2.5.29.19 Criticality=false
BasicConstraints:[
  CA:true
  PathLen:2147483647
]

[3]: ObjectId: 2.5.29.14 Criticality=false
SubjectKeyIdentifier [
KeyIdentifier [
0000: 7A 97 21 07 6E 4C 81 18   8F F8 92 05 22 F2 BE D5  z.!.nL......"...
0010: 98 22 01 00                                        ."..
]
]

]
  Algorithm: [SHA1withRSA]
  Signature:
0000: A6 0E C8 00 92 B6 AE 56   F5 07 CD 29 39 E8 D6 9F  .......V...)9...
0010: 5F 3A 20 29 47 1F 02 FB   80 00 B7 60 EA 3E B6 19  _: )G......`.>..
0020: 4B A3 1B EA 1A 3E B0 F0   16 FB 6B 9A 4E 48 E3 65  K....>....k.NH.e
0030: 1E 3D 96 3A 65 B7 9E C3   AF D6 90 2E 85 06 57 CD  .=.:e.........W.

]