⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 j-sec1-6-6.html

📁 Java安全--密码
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<tr valign="middle">
<td class="dwsearch" height="8"><select class="dwsearch" name="selScope"><option value="dW">dW 中国所有内容</option><option value="dW">.................  </option><option value="dW">dW 中国技术专区:</option><option value="comptZ"> 组件技术</option><option value="javaZ"> Java 技术</option><option value="linuxZ"> Linux</option><option value="xmlZ"> XML</option><option value="linuxZ">dW 中国特别专题:</option><option value="securityZ"> 安全</option><option value="unicodeZ"> Unicode</option><option value="all">IBM 全部内容</option></select></td><td class="small"><img alt="" border="0" height="1" width="5" src="../i/c.gif"></td><td><input maxlength="100" size="15" height="15" name="UserRestriction" class="dwsearch" type="text"></td><td><img alt="" height="1" width="5" border="0" src="../i/c.gif"></td><td width="64"><input alt="搜索" name="Search" value="Search" border="0" height="23" width="64" src="../i/search.gif" type="image"></td><td valign="top"><img alt="" height="1" width="34" border="0" src="../i/c.gif"></td>
</tr>
<tr valign="top">
<td class="small" colspan="6"><a target="_blank" style="color: #ffffff;" href="http://210.82.67.149/help.htm">高级搜索</a><img alt="" height="1" width="1" border="0" src="../i/c.gif"></td>
</tr>
<tr>
<td colspan="6"><img alt="" height="4" width="390" border="0" src="../i/c.gif"></td>
</tr>
</form>
</table>
</td>
</tr>
<tr>
<td class="hbg" height="21" width="160"></td><td class="bbg" valign="top" height="21" colspan="2">  <a href="/developerWorks/cn/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/" class="mainlink">IBM 主页</a><span class="divider"> | </span><a href="/developerWorks/cn/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/products/index.shtml" class="mainlink">产品与服务</a><span class="divider"> | </span><a href="/developerWorks/cn/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/support/index.shtml" class="mainlink">支持与下载</a><span class="divider"> | </span><a href="/developerWorks/cn/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/account/index.shtml" class="mainlink">个性化服务</a></td>
</tr>
</table>
<TABLE width="100%" cellspacing="0" cellpadding="0" border="0">
<TR>
<TD bgcolor="#ffffff" height="1" width="150"><IMG alt="" src="../i/c.gif" width="150" height="1"></TD>
</TR>
<TR>
<TD bgcolor="#cc6633" height="3" width="150"><IMG alt="" src="../i/c.gif" width="150" height="3"></TD>
</TR>
<TR>
<TD bgcolor="#333333" height="1" width="150"><IMG alt="" src="../i/c.gif" width="150" height="1"></TD>
</TR>
<TR>
<TD bgcolor="#000000" height="1" width="150"><IMG alt="" src="../i/c.gif" width="150" height="1"></TD>
</TR>
<TR>
<TD bgcolor="#ffffff" height="1" width="150"><IMG alt="" src="../i/c.gif" width="150" height="2"></TD>
</TR>
</TABLE>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td><font face="Verdana, sans-serif" size="-1"><a href="http://www.ibm.com/">IBM</a> : <a href="/developerWorks/cn/index.shtml">developerWorks 中国网站</a> : <a href="/developerWorks/cn/java/index.shtml">java</a> : <a href="/developerWorks/cn/cnedu.nsf/java-onlinecourse-bytitle">教学 - 在线教程</a></font>
<br>
<img alt="Java 安全性,第一部分:密码术基础" src="imagemaster/masthead.jpg"></td><td width="*" valign="bottom" align="right"><a border="0" href="j-sec1.zip"><img alt="下载 ZIP 文件" border="0" src="../i/icon-zip.gif"></a><a target="_blank" href="../tutorial_eng/index.html" border="0"><img alt="英文原文" border="0" src="../i/icon-source.gif"></a>
					&nbsp;&nbsp;&nbsp;
				</td>
