📄 java
字号:
<script language="javascript">
if (getCookie("SINAPRO") == "") {
print_stand_unipro_head();
} else {
print_stand_unipro_welcome();
}
</script>
</td></tr>
</table>
<!--导航end-->
<!--头部结束-->
<!--科技新闻内页顶部通栏开始-->
<!--A48D92A8239F-->
<a href=http://oascentral.sina.com/RealMedia/ads/click_lx.ads/us.sina.com/clickscommand/1234/x01/Sina.com/US_CMSforex_0205_Fix_CN/cms_1004_techContentBnr.gif/1
target=_blank><img src=http://ad4.sina.com.cn/200502/03/10833_750x80_cms.GIF border=0 width=750 height=80></a><!--$$ shuchang/2005-2-12 ~ 2005-2-17/B $-->
<!--科技新闻内页顶部通栏结束-->
<table width=750 border=0 cellspacing=0 cellpadding=0>
<tr><td height=34 width=150><a href=http://tech.sina.com.cn><img src=http://image2.sina.com.cn/IT/images/sina_kjsd.gif width=144 height=34 border=0 alt=科技时代></a></td><td width=440 valign=bottom><font color=#0000ff><a href=http://home.sina.com.cn/ class=a02>新浪首页</a> > <a href=http://tech.sina.com.cn/ class=a02>科技时代</a> > <a href=/s_h/news/ class=a02>软件</a> > 正文</font></td><td align=right valign=bottom width=150><a href=http://www.yesky.com/ target=_blank><img src=http://image2.sina.com.cn/IT/images/meiti_logo/U1084P2T50D213F629DT20041213105743.gif border=0 height=28></a></td><td width=10></td></tr>
</table>
<table width=750 border=0 cellspacing=0 cellpadding=0>
<tr><td height=8></td></tr>
<tr><td height=1 bgcolor=#747474><img src=http://image2.sina.com.cn/c.gif width=1 height=1></td></tr>
</table>
<div id="outer" style="position:relative;width:750px;">
<table width=750 border=0 cellspacing=0 cellpadding=0>
<tr><td width=620 valign=top align=center rowspan=2 bgcolor=#EDF0F5>
<br>
<div id=article>
<table width=560 border=0 cellspacing=0 cellpadding=0>
<tr><th class=f24><font color=#05006C><h1>Java加密和数字签名编程快速入门(2)</h1></font></th></tr>
<tr><td height=><hr size=1 bgcolor=#d9d9d9></td></tr>
<tr><td height=20 align=center>http://www.sina.com.cn 2005年02月17日 11:21 <font color=#A20010>天极yesky</font></td></tr>
<tr><td height=15></td></tr>
<tr><td class=l17><font id="zoom" class=f14>
<!-- 正文内部文字导航 : begin -->
<!-- 正文内部文字导航 : end -->
<p> 文/jwsh1984</p>
<p>
<p><span class=f14> 4)数字签名:<br><br> 数字签名,它是确定交换消息的通信方身份的第一个级别。上面A通过使用公钥加密数据后发给B,B利用私钥解密就得到了需要的数据,问题来了,由于都是使用公钥加密,那么如何检验是A发过来的消息呢?上面也提到了一点,私钥是唯一的,那么A就可以利用A自己的私钥进行加密,然后B再利用A的公钥来解密,就可以了;数字签名的原理就基于此,而通常为了证明发送数据的真实性,通过利用消息摘要获得简短的消息内容,然后再利用私钥进行加密散列数据和消息一起发送。java中为数字签名提供了良好的支持,java.security.Signature类提供了消息签名:<br><br><p>
<p>
<table borderColor=#ffcc66 width="90%" align=center bgColor=#dadacf border=1>
<tbody>
<tr>
<td>/**<br>*DigitalSignature2Example.java<br>*Copyright 2005-2-16<br>*/<br>import java.security.Signature;<br>import java.security.KeyPairGenerator;<br>import java.security.KeyPair;<br>import java.security.SignatureException;<br><br>/**<br>*数字签名,使用RSA私钥对对消息摘要签名,然后使用公?验证 测试<br>*/<br>public class DigitalSignature2Example{<br> public static void main(String[] args) throws Exception{<br> if(args.length!=1){<br> System.err.println("Usage:java DigitalSignature2Example <text>");<br> System.exit(1);<br> }<br><br> byte[] plainText=args[0].getBytes("UTF8");<br> //形成RSA公钥对<br> System.out.println("\nStart generating RSA key");<br> KeyPairGenerator keyGen=KeyPairGenerator.getInstance("RSA");<br> keyGen.initialize(1024);<br><br> KeyPair key=keyGen.generateKeyPair();<br> System.out.println("Finish generating RSA key");<br> //使用私?签名<br> Signature sig=Signature.getInstance("SHA1WithRSA");<br> sig.initSign(key.getPrivate());<br> sig.update(plainText);<br> byte[] signature=sig.sign();<br> System.out.println(sig.getProvider().getInfo());<br> System.out.println("\nSignature:");<br> System.out.println(new String(signature,"UTF8"));<br><br> //使用公?验证<br> System.out.println("\nStart signature verification");<br> sig.initVerify(key.getPublic());<br> sig.update(plainText);<br> try{<br> if(sig.verify(signature)){<br> System.out.println("Signature verified");<br> }else System.out.println("Signature failed");<br> }catch(SignatureException e){<br> System.out.println("Signature failed");<br> }<br> }<br>}</td></tr></tbody></table><p>
<p><br> 5)数字证书。<br><br> 还有个问题,就是公钥问题,A用私钥加密了,那么B接受到消息后,用A提供的公钥解密;那么现在有个讨厌的C,他把消息拦截了,然后用自己的私钥加密,同时把自己的公钥发给B,并告诉B,那是A的公钥,结果....,这时候就需要一个中间机构出来说话了(相信权威,我是正确的),就出现了Certificate Authority(也即<a href='http://finance.sina.com.cn/110/2004-10-20/575.html' class=akey target=_blank>CA</a>),有名的CA机构有Verisign等,目前数字认证的工业标准是:CCITT的X.509:<br>数字证书:它将一个身份标识连同公钥一起进行封装,并由称为认证中心或 CA 的第三方进行数字签名。<br><br> 密钥库:java平台为你提供了密钥库,用作密钥和证书的资源库。从物理上讲,密钥库是缺省名称为 .keystore 的文件(有一个选项使它成为加密文件)。密钥和证书可以拥有名称(称为别名),每个别名都由唯一的密码保护。密钥库本身也受密码保护;您可以选择让每个别名密码与主密钥库密码匹配。 <br><br> 使用工具keytool,我们来做一件自我认证的事情吧(相信我的认证):<br><br> 1、创建密钥库keytool -genkey -v -alias feiUserKey -keyalg RSA 默认在自己的home目录下(windows系统是c:\documents and settings\<你的用户名> 目录下的.keystore文件),创建我们用 RSA 算法生成别名为 feiUserKey 的自签名的证书,如果使用了-keystore mm 就在当前目录下创建一个密钥库mm文件来保存密钥和证书。<br><br> 2、查看证书:keytool -list 列举了密钥库的所有的证书 <br><br> 也可以在dos下<a href='http://tech.sina.com.cn/n/2005-01-26/1415515637.shtml' class=akey target=_blank>输入</a>keytool -help查看帮助。<br>
<p align=right><a style="FONT-SIZE: 14px" href="http://tech.sina.com.cn/s/s/2005-02-17/1121528490.shtml">[上一页]</a> <a href="http://tech.sina.com.cn/s/s/2005-02-17/1121528490.shtml">[1]</a> [2] <a href="http://tech.sina.com.cn/s/s/2005-02-17/1121528492.shtml">[3]</a> <a style="FONT-SIZE: 14px" href="http://tech.sina.com.cn/s/s/2005-02-17/1121528492.shtml">[下一页]</a><p>
<!--NEWSZW_HZH_BEGIN-->
<table border=0 cellspacing=0 cellpadding=0 align=left style="margin-top:10px;margin-bottom:3px;margin-left:4px;margin-right:7px">
<!--
<table id="innerad" border=0 cellspacing=0 cellpadding=0 align=left style="display:none">
-->
<tr><td>
<!--画中画广告开始-->
<table border=0 cellspacing=0 cellpadding=0 align=left>
<tr><td>
<!--科技新闻内页画中画开始-->
<!--A52493769A6C-->
<a href=http://noshow.adsina.allyes.com/main/adfclick?db=sina&bid=4029,19585,19613&cid=0,0,0&sid=20015&advid=1293&camid=4243&show=ignore&url=http://cn.rd.yahoo.com/auct/promo/sina/200411/tihuan/evt=28317/*http://cn.auctions.yahoo.com/?refcode=sinag
target=_blank><img src=http://ad4.sina.com.cn/200411/16/5037_360x300.gif border=0 width=360 height=300></a><!--$$ ae/2005-2-7 ~ 2005-2-7/A $-->
<!--科技新闻内页画中画结束-->
</td></tr></table>
<!--画中画广告结束-->
</td></tr>
<tr><td>
<!-- 画中画下文字链广告(从上至下顺序为01,02,03,04文字,需加class=a01)-->
<table width=360 border=0 cellpadding=0 cellspacing=0>
<tr><td height=9></td></tr>
<tr><td>
<table width=360 border=0 cellpadding=0 cellspacing=0 background=http://image2.sina.com.cn/dy/images/xfrd_02.gif>
<tr><td height=45 rowspan=2><img src=http://ad4.sina.com.cn/shc/xfrd_01.GIF width=70 height=45 border=0></td><td width=286 style='padding-top:4px;padding-left:5px'>
<!--F70BB90BB6BA--><!--nwy/uc/A-->
<script LANGUAGE="JavaScript">
ad1= "新浪推出点点通阅读器";
link1= "http://noshow.adsina.allyes.com/main/adfclick?db=sina&bid=3450,15660,15679&cid=0,0,0&sid=16002&advid=1293&camid=3644&show=ignore&url=http://rss.sina.com.cn/";
//左上
ad2= "新浪UC 千娇百媚";
link2= "http://noshow.adsina.allyes.com/main/adfclick?db=sina&bid=3612,16626,16650&cid=0,0,0&sid=16989&advid=1293&camid=3812&show=ignore&url=http://beauty.51uc.com/activity/wulindahui/index.html";
//右上
ad3= "新浪推出点点通阅读器";
link3= "http://noshow.adsina.allyes.com/main/adfclick?db=sina&bid=3450,15660,15679&cid=0,0,0&sid=16002&advid=1293&camid=3644&show=ignore&url=http://rss.sina.com.cn/";
//左下
ad4= "快乐消费在新浪";
link4= "http://sinapay.sina.com.cn/static/xljh.html";
//右下
//浏览器类型变量
var InternetExplorer = navigator.appName.indexOf("Microsoft") != -1;
//ie内容
function IEad(){
adCode = '<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" WIDTH="280" HEIGHT="40" id="ad_note" ALIGN=""><PARAM NAME=movie VALUE="http://image2.sina.com.cn/dy/zwyhzh/ad_note.swf"> <PARAM NAME=quality VALUE=high> <PARAM NAME=wmode VALUE=transparent> <PARAM NAME=bgcolor VALUE=#FFFFFF> <param name="swLiveConnect" value="true"> '
+' <EMBED src="http://image2.sina.com.cn/dy/zwyhzh/ad_note.swf" quality=high wmode=transparent bgcolor=#FFFFFF WIDTH="280" HEIGHT="40" NAME="ad_note" ALIGN="" TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer" swLiveConnect="true"></EMBED></OBJECT>'
document.write(adCode);
document.getElementById('ad_note').SetVariable("ad1", ad1);
document.getElementById('ad_note').SetVariable("ad2", ad2);
document.getElementById('ad_note').SetVariable("ad3", ad3);
document.getElementById('ad_note').SetVariable("ad4", ad4);
document.getElementById('ad_note').SetVariable("link1", link1);
document.getElementById('ad_note').SetVariable("link2", link2);
document.getElementById('ad_note').SetVariable("link3", link3);
document.getElementById('ad_note').SetVariable("link4", link4);
}
//ns内容
function NSad(){
adCode = '<table width=286 border=0 cellpadding=0 cellspacing=0>'
+'<tr height=20><td width=143 style="padding-top:5px;padding-left:5px"> <img src=http://image2.sina.com.cn/dy/images/xfrd_04.gif width=7 height=7> '
+'<a href='+ link1 +' class=a01 target=_blank>'+ ad1 +'</a>'
+'</td><td width=143 style="padding-top:5px;"> <img src=http://image2.sina.com.cn/dy/images/xfrd_04.gif width=7 height=7> '
+'<a href='+ link2 +' class=a01 target=_blank>'+ ad2 +'</a>'
+'</td></tr>'
+'<tr height=20><td style="padding-left:5px"> <img src=http://image2.sina.com.cn/dy/images/xfrd_04.gif width=7 height=7> '
+'<a href='+ link3 +' class=a01 target=_blank>'+ ad3 +'</a>'
+'</td><td> <img src=http://image2.sina.com.cn/dy/images/xfrd_04.gif width=7 height=7> '
+'<a href='+ link4 +' class=a01 target=_blank><SPAN ID="oSpan" UNSELECTABLE="on" >'+ ad4 +'</span></a></td></tr></table>'
document.write(adCode);
}
//主过程
if(InternetExplorer == true){
IEad();
}else{
NSad();
}
</script>
</td><td width=4 rowspan=2><img src=http://image2.sina.com.cn/dy/images/xfrd_03.gif width=4 height=45></td></tr>
</table>
</td></tr>
</table>
</td></tr>
</table>
<!--NEWSZW_HZH_END-->
<table width=90% border=0 align=center>
<tr></tr>
</table>
<br clear=all>
<table width=565 border=0 cellspacing=0 cellpadding=0>
<tr><td class=f14 height=30 valign=top> 点击此处查询<a href=http://chanews.sina.com.cn/s.cgi?k=keyword&c=2&k=Java target=_blank>全部<font color=red>Java</font>新闻</a> <a href=http://chanews.sina.com.cn/s.cgi?k=keyword&c=2&k=编程 target=_blank>全部<font color=red>编程</font>新闻</a> <a href=http://chanews.sina.com.cn/s.cgi?k=keyword&c=2&k=编程语言 target=_blank>全部<font color=red>编程语言</font>新闻</a> </td></tr></table>
</td></tr>
</table>
</div>
<br>
<table width=560 border=0 cellspacing=0 cellpadding=0>
<tr><td>
<table width=565 border=0 cellspacing=0 cellpadding=0>
<tr><td><form target="_blank" action="http://mms.sina.com.cn/xmlmms/xmlmmsQue.php" method="post" name="from_">
<input type="hidden" name="xmlCfg" value="http://rss.sina.com.cn/mms/tech/72/29/48/2-1-528491.xml">
<input type="hidden" name="sourceFrom" value="100001">
<input type="hidden" name="from" value="442">
<input type="submit" name="submit_" style="width:120" value="多种方式看新闻">
</form></td><td align=right>【<a href=http://comment.news.sina.com.cn/cgi-bin/comment/comment.cgi?channel=kj&newsid=528490>评论</a>】【<a href=http://forum.tech.sina.com.cn/cgi-bin/tree.cgi?gid=23&fid=288>应用软件</a>】【<a href=http://stat.sina.com.cn/cgi-bin/sms/edit_sms.cgi?title=Java%BC%D3%C3%DC%BA%CD%CA%FD%D7%D6%C7%A9%C3%FB%B1%E0%B3%CC%BF%EC%CB%D9%C8%EB%C3%C5%282%29&url=tech.sina.com.cn/s/s/2005-02-17/1121528491.shtml>推荐</a>】【<a href="javascript:doZoom(16)">大</a> <a href="javascript:doZoom(14)">中</a> <a href="javascript:doZoom(12)">小</a>】【<a href="javascript:doPrint()">打印</a>】【<a href=http://www.sina.com.cn/ddt/ target=_blank>下载点点通</a>】【<a href="javascript:window.close()">关闭</a>】</td></tr>
</table></td></tr>
</table>
<BR>
<table width=560 border=0 cellspacing=0 cellpadding=0>
<tr><td>
<!-- 正文底部小通栏 -->
<table width=585 border=0 cellpadding=0 cellspacing=0 align=center>
<tr><td>
<!--科技频道内页底部小通栏开始-->
<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0" WIDTH="585" HEIGHT="50">
<PARAM NAME=movie VALUE="http://ad4.sina.com.cn/200501/27/10283_ad_jy_585x50.swf"> <PARAM NAME=quality VALUE=high><param name=wmode value=opaque>
<EMBED src="http://ad4.sina.com.cn/200501/27/10283_ad_jy_585x50.swf" quality=high WIDTH="585" HEIGHT="50" TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></EMBED>
</OBJECT><!--nwy/MAIL/A-->
<!--科技频道内页底部小通栏结束-->
</td></tr>
<tr><td height=5></td></tr>
</table>
<!--Adforward Begin:测试勿删-->
<IFRAME MARGINHEIGHT=0 MARGINWIDTH=0 FRAMEBORDER=0 WIDTH=1 HEIGHT=1 SCROLLING=NO SRC="http://153.adsina.allyes.com/main/adfshow?user=AFP6_for_SINA|Tech|TechPIP&db=sina&border=0&local=yes">
<SCRIPT LANGUAGE="JavaScript1.1" SRC="http://153.adsina.allyes.com/main/adfshow?user=AFP6_for_SINA|Tech|TechPIP&db=sina&local=yes&js=on"></SCRIPT>
<NOSCRIPT><A HREF="http://153.adsina.allyes.com/main/adfclick?user=AFP6_for_SINA|Tech|TechPIP&db=sina"><IMG SRC="http://153.adsina.allyes.com/main/adfshow?user=AFP6_for_SINA|Tech|TechPIP&db=sina" WIDTH=1 HEIGHT=1 BORDER=0></a></NOSCRIPT></IFRAME>
<!--Adforward End-->
</td></tr>
</table>
<br>
<table width=560 border=0 cellspacing=0 cellpadding=0>
<tr><td>
<div id=PublicRelation1 name="PublicRelation" style="DISPLAY:none">
<table><tr><td> </td>
<td class=f14>
<p><!--要求文字在17字以内!-->
<!--科技新闻内页文字链01开始-->
<a href=http://sms.sina.com.cn/ target=_blank>创意贺岁更精彩</a><!--nwy/SMS-2/A-->
<!--科技新闻内页文字链01结束-->
<!--科技新闻内页文字链02开始-->
<a href=http://bobo.sina.com.cn target=_blank>免费点歌祝福 别出心裁!</a><!--nwy/SMS-ivr/A-->
<!--科技新闻内页文字链02结束--></p>
</td></tr></table>
</div>
<SCRIPT>
//<!--广告发布-->
<!--
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -