自动安装数字证书根证书

转自 http://blog.csdn.net/jiftlixu/article/details/9796561

[html]  view plain  copy
  1. <a target="_blank" href="http://www.cnblogs.com/jifeng/archive/2011/05/07/2040048.html">http://www.cnblogs.com/jifeng/archive/2011/05/07/2040048.html</a>  
[html]  view plain  copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="GBK"%>  
  2. <%@ page import="java.lang.*,java.io.*"%>  
  3. <html>  
  4.     <head>  
  5.         <title>IE中自动安装数字证书测试</title>  
  6.     </head>  
  7.     <body>  
  8.         IE中使用XEnroll.InstallPKCS7自动安装根数字证书  
  9.         <br />  
  10.         备注:这里测试的根证书采用Base64编码 X.509格式(CER)  
  11.         <br />  
  12. <%       
  13. StringBuffer server_cert =new StringBuffer();  
  14. try {      
  15.     java.net.URL url =config.getServletContext().getResource("/base64_root_comsys.cer");  
  16.     BufferedReader breader =new BufferedReader(new InputStreamReader(url.openStream()));  
  17. }  
  18. catch(Exception e)  
  19. {  
  20.     e.printStackTrace();  
  21.     out.println("<HTML><BODY><P>");      
  22.     out.println("<h2>读取证书文件出错</h2> <br/>");      
  23.     out.println(e.toString());      
  24.     out.println("</P></BODY></HTML>");      
  25.     out.flush();      
  26.     out.close();  
  27. }  
  28. String Agent = request.getHeader("User-Agent");  
  29. StringTokenizer st = new StringTokenizer(Agent,";");  
  30. st.nextToken();  
  31. String userBrowser = st.nextToken();  
  32. String userOS = st.nextToken();  
  33. out.println("你的操作系统为:");  
  34. out.println(userOS);  
  35. String activexLib="XEnroll";  
  36. //检查是否是Windows Vista,Windows 2008,Windows 7,在Vista,Windows 2008,Windows 7上,需要使用 CertEnroll.dll  
  37. //Windows 2008 Server, IE7 User-Agent header: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2;...  
  38. //Windows Vista, IE7 User-Agent header: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0;...  
  39. //Windows 7,IE8 User-Agent header: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1;...  
  40.   
  41.   
  42. if(userOS.indexOf("Windows NT 6.0")>-1 || userOS.indexOf("Windows NT 6.1")>-1 || userOS.indexOf("Windows NT 5.2")>-1){  
  43.         activexLib="CertEnroll";  
  44.     }  
  45.     String sPKCS7=server_cert.toString();  
  46.     sPKCS7="-----BEGIN CERTIFICATE-----MIIDZzCCAk+gAwIBAgIJAJrhdPt6af7nMA0GCSqGSIb3DQEBBAUAMGoxFjAUBgNVBAoTDWNvbXN5cy5uZXQuY24xDTALBgNVBAsTBFVDSVQxHjAcBgNVBAMTFUNPTVNZUy5ORVQuQ04gUk9PVCBDQTEhMB8GCSqGSIb3DQEJARYSbGlhb2ppZmVuZ0AxNjMuY29tMB4XDTExMDQyMjAyMTkzMFoXDTE3MDQyMDAyMTkzMFowajEWMBQGA1UEChMNY29tc3lzLm5ldC5jbjENMAsGA1UECxMEVUNJVDEeMBwGA1UEAxMVQ09NU1lTLk5FVC5DTiBST09UIENBMSEwHwYJKoZIhvcNAQkBFhJsaWFvamlmZW5nQDE2My5jb20wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDnYB2tr8D3IcPND9tCh4c1GMnL15hdJ5oYJ12DpoaEbCciELkOmogaQ2IjVSLBCfHZKkX6X9hJqdCHH2oiGvNZyuN5mjfy+KWuebs7r9sqaTzJ6/e1vgSaiYox1DIO+oI59MH22jH3i8OMw2qbE3TqlLvVmZBflomVkGIOz95iJOzKOJwIUA3VhFYvI/Wlf4NzqfOL0zNNmoFUcD4BYVAqhVa570FFxEaGl8DvLaKUraKfho6zRHVc7MrjFrI6SlSRhe2hi/c24HrOpzoUPD46zztL/v6sSV37chsf6+V44WO4rCth0wEZzws8Hd6ch8NsUcMJjM8IOG2NMrn6x0CLAgMBAAGjEDAOMAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEEBQADggEBAD4UIk7CVBwsK7DWg74eM2zwfU4bFm02BKMVFDVNwwTdjdcGSlxrNChbymHFuhG00USxy9/d4ApWxUX/y3MxmkXusENE2Rg6Wk92k6SCfbhIOXUrI+0YxXNAjInIcABsasOZAZ/ECuIuQbap5UyEHCiy0VJKRKKNCthE2dBbnTLaS1ierSuWubuyOMGDtQTCdjU7zYjwGLSLNXkUGNfG+t5XkltXRNFxUgkVh6q2sHxo76I2taya4KQp2SM6W4t8tdJXzBvdI1me5bT7sWDu4fYwsTnjwQACXrb6PKi86jq3YxP3DV/t+Beq87NP2mJg+0Ind8RNTenM714R7VtFrz8=-----END CERTIFICATE-----";          
  47. %>  
  48.         <% if(activexLib.equals("XEnroll")){ %>  
  49.         <object id="XEnroll" classid="clsid:127698e4-e730-4e5c-a2b1-21490a70c8a1" codebase="xenroll.dll">  
  50.         </object>  
  51.         <SCRIPT language="VBSCRIPT">  
  52.             ON ERROR resume next  
  53.             sPKCS7="-----BEGIN CERTIFICATE-----MIIDZzCCAk+gAwIBAgIJAJrhdPt6af7nMA0GCSqGSIb3DQEBBAUAMGoxFjAUBgNVBAoTDWNvbXN5cy5uZXQuY24xDTALBgNVBAsTBFVDSVQxHjAcBgNVBAMTFUNPTVNZUy5ORVQuQ04gUk9PVCBDQTEhMB8GCSqGSIb3DQEJARYSbGlhb2ppZmVuZ0AxNjMuY29tMB4XDTExMDQyMjAyMTkzMFoXDTE3MDQyMDAyMTkzMFowajEWMBQGA1UEChMNY29tc3lzLm5ldC5jbjENMAsGA1UECxMEVUNJVDEeMBwGA1UEAxMVQ09NU1lTLk5FVC5DTiBST09UIENBMSEwHwYJKoZIhvcNAQkBFhJsaWFvamlmZW5nQDE2My5jb20wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDnYB2tr8D3IcPND9tCh4c1GMnL15hdJ5oYJ12DpoaEbCciELkOmogaQ2IjVSLBCfHZKkX6X9hJqdCHH2oiGvNZyuN5mjfy+KWuebs7r9sqaTzJ6/e1vgSaiYox1DIO+oI59MH22jH3i8OMw2qbE3TqlLvVmZBflomVkGIOz95iJOzKOJwIUA3VhFYvI/Wlf4NzqfOL0zNNmoFUcD4BYVAqhVa570FFxEaGl8DvLaKUraKfho6zRHVc7MrjFrI6SlSRhe2hi/c24HrOpzoUPD46zztL/v6sSV37chsf6+V44WO4rCth0wEZzws8Hd6ch8NsUcMJjM8IOG2NMrn6x0CLAgMBAAGjEDAOMAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEEBQADggEBAD4UIk7CVBwsK7DWg74eM2zwfU4bFm02BKMVFDVNwwTdjdcGSlxrNChbymHFuhG00USxy9/d4ApWxUX/y3MxmkXusENE2Rg6Wk92k6SCfbhIOXUrI+0YxXNAjInIcABsasOZAZ/ECuIuQbap5UyEHCiy0VJKRKKNCthE2dBbnTLaS1ierSuWubuyOMGDtQTCdjU7zYjwGLSLNXkUGNfG+t5XkltXRNFxUgkVh6q2sHxo76I2taya4KQp2SM6W4t8tdJXzBvdI1me5bT7sWDu4fYwsTnjwQACXrb6PKi86jq3YxP3DV/t+Beq87NP2mJg+0Ind8RNTenM714R7VtFrz8=-----END CERTIFICATE-----"  
  54.             XEnroll.InstallPKCS7 sPKCS7  
  55.             //XEnroll.InstallPKCS7用于安装根证书。          
  56.             XEnroll.InstallPKCS7 sPKCS7       
  57.              if err.Number <> 0 then             
  58.                  if err.number = -2146885628 then                 
  59.                      MsgBox "Keyset does not exist"              
  60.                  else                 
  61.                      MsgBox "证书下载时出错,错误号="&err.description              
  62.                  end if          
  63.             else              
  64.                 MsgBox "证书已成功装入"          
  65.             end if  
  66.     </script>  
  67.         <% }   
  68. else {%>  
  69. <!--    
  70.         //方法来源://http://blogs.msdn.com/alejacma/archive/2009/01/28/how-to-create-a-certificate-request-with-certenroll-javascript.aspx  
  71.         //Vista下由于暂时没有测试环境,方法尚待验证  
  72. -->  
  73.         <object id="objCertEnrollClassFactory" classid="clsid:884e2049-217d-11da-b2a4-000e7bbb2b09"></object>  
  74.         <script language="javascript">  
  75.           function InstallCert(){  
  76.                 document.write("<br>Installing certificate...");          
  77.                 try {              
  78.                     // Variables              
  79.                     var objEnroll = objCertEnrollClassFactory.CreateObject("X509Enrollment.CX509Enrollment")              
  80.                     var sPKCS7 = "<%= sPKCS7 %>"              
  81.                     objEnroll.Initialize(1); // ContextUser              
  82.                     objEnroll.InstallResponse(0, sPKCS7, 6, "");   
  83.                     // AllowNone = 0XCN_CRYPT_STRING_BASE64_ANY = 6          
  84.                 }catch (err)   
  85.                 {              
  86.                     document.write("<br>" + err.description);              
  87.                     return false;         
  88.                  }      
  89.                 return true;  
  90.             }      
  91.         InstallCert();       
  92. </script>  
  93.         <% } %>  
  94.         <%/*  
  95. out.println("用下载方式下载p12格式的文件下载后安装");   
  96. ClassLoader cl = this.getClass().getClassLoader();  
  97. try   
  98. {      
  99. InputStream is = cl.getResourceAsStream("liangchuan.p12");     
  100.  //response.setContentType("application/x-x509-ca-cert");     
  101.  response.setContentType("application/x-pkcs12");      
  102. response.addHeader("Content-Disposition", "attachment; filename=liangchuan.p12");     
  103.  OutputStream os = response.getOutputStream();      
  104. //InputStream is = new FileInputStream(fileName);     
  105.  while (is.available() > 0)   
  106. {          
  107. char c = (char) is.read();         
  108.  os.write(c);    }      
  109. os.flush();      
  110. is.close(); }   
  111. catch (Exception e) {       
  112. out.println("<HTML><BODY><P>");      
  113. out.println("<h2>下载证书文件出错</h2> <br/>");      
  114. out.println(e.toString());      
  115. out.println("</P></BODY></HTML>");      
  116. out.flush();      
  117. out.close(); }*/%>  
  118.     </body>  
  119. </html>  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值