</tr>
<tr>
<td height="2" colspan="2"><img alt="" height="2" width="1" src="../i/c.gif"></td>
</tr>
</table>
<TABLE border="0" cellpadding="0" cellspacing="0" width="100%">
<TR>
<TD width="150" height="1" bgcolor="#000000" colspan="6"><IMG alt="" height="1" width="150" src="../i/c.gif"></TD>
</TR>
<TR>
<TD background="../i/sw-gold.gif"><a border="0" href="index.html" onMouseOver="iOver('topmain'); iOver('bottommain'); self.status=mainblurb; return true;" onMouseOut="iOut('topmain'); iOut('bottommain'); self.status=''; return true;"><img alt="主菜单" border="0" src="../i/main.gif" name="topmain"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topsection'); iOver('bottomsection'); self.status=sectionblurb; return true;" onMouseOut="iOut('topsection'); iOut('bottomsection'); self.status=''; return true;" href="index6.html"><img alt="章节菜单" border="0" src="../i/section.gif" name="topsection"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topfeedback'); iOver('bottomfeedback'); self.status=feedbackblurb; return true;" onMouseOut="iOut('topfeedback'); iOut('bottomfeedback'); self.status=''; return true;" href="j-sec1-10-3.html"><img alt="给出此教程的反馈意见" border="0" src="../i/feedback.gif" name="topfeedback"></a></TD><TD width="100%" background="../i/sw-gold.gif"><img alt="" src="../i/c.gif"></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topprevious'); iOver('bottomprevious'); self.status=previousblurb; return true;" onMouseOut="iOut('topprevious'); iOut('bottomprevious'); self.status=''; return true;" href="j-sec1-6-5.html"><img alt="上页" border="0" src="../i/previous.gif" name="topprevious"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topnext'); iOver('bottomnext'); self.status=nextblurb; return true;" onMouseOut="iOut('topnext'); iOut('bottomnext'); self.status=''; return true;" href="j-sec1-6-7.html"><img alt="下页" border="0" src="../i/next.gif" name="topnext"></a></TD>
</TR>
</TABLE>
<table bgcolor="ffffff" cellspacing="0" cellpadding="2" border="0" height="400" width="100%">
<tr valign="bottom">
<a name="navskip"></a><td height="25" colspan="4"><img alt="6.无纸签名" src="imagemaster/titlebar6.jpg" border="0" height="25" width="562"></td>
</tr>
<tr>
<td bgcolor="ffffff" width="15">&nbsp;</td><td bgcolor="ffffff" width="12">&nbsp;</td><td valign="top" align="left" bgcolor="ffffff" width="*">
<p>
<br>
<table cellspacing="0" cellpadding="0" border="0">
<tr>
<td width="90%"><font size="4" face="Verdana, Arial, Helvetica"><b>数字签名代码示例:简单的方法</b></font></td><td width="200" align="right"><font size="1" face="Verdana, Arial, Helvetica"><nobr>   第 6 页(共7 页)</nobr></font></td>
</tr>
</table>
<br>
<br>
</p>
<font size="2" face="Verdana, Arial, Helvetica">
<p>
<code>Signature</code> 类使用由 <code>KeyPairGenerator</code> 类产生的密钥来操作数字签名。下面的示例中使用了下列方法:</p>
<ul>
<li>
<code>KeyPairGenerator.getInstance("RSA")</code>、<code>.initialize(1024)</code> 和 <code>.generateKeyPair()</code>:生成密钥。<br>
<br>
</li>
<li>
<code>Cipher.getInstance("MD5WithRSA")</code>:创建 <code>Signature</code> 对象。<br>
<br>
</li>
<li>
<code>.initSign(key.getPrivate())</code>:初始化 <code>Signature</code> 对象。<br>
<br>
</li>
<li>
<code>.update(plainText)</code> 和 <code>.sign()</code>:用明文字符串计算签名。<br>
<br>
</li>
<li>
<code>.initVerify(key.getPublic())</code> 和 <code>.verify(signature)</code>:验证签名。</li>
</ul>
<pre>
<code style="font-family: Courier New, Courier, monospace; font-size: 12"> 
import java.security.*;
import javax.crypto.*;
//
// This example uses the digital signature features to generate and
// verify a signature much more easily than the previous example
public class DigitalSignature2Example {

