JS(Javascript)安裝數位憑證

來源:互聯網
上載者:User

在win7系統下

<!DOCTYPE html><html><head><title>MyHtml.html</title><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="this is my page"><meta http-equiv="content-type" content="text/html; charset=UTF-8"><!--<link rel="stylesheet" type="text/css" href="./styles.css">--></head><body><textarea id="sPKCS7" rows="6" cols="50">-----BEGIN CERTIFICATE-----MIICQTCCAaq   ** 認證內容  **/wpUHBs8=-----END CERTIFICATE-----</textarea><br><object id="objCertEnrollClassFactory"classid="clsid:884e2049-217d-11da-b2a4-000e7bbb2b09"></object><noscript>瀏覽器不支援Javascript或者是您的安全設定不允許Javascript運行,請調整您的瀏覽器設定!</noscript><script type="text/javascript">function installCertForWin7(){document.write("<br>正在安裝認證...");try{var certEnroll=document.getElementById("objCertEnrollClassFactory");var objEnroll=certEnroll.CreateObject("X509Enrollment.CX509Enrollment");var cer=document.getElementById("sPKCS7").innerText;objEnroll.Initialize(1);objEnroll.InstallResponse(4,cer,6,"");document.write("<br>認證安裝成功!");}catch(ex){document.write("<br><font color='red'>"+ex.description+"</font>");return false;}return true;}installCertForWin7();</script>This is my HTML page.<br></body></html>

在xp系統下

<!DOCTYPE html><html><head><title>MyHtml.html</title><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="this is my page"><meta http-equiv="content-type" content="text/html; charset=UTF-8"><!--<link rel="stylesheet" type="text/css" href="./styles.css">--></head><body><textarea id="sPKCS7" rows="6" cols="50">-----BEGIN CERTIFICATE----- //這裡是認證內容。MIICQTCCAaqgA****ckS5yg7l+NPjkDjT77onAoo4iJIjsbsGF/VMQENTCTXA4S+qFL/a+Fur5EDJ4g4fzwRQZjs1ROFzbPVAwsw/wpUHBs8=-----END CERTIFICATE-----</textarea><br><object id="XEnroll"classid="clsid:127698e4-e730-4e5c-a2b1-21490a70c8a1"></object><noscript>瀏覽器不支援Javascript或者是您的安全設定不允許Javascript運行,請調整您的瀏覽器設定!</noscript><script type="text/javascript">function installCertForWinXP(){document.write("<br>正在安裝認證...");try{var enroll=document.getElementById("XEnroll");var cer=document.getElemxentById("sPKCS7").innerText;enroll.InstallPKCS7(cer);}catch(ex){document.write("<br><font color='red'>"+ex.description+"</font>");return false;}return true;}installCertForWinXP();</script>This is my HTML page.<br></body></html>

Java中讀取認證內容,轉成上述中的字串認證內容

package dcec.rdd;import java.io.FileInputStream;import java.io.IOException;import java.net.URLEncoder;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import sun.misc.BASE64Encoder;public class TestCert extends HttpServlet {/** * Constructor of the object. */public TestCert() {super();}/** * Destruction of the servlet. <br> */public void destroy() {super.destroy(); // Just puts "destroy" string in log// Put your code here}/** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. *  * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {}/** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. *  * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {String type=request.getParameter("type");//獲得根憑證的檔案目錄String rootPath=this.getServletContext().getRealPath("/");if(type.equals("root")){//表示安裝根憑證rootPath+="cert\\dcec1D.cer";}else if(type.equals("sign")){//表示安裝數位簽章認證rootPath+="cert\\dcec1D.cer";}//讀取跟認證FileInputStream inUserCert=new FileInputStream(rootPath);//檔案輸入資料流,採用二進位的方式讀取int len=inUserCert.available();//擷取認證總長度byte[] userCert=new byte[len];//聲明一個byte類型的數組,長度為認證的長度inUserCert.read(userCert);//將認證讀取到byte裡inUserCert.close();//關閉輸入資料流BASE64Encoder encoder=new BASE64Encoder();//聲明一個編碼器String strCert=encoder.encode(userCert);//對認證進行編碼//對認證進行組裝,使其可以為指令碼安裝所使用strCert="-----BEGIN CERTIFICATE-----"+strCert;//添加認證標識的頭部和尾部strCert+="-----END CERTIFICATE-----";//剛才讀取的認證內容response.setContentType("application/x-pkcs7-certificates");response.addHeader("Content-Disposition", "attachment;filename="+URLEncoder.encode("蘇州市數字城市工程研究中心有限公司","UTF-8"));response.getOutputStream().write(userCert);response.getOutputStream().flush();response.getOutputStream().close();}/** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */public void init() throws ServletException {// Put your code here}}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.