﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-wenger的天空-文章分类-Security</title><link>http://www.blogjava.net/wenger/category/3416.html</link><description /><language>zh-cn</language><lastBuildDate>Tue, 27 Feb 2007 23:19:06 GMT</lastBuildDate><pubDate>Tue, 27 Feb 2007 23:19:06 GMT</pubDate><ttl>60</ttl><item><title>keytool生成证书 查看证书信息</title><link>http://www.blogjava.net/wenger/articles/13457.html</link><dc:creator>wenger的天空</dc:creator><author>wenger的天空</author><pubDate>Mon, 19 Sep 2005 13:54:00 GMT</pubDate><guid>http://www.blogjava.net/wenger/articles/13457.html</guid><wfw:comment>http://www.blogjava.net/wenger/comments/13457.html</wfw:comment><comments>http://www.blogjava.net/wenger/articles/13457.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/wenger/comments/commentRss/13457.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/wenger/services/trackbacks/13457.html</trackback:ping><description><![CDATA[<P>首先用keytool生成证书<BR>1 产生密钥<BR>D:\&gt;keytool -genkey -alias wenger -keysize 1024 -keypass abcdef -keystore myKeys<BR>tore -storepass abcdef -dname "CN=chen sr, OU=tanglab, O=lab, L=gz, ST=gd, C=cn"<BR><BR>2 导出密钥，用证书的形式保存<BR>D:\&gt;keytool -export -alias wenger -file mycert.cer -keystore myKeystore -storepass abcdef<BR>保存在文件中的认证 &lt;mycert.cer&gt;<BR><BR>用java读出证书的信息</P>
<DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000">&nbsp;java.io.</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000">&nbsp;java.security.cert.</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/None.gif" align=top><BR><IMG id=Codehighlighter1_71_595_Open_Image onclick="this.style.display='none'; Codehighlighter1_71_595_Open_Text.style.display='none'; Codehighlighter1_71_595_Closed_Image.style.display='inline'; Codehighlighter1_71_595_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_71_595_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_71_595_Closed_Text.style.display='none'; Codehighlighter1_71_595_Open_Image.style.display='inline'; Codehighlighter1_71_595_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000">&nbsp;TestCert&nbsp;</SPAN><SPAN id=Codehighlighter1_71_595_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_71_595_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG id=Codehighlighter1_114_593_Open_Image onclick="this.style.display='none'; Codehighlighter1_114_593_Open_Text.style.display='none'; Codehighlighter1_114_593_Closed_Image.style.display='inline'; Codehighlighter1_114_593_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_114_593_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_114_593_Closed_Text.style.display='none'; Codehighlighter1_114_593_Open_Image.style.display='inline'; Codehighlighter1_114_593_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">static</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000">&nbsp;main(String[]&nbsp;args)&nbsp;</SPAN><SPAN id=Codehighlighter1_114_593_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_114_593_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG id=Codehighlighter1_124_458_Open_Image onclick="this.style.display='none'; Codehighlighter1_124_458_Open_Text.style.display='none'; Codehighlighter1_124_458_Closed_Image.style.display='inline'; Codehighlighter1_124_458_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_124_458_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_124_458_Closed_Text.style.display='none'; Codehighlighter1_124_458_Open_Image.style.display='inline'; Codehighlighter1_124_458_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">try</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN id=Codehighlighter1_124_458_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_124_458_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FileInputStream&nbsp;fis&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000">&nbsp;FileInputStream(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">mycert.cer</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CertificateFactory&nbsp;cf</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">CertificateFactory.getInstance(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">X509</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;X509Certificate&nbsp;c</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">(X509Certificate)&nbsp;cf.generateCertificate(fis);<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Certficate&nbsp;for</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000">&nbsp;c.getSubjectDN());<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Generated&nbsp;with&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000">c.getSigAlgName());<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG id=Codehighlighter1_497_503_Open_Image onclick="this.style.display='none'; Codehighlighter1_497_503_Open_Text.style.display='none'; Codehighlighter1_497_503_Closed_Image.style.display='inline'; Codehighlighter1_497_503_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_497_503_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_497_503_Closed_Text.style.display='none'; Codehighlighter1_497_503_Open_Image.style.display='inline'; Codehighlighter1_497_503_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">catch</SPAN><SPAN style="COLOR: #000000">&nbsp;(FileNotFoundException&nbsp;ex)&nbsp;</SPAN><SPAN id=Codehighlighter1_497_503_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_497_503_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG id=Codehighlighter1_541_588_Open_Image onclick="this.style.display='none'; Codehighlighter1_541_588_Open_Text.style.display='none'; Codehighlighter1_541_588_Closed_Image.style.display='inline'; Codehighlighter1_541_588_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_541_588_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_541_588_Closed_Text.style.display='none'; Codehighlighter1_541_588_Open_Image.style.display='inline'; Codehighlighter1_541_588_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">catch</SPAN><SPAN style="COLOR: #000000">&nbsp;(CertificateException&nbsp;ex)&nbsp;</SPAN><SPAN id=Codehighlighter1_541_588_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.blogjava.net/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_541_588_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG id=Codehighlighter1_549_582_Open_Image onclick="this.style.display='none'; Codehighlighter1_549_582_Open_Text.style.display='none'; Codehighlighter1_549_582_Closed_Image.style.display='inline'; Codehighlighter1_549_582_Closed_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_549_582_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_549_582_Closed_Text.style.display='none'; Codehighlighter1_549_582_Open_Image.style.display='inline'; Codehighlighter1_549_582_Open_Text.style.display='inline';" src="http://www.blogjava.net/images/OutliningIndicators/ContractedSubBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN id=Codehighlighter1_549_582_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">/**&nbsp;*/</SPAN><SPAN id=Codehighlighter1_549_582_Open_Text><SPAN style="COLOR: #008000">/**</SPAN><SPAN style="COLOR: #008000">&nbsp;@todo&nbsp;Handle&nbsp;this&nbsp;exception&nbsp;</SPAN><SPAN style="COLOR: #008000">*/</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/InBlock.gif" align=top><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>&nbsp;&nbsp;}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.blogjava.net/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN></DIV>
<P>输出：<BR>Certficate forCN=chen sr, OU=tanglab, O=lab, L=gz, ST=gd, C=cn</P>
<P>Generated with SHA1withDSA<BR></P><img src ="http://www.blogjava.net/wenger/aggbug/13457.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/wenger/" target="_blank">wenger的天空</a> 2005-09-19 21:54 <a href="http://www.blogjava.net/wenger/articles/13457.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>