  public static void main (String[] args) throws Exception {
    //
    // check args and get plaintext
    if (args.length !=1) {
      System.err.println("Usage: java DigitalSignature1Example text");
      System.exit(1);
    }
    byte[] plainText = args[0].getBytes("UTF8");
    //
    // generate an RSA keypair
    System.out.println( "\nStart generating RSA key" );
    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
    keyGen.initialize(1024);

    KeyPair key = keyGen.generateKeyPair();
    System.out.println( "Finish generating RSA key" );
    //
    // get a signature object using the MD5 and RSA combo
    // and sign the plaintext with the private key,
    // listing the provider along the way

    Signature sig = Signature.getInstance("MD5WithRSA");
    sig.initSign(key.getPrivate());
    sig.update(plainText);
    byte[] signature = sig.sign();
    System.out.println( sig.getProvider().getInfo() );
    System.out.println( "\nSignature:" );
    System.out.println( new String(signature, "UTF8") );
    //
    // verify the signature with the public key
    System.out.println( "\nStart signature verification" );
    sig.initVerify(key.getPublic());
    sig.update(plainText);
    try {
      if (sig.verify(signature)) {
        System.out.println( "Signature verified" );
      } else System.out.println( "Signature failed" );
    } catch (SignatureException se) {
      System.out.println( "Signature failed" );
    }
  }
}
</code>
</pre>
<br>
</font></td>
</tr>
</table>
<TABLE border="0" cellpadding="0" cellspacing="0" width="100%">
<TR>
<TD background="../i/sw-gold.gif"><a border="0" href="index.html" onMouseOver="iOver('topmain'); iOver('bottommain'); self.status=mainblurb; return true;" onMouseOut="iOut('topmain'); iOut('bottommain'); self.status=''; return true;"><img alt="主菜单" border="0" src="../i/main.gif" name="bottommain"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topsection'); iOver('bottomsection'); self.status=sectionblurb; return true;" onMouseOut="iOut('topsection'); iOut('bottomsection'); self.status=''; return true;" href="index6.html"><img alt="章节菜单" border="0" src="../i/section.gif" name="bottomsection"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topfeedback'); iOver('bottomfeedback'); self.status=feedbackblurb; return true;" onMouseOut="iOut('topfeedback'); iOut('bottomfeedback'); self.status=''; return true;" href="j-sec1-10-3.html"><img alt="给出此教程的反馈意见" border="0" src="../i/feedback.gif" name="bottomfeedback"></a></TD><TD width="100%" background="../i/sw-gold.gif"><img alt="" src="../i/c.gif"></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topprevious'); iOver('bottomprevious'); self.status=previousblurb; return true;" onMouseOut="iOut('topprevious'); iOut('bottomprevious'); self.status=''; return true;" href="j-sec1-6-5.html"><img alt="上页" border="0" src="../i/previous.gif" name="bottomprevious"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topnext'); iOver('bottomnext'); self.status=nextblurb; return true;" onMouseOut="iOut('topnext'); iOut('bottomnext'); self.status=''; return true;" href="j-sec1-6-7.html"><img alt="下页" border="0" src="../i/next.gif" name="bottomnext"></a></TD>
</TR>
<TR>
<TD width="150" height="1" bgcolor="#000000" colspan="6"><IMG alt="" height="1" width="150" src="../i/c.gif"></TD>
</TR>
</TABLE>
<TABLE width="100%" cellpadding="0" cellspacing="0" border="0">
<TR>
<TD width="100%">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td><img alt="" height="1" width="1" src="../i/c.gif"></td>
</tr>
<tr valign="top">
<td class="bbg" height="21"> <a class="mainlink" href="/developerWorks/cn/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/ibm/index.shtml">关于 IBM</a><span class="divider"> | </span><a class="mainlink" href="/developerWorks/cn/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/ibm/privacy/index.shtml">隐私条约</a><span class="divider"> | </span><a class="mainlink" href="/developerWorks/cn/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/ibm/legal/index.shtml">法律条款</a><span class="divider"> | </span><a class="mainlink" href="/developerWorks/cn/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/ibm/contact/index.shtml">联系 IBM</a></td>
</tr>
</table>
</TD>
</TR>
</TABLE>
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -