??xml version="1.0" encoding="utf-8" standalone="yes"?>男男激情在线,中文字幕视频在线观看,国产一区二区成人http://www.aygfsteel.com/wuxufeng8080/category/12855.htmlzh-cnFri, 02 Mar 2007 02:04:42 GMTFri, 02 Mar 2007 02:04:42 GMT60Java加密和数字签?/title><link>http://www.aygfsteel.com/wuxufeng8080/articles/78466.html</link><dc:creator>风h?/dc:creator><author>风h?/author><pubDate>Wed, 01 Nov 2006 06:07:00 GMT</pubDate><guid>http://www.aygfsteel.com/wuxufeng8080/articles/78466.html</guid><description><![CDATA[ <a> <span id="wmqeeuq" class="f14">本文主要谈一下密码学中的加密和数字签名,以及其在java中如何进行用。对密码学有兴趣的伙_推荐?Bruce Schneier的著作:Applied Crypotography。在jdk1.5的发行版本中安全性方面有了很大的改进Q也提供了对RSA法的直接支持,现在我们从实例入手解决问题(本文仅是作ؓ单介l)Q?<br /><br />  <strong>一、密码学上常用的概念</strong> <br /><br />  1Q消息摘要:<br /><br />  q是一U与消息认证码结合用以保消息完整性的技术。主要用单向散列函数算法,可用于检验消息的完整性,和通过散列密码直接以文本Ş式保存等Q目前广泛用的法有MD4、MD5、SHA-1Qjdk1.5对上面都提供了支持,在java中进行消息摘要很单, java.security.MessageDigest提供了一个简易的操作ҎQ?br /><br /><div style="BORDER-RIGHT: rgb(204,204,204) 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: rgb(204,204,204) 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: rgb(204,204,204) 1px solid; WIDTH: 98%; PADDING-TOP: 4px; BORDER-BOTTOM: rgb(204,204,204) 1px solid; BACKGROUND-COLOR: rgb(238,238,238)"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="COLOR: rgb(0,128,0)">/**</span><span style="COLOR: rgb(0,128,0)"><br />*MessageDigestExample.java<br />*Copyright 2005-2-16<br /></span><span style="COLOR: rgb(0,128,0)">*/</span><span style="COLOR: rgb(0,0,0)"><br /></span><span style="COLOR: rgb(0,0,255)">import</span><span style="COLOR: rgb(0,0,0)"> java.security.MessageDigest;<br /></span><span style="COLOR: rgb(0,128,0)">/**</span><span style="COLOR: rgb(0,128,0)"><br />*单一的消息摘要算法,不用密?可以用来Ҏ文消息(如:密码Q隐藏保?br /></span><span style="COLOR: rgb(0,128,0)">*/</span><span style="COLOR: rgb(0,0,0)"><br /></span><span style="COLOR: rgb(0,0,255)">public</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,255)">class</span><span style="COLOR: rgb(0,0,0)"> MessageDigestExample{<br /> </span><span style="COLOR: rgb(0,0,255)">public</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,255)">static</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,255)">void</span><span style="COLOR: rgb(0,0,0)"> main(String[] args) </span><span style="COLOR: rgb(0,0,255)">throws</span><span style="COLOR: rgb(0,0,0)"> Exception{<br />  </span><span style="COLOR: rgb(0,0,255)">if</span><span style="COLOR: rgb(0,0,0)">(args.length</span><span style="COLOR: rgb(0,0,0)">!=</span><span style="COLOR: rgb(0,0,0)">1</span><span style="COLOR: rgb(0,0,0)">){<br />   System.err.println(</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">Usage:java MessageDigestExample text</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">);<br />   System.exit(</span><span style="COLOR: rgb(0,0,0)">1</span><span style="COLOR: rgb(0,0,0)">);<br />  }<br /><br />  </span><span style="COLOR: rgb(0,0,255)">byte</span><span style="COLOR: rgb(0,0,0)">[] plainText</span><span style="COLOR: rgb(0,0,0)">=</span><span style="COLOR: rgb(0,0,0)">args[</span><span style="COLOR: rgb(0,0,0)">0</span><span style="COLOR: rgb(0,0,0)">].getBytes(</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">UTF8</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">);<br /><br />  </span><span style="COLOR: rgb(0,128,0)">//</span><span style="COLOR: rgb(0,128,0)">使用getInstance("法")来获得消息摘?q里使用SHA-1?60位算?/span><span style="COLOR: rgb(0,128,0)"><br /></span><span style="COLOR: rgb(0,0,0)">  MessageDigest messageDigest</span><span style="COLOR: rgb(0,0,0)">=</span><span style="COLOR: rgb(0,0,0)">MessageDigest.getInstance(</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">SHA-1</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">);<br /><br />  System.out.println(</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">\n</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">+</span><span style="COLOR: rgb(0,0,0)">messageDigest.getProvider().getInfo());<br />  </span><span style="COLOR: rgb(0,128,0)">//</span><span style="COLOR: rgb(0,128,0)">开始用算?/span><span style="COLOR: rgb(0,128,0)"><br /></span><span style="COLOR: rgb(0,0,0)">  messageDigest.update(plainText);<br />  System.out.println(</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">\nDigest:</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">);<br />  </span><span style="COLOR: rgb(0,128,0)">//</span><span style="COLOR: rgb(0,128,0)">输出法q算l果</span><span style="COLOR: rgb(0,128,0)"><br /></span><span style="COLOR: rgb(0,0,0)">  System.out.println(</span><span style="COLOR: rgb(0,0,255)">new</span><span style="COLOR: rgb(0,0,0)"> String(messageDigest.digest(),</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">UTF8</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">));<br /> }<br />}</span></div><br />  q可以通过消息认证码来q行加密实现Qjavax.crypto.Mac提供了一个解x案,有兴者可以参考相关API文档Q本文只是简单介l什么是摘要法?br /><br />q里补充另一个运用消息摘要的方式加密的例?<br /><div style="BORDER-RIGHT: rgb(204,204,204) 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: rgb(204,204,204) 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: rgb(204,204,204) 1px solid; WIDTH: 98%; PADDING-TOP: 4px; BORDER-BOTTOM: rgb(204,204,204) 1px solid; BACKGROUND-COLOR: rgb(238,238,238)"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="COLOR: rgb(0,0,255)">public</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,255)">class</span><span style="COLOR: rgb(0,0,0)"> TestEncrypt {<br /><br />    </span><span style="COLOR: rgb(0,0,255)">public</span><span style="COLOR: rgb(0,0,0)"> TestEncrypt() {<br />    }<br /><br />    </span><span style="COLOR: rgb(0,128,0)">/**</span><span style="COLOR: rgb(0,128,0)"><br />     * </span><span style="COLOR: rgb(128,128,128)">@param</span><span style="COLOR: rgb(0,128,0)"> strSrc  :strSrc is a string will be encrypted,<br />     * </span><span style="COLOR: rgb(128,128,128)">@param</span><span style="COLOR: rgb(0,128,0)"> encName : encName is the algorithm name will be used.<br />     *                encName dafault to "MD5"<br />     * </span><span style="COLOR: rgb(128,128,128)">@return</span><span style="COLOR: rgb(0,128,0)"> String<br />     </span><span style="COLOR: rgb(0,128,0)">*/</span><span style="COLOR: rgb(0,0,0)"><br />    </span><span style="COLOR: rgb(0,0,255)">public</span><span style="COLOR: rgb(0,0,0)"> String Encrypt(String strSrc, String encName) {<br /><br />        MessageDigest md </span><span style="COLOR: rgb(0,0,0)">=</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,255)">null</span><span style="COLOR: rgb(0,0,0)">;<br />        String strDes </span><span style="COLOR: rgb(0,0,0)">=</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,255)">null</span><span style="COLOR: rgb(0,0,0)">;<br /><br />        </span><span style="COLOR: rgb(0,0,255)">byte</span><span style="COLOR: rgb(0,0,0)">[] bt </span><span style="COLOR: rgb(0,0,0)">=</span><span style="COLOR: rgb(0,0,0)"> strSrc.getBytes();<br />        </span><span style="COLOR: rgb(0,0,255)">try</span><span style="COLOR: rgb(0,0,0)"> {<br />            </span><span style="COLOR: rgb(0,0,255)">if</span><span style="COLOR: rgb(0,0,0)"> (encName </span><span style="COLOR: rgb(0,0,0)">==</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,255)">null</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,0)">||</span><span style="COLOR: rgb(0,0,0)"> encName.equals(</span><span style="COLOR: rgb(0,0,0)">""</span><span style="COLOR: rgb(0,0,0)">)) {<br />                encName </span><span style="COLOR: rgb(0,0,0)">=</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">MD5</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">;<br />            }<br />            md </span><span style="COLOR: rgb(0,0,0)">=</span><span style="COLOR: rgb(0,0,0)"> MessageDigest.getInstance(encName);<br />            md.update(bt);<br />            strDes </span><span style="COLOR: rgb(0,0,0)">=</span><span style="COLOR: rgb(0,0,0)"> bytes2Hex(md.digest()); </span><span style="COLOR: rgb(0,128,0)">//</span><span style="COLOR: rgb(0,128,0)">to HexString</span><span style="COLOR: rgb(0,128,0)"><br /></span><span style="COLOR: rgb(0,0,0)">        }<br />        </span><span style="COLOR: rgb(0,0,255)">catch</span><span style="COLOR: rgb(0,0,0)"> (NoSuchAlgorithmException e) {<br />            System.out.println(</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">Invalid algorithm.</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">);<br />            </span><span style="COLOR: rgb(0,0,255)">return</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,255)">null</span><span style="COLOR: rgb(0,0,0)">;<br />        }<br />        </span><span style="COLOR: rgb(0,0,255)">return</span><span style="COLOR: rgb(0,0,0)"> strDes;<br />    }<br /><br />    </span><span style="COLOR: rgb(0,0,255)">public</span><span style="COLOR: rgb(0,0,0)"> String bytes2Hex(</span><span style="COLOR: rgb(0,0,255)">byte</span><span style="COLOR: rgb(0,0,0)">[] bts) {<br />        String des </span><span style="COLOR: rgb(0,0,0)">=</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,0)">""</span><span style="COLOR: rgb(0,0,0)">;<br />        String tmp </span><span style="COLOR: rgb(0,0,0)">=</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,255)">null</span><span style="COLOR: rgb(0,0,0)">;<br />        </span><span style="COLOR: rgb(0,0,255)">for</span><span style="COLOR: rgb(0,0,0)"> (</span><span style="COLOR: rgb(0,0,255)">int</span><span style="COLOR: rgb(0,0,0)"> i </span><span style="COLOR: rgb(0,0,0)">=</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,0)">0</span><span style="COLOR: rgb(0,0,0)">; i </span><span style="COLOR: rgb(0,0,0)"><</span><span style="COLOR: rgb(0,0,0)"> bts.length; i</span><span style="COLOR: rgb(0,0,0)">++</span><span style="COLOR: rgb(0,0,0)">) {<br />            tmp </span><span style="COLOR: rgb(0,0,0)">=</span><span style="COLOR: rgb(0,0,0)"> (Integer.toHexString(bts[i] </span><span style="COLOR: rgb(0,0,0)">&</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,0)">0xFF</span><span style="COLOR: rgb(0,0,0)">));<br />            </span><span style="COLOR: rgb(0,0,255)">if</span><span style="COLOR: rgb(0,0,0)"> (tmp.length() </span><span style="COLOR: rgb(0,0,0)">==</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,0)">1</span><span style="COLOR: rgb(0,0,0)">) {<br />                des </span><span style="COLOR: rgb(0,0,0)">+=</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">0</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">;<br />            }<br />            des </span><span style="COLOR: rgb(0,0,0)">+=</span><span style="COLOR: rgb(0,0,0)"> tmp;<br />        }<br />        </span><span style="COLOR: rgb(0,0,255)">return</span><span style="COLOR: rgb(0,0,0)"> des;<br />    }<br /><br />    </span><span style="COLOR: rgb(0,0,255)">public</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,255)">static</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,255)">void</span><span style="COLOR: rgb(0,0,0)"> main(String[]args) {<br />        TestEncrypt te </span><span style="COLOR: rgb(0,0,0)">=</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,255)">new</span><span style="COLOR: rgb(0,0,0)"> TestEncrypt();<br />        String strSrc </span><span style="COLOR: rgb(0,0,0)">=</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">可以加密汉字.Oh,and english</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">;<br />        System.out.println(</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">Source String:</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,0)">+</span><span style="COLOR: rgb(0,0,0)"> strSrc);<br />        System.out.println(</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">Encrypted String:</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">);<br />        System.out.println(</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">Use Def:</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,0)">+</span><span style="COLOR: rgb(0,0,0)"> te.Encrypt(strSrc, </span><span style="COLOR: rgb(0,0,255)">null</span><span style="COLOR: rgb(0,0,0)">));<br />        System.out.println(</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">Use MD5:</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,0)">+</span><span style="COLOR: rgb(0,0,0)"> te.Encrypt(strSrc, </span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">MD5</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">));<br />        System.out.println(</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">Use SHA:</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,0)">+</span><span style="COLOR: rgb(0,0,0)"> te.Encrypt(strSrc, </span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">SHA-1</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">));<br />        System.out.println(</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">Use SHA-256:</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,0)">+</span><span style="COLOR: rgb(0,0,0)"> te.Encrypt(strSrc, </span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">SHA-256</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">));<br />    }<br />}</span></div><br />另外Q在javawebparts中的 RequestHelpers里的generateGUIDҎ也涉及到了MD5的方法,代码如下:<br /><div style="BORDER-RIGHT: rgb(204,204,204) 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: rgb(204,204,204) 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: rgb(204,204,204) 1px solid; WIDTH: 98%; PADDING-TOP: 4px; BORDER-BOTTOM: rgb(204,204,204) 1px solid; BACKGROUND-COLOR: rgb(238,238,238)"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="COLOR: rgb(0,0,255)">public</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,255)">static</span><span style="COLOR: rgb(0,0,0)"> String generateGUID(HttpServletRequest request) {<br /><br />    String out </span><span style="COLOR: rgb(0,0,0)">=</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,0)">""</span><span style="COLOR: rgb(0,0,0)">;<br />    </span><span style="COLOR: rgb(0,0,255)">try</span><span style="COLOR: rgb(0,0,0)"> {<br />      </span><span style="COLOR: rgb(0,128,0)">//</span><span style="COLOR: rgb(0,128,0)"> Construct a string that is comprised of:<br />      </span><span style="COLOR: rgb(0,128,0)">//</span><span style="COLOR: rgb(0,128,0)"> Remote IP Address + Host IP Address + Date (yyyyMMdd) +<br />      </span><span style="COLOR: rgb(0,128,0)">//</span><span style="COLOR: rgb(0,128,0)"> Time (hhmmssSSa) + Requested Path + Session ID +<br />      </span><span style="COLOR: rgb(0,128,0)">//</span><span style="COLOR: rgb(0,128,0)"> HashCode Of ParameterMap</span><span style="COLOR: rgb(0,128,0)"><br /></span><span style="COLOR: rgb(0,0,0)">      StringBuffer sb </span><span style="COLOR: rgb(0,0,0)">=</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,255)">new</span><span style="COLOR: rgb(0,0,0)"> StringBuffer(</span><span style="COLOR: rgb(0,0,0)">1024</span><span style="COLOR: rgb(0,0,0)">);<br />      sb.append(request.getRemoteAddr());<br />      InetAddress ia </span><span style="COLOR: rgb(0,0,0)">=</span><span style="COLOR: rgb(0,0,0)"> InetAddress.getLocalHost();<br />      sb.append(ia.getHostAddress());<br />      sb.append(</span><span style="COLOR: rgb(0,0,255)">new</span><span style="COLOR: rgb(0,0,0)"> SimpleDateFormat(</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">yyyyMMddhhmmssSSa</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">).format(</span><span style="COLOR: rgb(0,0,255)">new</span><span style="COLOR: rgb(0,0,0)"> Date()));<br />      String path </span><span style="COLOR: rgb(0,0,0)">=</span><span style="COLOR: rgb(0,0,0)"> request.getServletPath();<br />      String pathInfo </span><span style="COLOR: rgb(0,0,0)">=</span><span style="COLOR: rgb(0,0,0)"> request.getPathInfo();<br />      </span><span style="COLOR: rgb(0,0,255)">if</span><span style="COLOR: rgb(0,0,0)"> (pathInfo </span><span style="COLOR: rgb(0,0,0)">!=</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,255)">null</span><span style="COLOR: rgb(0,0,0)">) {<br />        path </span><span style="COLOR: rgb(0,0,0)">+=</span><span style="COLOR: rgb(0,0,0)"> pathInfo;<br />      }<br />      sb.append(path);<br />      sb.append(request.getSession(</span><span style="COLOR: rgb(0,0,255)">false</span><span style="COLOR: rgb(0,0,0)">));<br />      sb.append(request.getParameterMap().hashCode());<br />      String str </span><span style="COLOR: rgb(0,0,0)">=</span><span style="COLOR: rgb(0,0,0)"> sb.toString();<br />      </span><span style="COLOR: rgb(0,128,0)">//</span><span style="COLOR: rgb(0,128,0)"> Now encode the string using an MD5 encryption algorithm.</span><span style="COLOR: rgb(0,128,0)"><br /></span><span style="COLOR: rgb(0,0,0)">      MessageDigest md </span><span style="COLOR: rgb(0,0,0)">=</span><span style="COLOR: rgb(0,0,0)"> MessageDigest.getInstance(</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">md5</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">);<br />      md.update(str.getBytes());<br />      </span><span style="COLOR: rgb(0,0,255)">byte</span><span style="COLOR: rgb(0,0,0)">[] digest </span><span style="COLOR: rgb(0,0,0)">=</span><span style="COLOR: rgb(0,0,0)"> md.digest();<br />      StringBuffer hexStr </span><span style="COLOR: rgb(0,0,0)">=</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,255)">new</span><span style="COLOR: rgb(0,0,0)"> StringBuffer(</span><span style="COLOR: rgb(0,0,0)">1024</span><span style="COLOR: rgb(0,0,0)">);<br />      </span><span style="COLOR: rgb(0,0,255)">for</span><span style="COLOR: rgb(0,0,0)"> (</span><span style="COLOR: rgb(0,0,255)">int</span><span style="COLOR: rgb(0,0,0)"> i </span><span style="COLOR: rgb(0,0,0)">=</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,0)">0</span><span style="COLOR: rgb(0,0,0)">; i </span><span style="COLOR: rgb(0,0,0)"><</span><span style="COLOR: rgb(0,0,0)"> digest.length; i</span><span style="COLOR: rgb(0,0,0)">++</span><span style="COLOR: rgb(0,0,0)">) {<br />        str </span><span style="COLOR: rgb(0,0,0)">=</span><span style="COLOR: rgb(0,0,0)"> Integer.toHexString(</span><span style="COLOR: rgb(0,0,0)">0xFF</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,0)">&</span><span style="COLOR: rgb(0,0,0)"> digest[i]);<br />        </span><span style="COLOR: rgb(0,0,255)">if</span><span style="COLOR: rgb(0,0,0)"> (str.length() </span><span style="COLOR: rgb(0,0,0)"><</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,0)">2</span><span style="COLOR: rgb(0,0,0)">) {<br />          str </span><span style="COLOR: rgb(0,0,0)">=</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">0</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,0)">+</span><span style="COLOR: rgb(0,0,0)"> str;<br />        }<br />        hexStr.append(str);<br />      }<br />      out </span><span style="COLOR: rgb(0,0,0)">=</span><span style="COLOR: rgb(0,0,0)"> hexStr.toString();<br />    } </span><span style="COLOR: rgb(0,0,255)">catch</span><span style="COLOR: rgb(0,0,0)"> (NoSuchAlgorithmException nsae) {<br />      log.error(nsae);<br />    } </span><span style="COLOR: rgb(0,0,255)">catch</span><span style="COLOR: rgb(0,0,0)"> (UnknownHostException uhe) {<br />      log.error(uhe);<br />    }<br />    </span><span style="COLOR: rgb(0,128,0)">//</span><span style="COLOR: rgb(0,128,0)"> Return the encrypted string.  It should be unique based on the<br />    </span><span style="COLOR: rgb(0,128,0)">//</span><span style="COLOR: rgb(0,128,0)"> components that comprise the plain text string, and should always be<br />    </span><span style="COLOR: rgb(0,128,0)">//</span><span style="COLOR: rgb(0,128,0)"> 32 characters thanks to the MD5 algorithm.</span><span style="COLOR: rgb(0,128,0)"><br /></span><span style="COLOR: rgb(0,0,0)">    </span><span style="COLOR: rgb(0,0,255)">return</span><span style="COLOR: rgb(0,0,0)"> out;<br /><br />  } </span><span style="COLOR: rgb(0,128,0)">//</span><span style="COLOR: rgb(0,128,0)"> End generateGUID().</span></div><br /><br />  2Q私钥加密:<br /><br />  消息摘要只能查消息的完整性,但是单向的,Ҏ文消息ƈ不能加密Q要加密明文的消息的话,p使用其他的算法,要确保机密性,我们需要用私钥密码术来交换私有消息?br /><br />  q种最好理解,使用对称法。比如:A用一个密钥对一个文件加密,而Bdq个文g的话Q则需要和A一L密钥Q双方共享一个私钥(而在web环境下,U钥在传递时Ҏ被侦听)Q?br /><br />  使用U钥加密的话Q首先需要一个密钥,可用javax.crypto.KeyGenerator产生一个密?java.security.Key), 然后传递给一个加密工?javax.crypto.Cipher),该工具再使用相应的算法来q行加密Q主要对U算法有QDESQ实际密钥只用到56 位)QAESQ支持三U密钥长度:128?92?56位)Q通常首先128位,其他的还有DESede{,jdk1.5U也提供了对对称法的支持,以下例子使用AES法来加密:<br /><br /><div style="BORDER-RIGHT: rgb(204,204,204) 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: rgb(204,204,204) 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: rgb(204,204,204) 1px solid; WIDTH: 98%; PADDING-TOP: 4px; BORDER-BOTTOM: rgb(204,204,204) 1px solid; BACKGROUND-COLOR: rgb(238,238,238)"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="COLOR: rgb(0,128,0)">/**</span><span style="COLOR: rgb(0,128,0)"><br />*PrivateExmaple.java<br />*Copyright 2005-2-16<br /></span><span style="COLOR: rgb(0,128,0)">*/</span><span style="COLOR: rgb(0,0,0)"><br /></span><span style="COLOR: rgb(0,0,255)">import</span><span style="COLOR: rgb(0,0,0)"> javax.crypto.Cipher;<br /></span><span style="COLOR: rgb(0,0,255)">import</span><span style="COLOR: rgb(0,0,0)"> javax.crypto.KeyGenerator;<br /></span><span style="COLOR: rgb(0,0,255)">import</span><span style="COLOR: rgb(0,0,0)"> java.security.Key;<br /><br /></span><span style="COLOR: rgb(0,128,0)">/**</span><span style="COLOR: rgb(0,128,0)"><br />*U鈅加密Q保证消息机密?br /></span><span style="COLOR: rgb(0,128,0)">*/</span><span style="COLOR: rgb(0,0,0)"><br /></span><span style="COLOR: rgb(0,0,255)">public</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,255)">class</span><span style="COLOR: rgb(0,0,0)"> PrivateExample{<br /> </span><span style="COLOR: rgb(0,0,255)">public</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,255)">static</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,255)">void</span><span style="COLOR: rgb(0,0,0)"> main(String[] args) </span><span style="COLOR: rgb(0,0,255)">throws</span><span style="COLOR: rgb(0,0,0)"> Exception{<br />  </span><span style="COLOR: rgb(0,0,255)">if</span><span style="COLOR: rgb(0,0,0)">(args.length</span><span style="COLOR: rgb(0,0,0)">!=</span><span style="COLOR: rgb(0,0,0)">1</span><span style="COLOR: rgb(0,0,0)">){<br />   System.err.println(</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">Usage:java PrivateExample <text></span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">);<br />   System.exit(</span><span style="COLOR: rgb(0,0,0)">1</span><span style="COLOR: rgb(0,0,0)">);<br />  }<br />  </span><span style="COLOR: rgb(0,0,255)">byte</span><span style="COLOR: rgb(0,0,0)">[] plainText</span><span style="COLOR: rgb(0,0,0)">=</span><span style="COLOR: rgb(0,0,0)">args[</span><span style="COLOR: rgb(0,0,0)">0</span><span style="COLOR: rgb(0,0,0)">].getBytes(</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">UTF8</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">);<br /><br />  </span><span style="COLOR: rgb(0,128,0)">//</span><span style="COLOR: rgb(0,128,0)">通过KeyGenerator形成一个key</span><span style="COLOR: rgb(0,128,0)"><br /></span><span style="COLOR: rgb(0,0,0)">  System.out.println(</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">\nStart generate AES key</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">);<br />  KeyGenerator keyGen</span><span style="COLOR: rgb(0,0,0)">=</span><span style="COLOR: rgb(0,0,0)">KeyGenerator.getInstance(</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">AES</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">);<br />  keyGen.init(</span><span style="COLOR: rgb(0,0,0)">128</span><span style="COLOR: rgb(0,0,0)">);<br />  Key key</span><span style="COLOR: rgb(0,0,0)">=</span><span style="COLOR: rgb(0,0,0)">keyGen.generateKey();<br />  System.out.println(</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">Finish generating DES key</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">);<br /><br />  </span><span style="COLOR: rgb(0,128,0)">//</span><span style="COLOR: rgb(0,128,0)">获得一个私鈅加密类CipherQECB是加密方式,PKCS5Padding是填充方?/span><span style="COLOR: rgb(0,128,0)"><br /></span><span style="COLOR: rgb(0,0,0)">  Cipher cipher</span><span style="COLOR: rgb(0,0,0)">=</span><span style="COLOR: rgb(0,0,0)">Cipher.getInstance(</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">AES/ECB/PKCS5Padding</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">);<br />  System.out.println(</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">\n</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">+</span><span style="COLOR: rgb(0,0,0)">cipher.getProvider().getInfo());<br /><br />  </span><span style="COLOR: rgb(0,128,0)">//</span><span style="COLOR: rgb(0,128,0)">使用U鈅加密</span><span style="COLOR: rgb(0,128,0)"><br /></span><span style="COLOR: rgb(0,0,0)">  System.out.println(</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">\nStart encryption:</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">);<br />  cipher.init(Cipher.ENCRYPT_MODE,key);<br />  </span><span style="COLOR: rgb(0,0,255)">byte</span><span style="COLOR: rgb(0,0,0)">[] cipherText</span><span style="COLOR: rgb(0,0,0)">=</span><span style="COLOR: rgb(0,0,0)">cipher.doFinal(plainText);<br />  System.out.println(</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">Finish encryption:</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">);<br />  System.out.println(</span><span style="COLOR: rgb(0,0,255)">new</span><span style="COLOR: rgb(0,0,0)"> String(cipherText,</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">UTF8</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">));<br /><br />  System.out.println(</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">\nStart decryption:</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">);<br />  cipher.init(Cipher.DECRYPT_MODE,key);<br />  </span><span style="COLOR: rgb(0,0,255)">byte</span><span style="COLOR: rgb(0,0,0)">[] newPlainText</span><span style="COLOR: rgb(0,0,0)">=</span><span style="COLOR: rgb(0,0,0)">cipher.doFinal(cipherText);<br />  System.out.println(</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">Finish decryption:</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">);<br /><br />  System.out.println(</span><span style="COLOR: rgb(0,0,255)">new</span><span style="COLOR: rgb(0,0,0)"> String(newPlainText,</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">UTF8</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">));<br /><br /> }<br />}</span></div><br />  3Q公钥加密:<br /><br />  上面提到Q私钥加密需要一个共享的密钥Q那么如何传递密钥呢Qweb环境下,直接传递的话很Ҏ被侦听到Q幸好有了公钥加密的出现。公钥加密也叫不对称加密Q不对称法使用一对密钥对Q一个公钥,一个私钥,使用公钥加密的数据,只有U钥能解开Q可用于加密Q;同时Q用私钥加密的数据Q只有公钥能解开Q签名)。但是速度很慢Q比U钥加密?00?000倍)Q公钥的主要法有RSAQ还包括Blowfish,Diffie-Helman{, jdk1.5U提供了对RSA的支持,是一个改q的地方Q?<br /><br /><div style="BORDER-RIGHT: rgb(204,204,204) 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: rgb(204,204,204) 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: rgb(204,204,204) 1px solid; WIDTH: 98%; PADDING-TOP: 4px; BORDER-BOTTOM: rgb(204,204,204) 1px solid; BACKGROUND-COLOR: rgb(238,238,238)"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="COLOR: rgb(0,128,0)">/**</span><span style="COLOR: rgb(0,128,0)"><br />*PublicExample.java<br />*Copyright 2005-2-16<br /></span><span style="COLOR: rgb(0,128,0)">*/</span><span style="COLOR: rgb(0,0,0)"><br /></span><span style="COLOR: rgb(0,0,255)">import</span><span style="COLOR: rgb(0,0,0)"> java.security.Key;<br /></span><span style="COLOR: rgb(0,0,255)">import</span><span style="COLOR: rgb(0,0,0)"> javax.crypto.Cipher;<br /></span><span style="COLOR: rgb(0,0,255)">import</span><span style="COLOR: rgb(0,0,0)"> java.security.KeyPairGenerator;<br /></span><span style="COLOR: rgb(0,0,255)">import</span><span style="COLOR: rgb(0,0,0)"> java.security.KeyPair;<br /></span><span style="COLOR: rgb(0,128,0)">/**</span><span style="COLOR: rgb(0,128,0)"><br />*一个简单的公鈅加密例子,CiphercM用KeyPairGenerator生成的公鈅和U鈅<br /></span><span style="COLOR: rgb(0,128,0)">*/</span><span style="COLOR: rgb(0,0,0)"><br /></span><span style="COLOR: rgb(0,0,255)">public</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,255)">class</span><span style="COLOR: rgb(0,0,0)"> PublicExample{<br /> </span><span style="COLOR: rgb(0,0,255)">public</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,255)">static</span><span style="COLOR: rgb(0,0,0)"> </span><span style="COLOR: rgb(0,0,255)">void</span><span style="COLOR: rgb(0,0,0)"> main(String[] args) </span><span style="COLOR: rgb(0,0,255)">throws</span><span style="COLOR: rgb(0,0,0)"> Exception{<br />  </span><span style="COLOR: rgb(0,0,255)">if</span><span style="COLOR: rgb(0,0,0)">(args.length</span><span style="COLOR: rgb(0,0,0)">!=</span><span style="COLOR: rgb(0,0,0)">1</span><span style="COLOR: rgb(0,0,0)">){<br />   System.err.println(</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">Usage:java PublicExample <text></span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">);<br />   System.exit(</span><span style="COLOR: rgb(0,0,0)">1</span><span style="COLOR: rgb(0,0,0)">);<br />  }<br /><br />  </span><span style="COLOR: rgb(0,0,255)">byte</span><span style="COLOR: rgb(0,0,0)">[] plainText</span><span style="COLOR: rgb(0,0,0)">=</span><span style="COLOR: rgb(0,0,0)">args[</span><span style="COLOR: rgb(0,0,0)">0</span><span style="COLOR: rgb(0,0,0)">].getBytes(</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">UTF8</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">);<br />  </span><span style="COLOR: rgb(0,128,0)">//</span><span style="COLOR: rgb(0,128,0)">构成一个RSA密钥</span><span style="COLOR: rgb(0,128,0)"><br /></span><span style="COLOR: rgb(0,0,0)">  System.out.println(</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">\nStart generating RSA key</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">);<br />  KeyPairGenerator keyGen</span><span style="COLOR: rgb(0,0,0)">=</span><span style="COLOR: rgb(0,0,0)">KeyPairGenerator.getInstance(</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">RSA</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">);<br />  keyGen.initialize(</span><span style="COLOR: rgb(0,0,0)">1024</span><span style="COLOR: rgb(0,0,0)">);<br />  KeyPair key</span><span style="COLOR: rgb(0,0,0)">=</span><span style="COLOR: rgb(0,0,0)">keyGen.generateKeyPair();<br />  System.out.println(</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">Finish generating RSA key</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">);<br /><br />  </span><span style="COLOR: rgb(0,128,0)">//</span><span style="COLOR: rgb(0,128,0)">获得一个RSA的Cipherc,使用公鈅加密</span><span style="COLOR: rgb(0,128,0)"><br /></span><span style="COLOR: rgb(0,0,0)">  Cipher cipher</span><span style="COLOR: rgb(0,0,0)">=</span><span style="COLOR: rgb(0,0,0)">Cipher.getInstance(</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">RSA/ECB/PKCS1Padding</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">);<br />  System.out.println(</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">\n</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">+</span><span style="COLOR: rgb(0,0,0)">cipher.getProvider().getInfo());<br /><br />  System.out.println(</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">\nStart encryption</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">);<br />  cipher.init(Cipher.ENCRYPT_MODE,key.getPublic());<br />  </span><span style="COLOR: rgb(0,0,255)">byte</span><span style="COLOR: rgb(0,0,0)">[] cipherText</span><span style="COLOR: rgb(0,0,0)">=</span><span style="COLOR: rgb(0,0,0)">cipher.doFinal(plainText);<br />  System.out.println(</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">Finish encryption:</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">);<br />  System.out.println(</span><span style="COLOR: rgb(0,0,255)">new</span><span style="COLOR: rgb(0,0,0)"> String(cipherText,</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">UTF8</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">));<br /><br />  </span><span style="COLOR: rgb(0,128,0)">//</span><span style="COLOR: rgb(0,128,0)">使用U鈅解密</span><span style="COLOR: rgb(0,128,0)"><br /></span><span style="COLOR: rgb(0,0,0)">  System.out.println(</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">\nStart decryption</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">);<br />  cipher.init(Cipher.DECRYPT_MODE,key.getPrivate());<br />  </span><span style="COLOR: rgb(0,0,255)">byte</span><span style="COLOR: rgb(0,0,0)">[] newPlainText</span><span style="COLOR: rgb(0,0,0)">=</span><span style="COLOR: rgb(0,0,0)">cipher.doFinal(cipherText);<br />  System.out.println(</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">Finish decryption:</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">);<br />  System.out.println(</span><span style="COLOR: rgb(0,0,255)">new</span><span style="COLOR: rgb(0,0,0)"> String(newPlainText,</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">UTF8</span><span style="COLOR: rgb(0,0,0)">"</span><span style="COLOR: rgb(0,0,0)">));<br /> }<br />}</span></div></span> </a> <img src ="http://www.aygfsteel.com/wuxufeng8080/aggbug/78466.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/wuxufeng8080/" target="_blank">风h?/a> 2006-11-01 14:07 <a href="http://www.aygfsteel.com/wuxufeng8080/articles/78466.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>BMP、GIF和JPEG文g格式?/title><link>http://www.aygfsteel.com/wuxufeng8080/articles/57060.html</link><dc:creator>风h?/dc:creator><author>风h?/author><pubDate>Fri, 07 Jul 2006 00:10:00 GMT</pubDate><guid>http://www.aygfsteel.com/wuxufeng8080/articles/57060.html</guid><description><![CDATA[ <div id="wmqeeuq" class="postcontent"> <span id="wmqeeuq" class="line">  图象文g是描绘了一q图象的计算机磁盘文件。Ş成数字图象数据后Q将其存储在计算机里的方法有2U,即位映射和向量处理方式?br />  我们q里主要讨论的是位图。不同图象Y件几乎都用各U的Ҏ处理图象Q图象格式也多种多样Q它主要包括文g识别头和图象数据。文件识别头用来让计机判断是哪U文件格式,图象数据包含了整个对图象描绘相关数据Q包括调色板、位图映象等。根据压~算法不同,映象方式也不同,下面要介l一下压~算法?</span> <p> <span id="wmqeeuq" class="line"> <b> <font color="#5288c7">一?行程长度压羃</font> </b> <br />  原理是将一扫描行中的颜色值相同的盔R像素用一个计数值和那些像素的颜色值来代替。例?aaabccccccddeeeQ则可用3a1b6c2d3e来代ѝ对于拥有大面积Q相同颜色区域的囑փQ用RLE压羃Ҏ非常有效。由RLE原理z多具体行E压~方? <br />  1.PCX行程压羃Ҏ: 该算法实际上是位映射格式到压~格式的转换法Q该法对于q箋出现1ơ的字节ChQ若Ch>0xc0则压~时在该字节前加?xc1Q否则直接输出ChQ对于连l出现N ơ的字节ChQ则压羃?xc0QNQChq两个字节,因而N最大只能ؓffQc0=3fh(十进制ؓ63)Q当N大于63Ӟ 则需分多ơ压~?<br />  2.BI_RLE8压羃Ҏ:在WINDOWS的位图文件中采用了这U压~方法。该压羃Ҏ~码也是以两个字节ؓ基本单位。其中第一个字节规定了用第二个字节指定的颜色重复次数?如编?0504表示从当前位|开始连l显C?个颜色gؓ04的像素。当W二个字节ؓ零时W二个字节有Ҏ含义:0表示行末;1表示图末;2转义后面2个字节, q两个字节分别表CZ一像素相对于当前位|的水^位移和垂直位UR这U压~方法所能压~的囑փ像素位数最大ؓ8?256?囑փ?br />  3.BI_RLE压羃Ҏ: 该方法也用于WINDOWS位图文g中,它与 BI_RLE8~码cMQ唯一不同?BI_RLE4的一个字节包含了两个像素的颜Ԍ因此Q它只能压羃的颜色数不超q?6的图像。因而这U压~应用范围有限?<br />  4.紧羃位压~方?Packbits):该方法是用于Apple公司的MacintoshZ的位图数据压~?ҎQ?TIFF 规范中用了q种ҎQ?q种压羃Ҏ与BI_RLE8压羃Ҏ怼Q如1c1c1c2132325648 压羃?83 1c 21 81 32 56 48Q显而易见, q种压羃Ҏ最好情冉|每连l?28个字节相同,q?28个字节可压羃Z个数?f。这U方法还是非常有效的?</span> </p> <p> <span id="wmqeeuq" class="line"> <b> <font color="#5288c7">二、霍夫曼~码压羃:</font> </b> <br />  也是一U常用的压羃Ҏ。是1952qؓ文本文g建立的,其基本原理是频繁使用的数据用较短的代码代替,很少使用的数据用较长的代码代替,每个数据的代码各不相同。这些代码都是二q制码,且码的长度是可变的。如: 有一个原始数据序列,ABACCDAA则编码ؓA(0)QB(10)QC(110),(D111)Q压~后?10011011011100。生霍夫曼~码需要对原始数据扫描两遍Q第一遍扫描要_地统计出原始数据中的每个值出现的频率Q第二遍是徏立霍夫曼树ƈq行~码Q由于需要徏立二叉树q历二叉树生成~码Q因此数据压~和q原速度都较慢,但简单有效,因而得到广泛的应用?/span> </p> <p> <span id="wmqeeuq" class="line"> <b> <font color="#5288c7">三、LZW压羃Ҏ</font> </b> <br />  LZW压羃技术比其它大多数压~技术都复杂Q?压羃效率也较高。其基本原理是把每一个第一ơ出现的字符串用一个数值来~码Q在q原E序中再这个数D成原来的字符 Ԍ如用数?x100代替字符?abccddeee"q样每当出现该字W串Ӟ都用0x100代替QvC压羃的作用?至于0x100与字W串的对应关pd是在压羃q程中动态生成的Q而且q种对应关系是隐含在压羃数据中,随着解压~的q行q张~码表会从压~数据中逐步得到恢复Q后面的压羃数据再根据前面数据生的对应关系产生更多的对应关pR直到压~文件结束ؓ止。LZW是可逆的Q?所有信息全部保留?/span> </p> <p> <span id="wmqeeuq" class="line"> <b> <font color="#5288c7">四、算术压~方?/font> </b> <br />  术压羃与霍夫曼~码压羃ҎcMQ只不过它比霍夫曼编码更加有效。算术压~适合于由相同的重复序列组成的文gQ算术压~接q压~的理论极限。这U方法,是将不同的序列映像到0?之间的区域内Q该区域表示成可变精?位数 )的二q制数Q越不常见的数据要的_ֺ高(更多的位?Q这U方法比较复杂,因而不太常用?/span> </p> <p> <span id="wmqeeuq" class="line"> <b> <font color="#5288c7">五?JPEG( 联合摄媄专家l?Joint Photographic Exprerts Group)</font> </b> <br />  JPEG标准与其它的标准不同Q它定义了不兼容的编码方法,在它最常用的模式中Q它是带q的,一个从JPEG文g恢复出来的图像与原始囑փL不同的,但有损压~重建后的图像常常比原始囑փ的效果更好。JPEG的另一个显著的特点是它的压~比例相当高Q原囑փ大小与压~后的图像大相比,比例可以?Q到80?0Q不{?q种Ҏ?果也好,适合多媒体系l?/span> </p> <p> <span id="wmqeeuq" class="line">  介绍完了压羃法Q我们来要介l一下三U位图格式的异同和它们之间的怺转换?br />  <b>1. bmp图象</b><br />   · 位图文g?BITMAPHEADER)数据l构<br />   · 位图信息(BITMAPINFO)数据l构<br />   · 位图阵列</span> </p> <p> <span id="wmqeeuq" class="line">  1)位图文g头数据结构包含BMP图象文g的类型、显C内容等信息?br />  Typedef struct{<br />      Int bfType; //must be "BM"<br />      Long bfSize; //位图大小<br />      Int bfReserved1;//must be "0"<br />      Int bfReserved2;//must be "0"<br />      Long bfOffBits;//位图阵列的v始位|?br />  }BITMAPEFILEHEADER;</span> </p> <p> <span id="wmqeeuq" class="line">  2)位图信息数据l构由BITMAPINFOHEADER和RGBQUAD两个数据l构l成Q?br />  typedef struct{<br />      BITMAPINFOHEADER bmiHeader;<br />      RGBQUAD bmiColors[];<br />  }BITMAPINFO</span> </p> <p> <span id="wmqeeuq" class="line">  其中BITMAPINFOHEADER数据l构包含了有关BMP图象的宽、高、压~方法等信息?br />  数据l构RGBQUAD定义一U颜艌Ӏ?/span> </p> <p> <span id="wmqeeuq" class="line">  3Q位N?br />  位图阵列记录了图象的每一个象素倹{从图象的左下角开始逐行扫描图象。从左到叻I从上CQ将图象的象素值逐一记录下来Q这些记录象素值的字节l成了位N列?br />  位图阵列数据的存储格式有压羃和非压羃两种格式?br />  1Q?非压~格?位图中的每一个点的象素值对应与位图阵列的若q位Q而位N列的若干位由图象的高度、宽度及图象的颜色数军_?br />  2Q?压羃格式 在bmp格式文g中,Windows支持BI-RLE8和BI-RLE4两种压羃cd的存储格式?/span> </p> <p> <span id="wmqeeuq" class="line">  <b>2、GIF图象文g格式</b><br />  GIF的全U是Graphics Interchange Format,.译做囑Ş交换格式。GIF是一U公用的图象文g格式标准Q但它归compuServe公司版权所有?br />GIF文gl构包含一个文件头Q?/span> <span id="wmqeeuq" class="line">在一个GIF文g中首先碰到的是GIF的标志,q个标志告诉解码E序q是个GIF文g。这个标志是3字节的串QGIF。一个GIF文g中可以存攑֤q图象,但绝大多数的文g都只包含一q图象?br />  然后是屏q描q字(screen descriptor),说明了用来生成的昄文g中的图象的显C器分L率,分别表示屏幕的宽和高?br />  紧跟下来的一个字节是全局标志Q其低三位说明了卛_到的图象是多少颜色的。最高位表示是否存在一个全局色彩表?br />  背景色表C把背景|成适当的颜Ԍ实际上是一个指向全局色彩表的数?br />  Struct Global_Data{<br />    Unsigned short screen_width;<br />    Unsigned short screen_height;<br />    Unsigned char background;<br />    har tail='\0';<br />    }<br />  接下来是全局色彩表,按顺序存放所有的严肃Q每一U严肃由色彩表的一Ҏ描述Q每Ҏ3字节Q分别表C红、绿、蓝三种原色的强度。其长度有全局标志的低三位表示?br />  以后的数据都是局部的了。是些数据块的集合。下面是图象数据块的l构?br />  Struct Local_Head{<br />      Char heading=',';<br />      Unsigned short image_left;//图象的屏q上昄的v始位|?br />    Unsigned short image_top;<br />    Unsigned short image_width;<br />    Unsigned short image-height;<br />    Unsigned char local_flag;//局部标?br />    } </span> </p> <p> <span id="wmqeeuq" class="line">  局部标志和 全局标志的不同之处在于次高位Q如果这位被|成1则表C图像的位图数据是以隔行方式存放的。也是_在解开的位图数据中Q第一行存攄是屏q上W一行,W二行对应屏q上的第9行,W三行对应屏q上的第17行,以此递增--q是W一遍扫描;W二遍扫描是从屏q上的第5行开始,两行之间也是?递增Q第三遍扫描是从屏幕上的W?行开始,两行之间?递增Q第四遍扫描Q也是最后一遍,从第2行开始,两行之间?递增?/span> <span id="wmqeeuq" class="line">隔行存放的GIF囑փ在边解码ҎC的时候可分成四遍扫描。第一遍扫描虽然只昄了整个图像的八分之一Q第二遍的扫描后也只昄了四分之一Q但q已l把整个囑փ的概貌显C出来了。在昄GIF囑փ的时Q隔行存攄囑փ会给你这样一个印象:向它的显C速度g要比其他囑փ快一些,q是隔行存放的优炏V?br />  在GIF囑փ的编解码中用CLZW压羃法--把这些字W流转换成另一UŞ式的代码,解码q程则是把这U代码流q原成原来的字符?/span> </p> <p> <span id="wmqeeuq" class="line">  <b>3、JPEG图象文g格式</b><br />  JPEG是Joint Photographic Experts Group(联合摄媄专家组)的首字母~写。JPEG的主要作用是用于数字化图象的标准~码技术。JPEG图象文g是一U象素格式文件格式,但它比诸如象GIF、BMP{图象文件要复杂的多。所q?我们在用由JPEGl成的JPEG库时,只要对该文g格式有个一般的了解可以了。而没有必要对JPEG文g格式做一个全面细致的了解?br />  JPEG格式是种有损的编码格式,但如果GIF文g比,他经q解码后的重构图象要比GIF图象更接q于原始的图象?JPEG~码技术是由颜色{换、DCT变换、进行量化、编码。其压羃格式p名的JPEG集团所著的4.0版本的库所定?/span> </p> <p> <span id="wmqeeuq" class="line">  关于q?U图象格式的互相转换Q主要是用c语言或c++和汇~语aQ因些语a可以 直接q行底层的操作,把图象解压羃后按用另一U格式要求进行压~即可?br />本文属{载,原文地址Q?a >http://www.chinaaspx.com/comm/dotnetbbs/Showtopic.aspx?Forum_ID=44&Id=139926&PPage=1</a></span> </p> </div> <img src ="http://www.aygfsteel.com/wuxufeng8080/aggbug/57060.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/wuxufeng8080/" target="_blank">风h?/a> 2006-07-07 08:10 <a href="http://www.aygfsteel.com/wuxufeng8080/articles/57060.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss> <footer> <div class="friendship-link"> <a href="http://www.aygfsteel.com/" title="狠狠久久亚洲欧美专区_中文字幕亚洲综合久久202_国产精品亚洲第五区在线_日本免费网站视频">狠狠久久亚洲欧美专区_中文字幕亚洲综合久久202_国产精品亚洲第五区在线_日本免费网站视频</a> </div> </footer> վ֩ģ壺 <a href="http://" target="_blank">ʢ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">质</a>| <a href="http://" target="_blank">ͼ</a>| <a href="http://" target="_blank">ɽ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ͬ</a>| <a href="http://" target="_blank">̨</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ٳ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">Ϫ</a>| <a href="http://" target="_blank">פ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">̨</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">̨</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">¹Ȫ</a>| <a href="http://" target="_blank">ɽ</a>| <a href="http://" target="_blank">Ӫ</a>| <a href="http://" target="_blank">̨</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">â</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">Ʊ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ʡ</a>| <a href="http://" target="_blank">żҿ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ߺ</a>| <a href="http://" target="_blank">DZɽ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ƽ</a>| <script> (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })(); </script> </body>