﻿<?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-Topquan's Blog-文章分类-Classical Arithmetic</title><link>http://www.blogjava.net/topquan/category/23912.html</link><description>分享价值----成就你我----我的博客----你的家</description><language>zh-cn</language><lastBuildDate>Sat, 07 Jul 2007 16:32:53 GMT</lastBuildDate><pubDate>Sat, 07 Jul 2007 16:32:53 GMT</pubDate><ttl>60</ttl><item><title>Base64加密算法</title><link>http://www.blogjava.net/topquan/articles/75132.html</link><dc:creator>topquan</dc:creator><author>topquan</author><pubDate>Sat, 14 Oct 2006 08:21:00 GMT</pubDate><guid>http://www.blogjava.net/topquan/articles/75132.html</guid><wfw:comment>http://www.blogjava.net/topquan/comments/75132.html</wfw:comment><comments>http://www.blogjava.net/topquan/articles/75132.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/topquan/comments/commentRss/75132.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/topquan/services/trackbacks/75132.html</trackback:ping><description><![CDATA[<span class=content><font color=#ff0000>原理：</font> <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Base64 使用US-ASCII子集的64个字符,即大小写的26个英文字母，0－9，＋，/。<br>编码总是基于3个字符，每个字符用8位二进制表示，因此一共24位，再分为4四组，每组6位，表示一个Base64的值。如下：<br>"A", "B", "C", "D", "E", "F", "G", "H", "I","J", "K", "L", "M", "N", "O", "P",<br>"Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f",<br>"g", "h", "i","j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",<br>"w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+", "/" <br>Base64值为0就是A，为27的就是b。这样，每3个字符产生4位的Base64字符。如果被加密的字符串每3个一组，还剩1或2个字符，使用特殊字符"="补齐Base64的4字。<br><br>如，编码只有2个字符&#8220;me&#8221;，m的ascii是109，e的是101，用二进制表示分别是01101101、01100101，连接起来就是0110110101100101，再按6位分为一组：011011、010110、010100（不足6位补0），分别ascii分别是27、22、20，即Base64值为bWU，Base64不足4字，用＝补齐，因此bWU＝就me的Base64值。<br><br>用java的按位逻辑和移位运算就可以实现该算法。但实际上，并不用我们自己去编程实现。现有实现该加密解密算法的程序很多，如javamail的MimeUtility。<br><br><font color=red>实践：javamail的MimeUtility实现对字符串的加密解密</font><br><br>第一步，在eclipse新建一个java项目，并引进javamail.jar<br>第二步，在com.mascot.encrypt包下创建测试单元，注意引进javax.mail.internet.MimeUtility<br>package com.mascot.encrypt;<br><br>import java.io.BufferedReader;<br>import java.io.ByteArrayInputStream;<br>import java.io.ByteArrayOutputStream;<br>import java.io.InputStreamReader;<br><br>import javax.mail.internet.MimeUtility;<br><br>public class Base64 {<br><br>&nbsp; public static BufferedReader decode(String b64string) throws Exception {<br>&nbsp; &nbsp; return new BufferedReader(<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new InputStreamReader(<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MimeUtility.decode(<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new ByteArrayInputStream(<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; b64string.getBytes()), "base64")));<br>&nbsp; }<br>&nbsp; public static void main(String args[]) throws Exception{<br>&nbsp; &nbsp; &nbsp; System.out.println(encodeAsString("hello"));<br>&nbsp; &nbsp; &nbsp; System.out.println(decodeAsString("aGVsbG8="));<br>&nbsp; &nbsp; &nbsp; System.out.println(decodeAsString("aGVsbG8A"));<br>&nbsp; &nbsp; <br>&nbsp; }<br><br>&nbsp; public static String decodeAsString(String b64string) throws Exception {<br>&nbsp; &nbsp; if (b64string == null) {<br>&nbsp; &nbsp; &nbsp; &nbsp; return b64string;<br>&nbsp; &nbsp; }<br>&nbsp; &nbsp; String returnString = decode(b64string).readLine();<br>&nbsp; &nbsp; if (returnString == null) {<br>&nbsp; &nbsp; &nbsp; &nbsp; return returnString;<br>&nbsp; &nbsp; }<br>&nbsp; &nbsp; return returnString.trim();<br>&nbsp; }<br><br>&nbsp; public static ByteArrayOutputStream encode(String plaintext)<br>&nbsp; &nbsp; &nbsp; &nbsp; throws Exception {<br>&nbsp; &nbsp; ByteArrayOutputStream out = new ByteArrayOutputStream();<br>&nbsp; &nbsp; byte[] in = plaintext.getBytes();<br>&nbsp; &nbsp; ByteArrayOutputStream inStream = new ByteArrayOutputStream();<br>&nbsp; &nbsp; inStream.write(in, 0, in.length);<br>&nbsp; &nbsp; //补0<br>&nbsp; &nbsp; if ((in.length % 3 ) == 1){<br>&nbsp; &nbsp; &nbsp; &nbsp; inStream.write(0);<br>&nbsp; &nbsp; &nbsp; &nbsp; inStream.write(0);<br>&nbsp; &nbsp; } else if((in.length % 3 ) == 2){<br>&nbsp; &nbsp; &nbsp; &nbsp; inStream.write(0);<br>&nbsp; &nbsp; }<br>&nbsp; &nbsp; inStream.writeTo( MimeUtility.encode(out, "base64") );<br>&nbsp; &nbsp; return out;<br>&nbsp; }<br><br>&nbsp; public static String encodeAsString(String plaintext) throws Exception {<br>&nbsp; &nbsp; return encode(plaintext).toString();<br>&nbsp; }<br>}<br>第三步，运行程序，结果：<br>aGVsbG8A<br>hello<br>hello<br><br>注意到hello的加密结果并不是aGVsbG8＝，而是aGVsbG8A，这是因为程序补齐都是补0，而0对应的Base64值就是A，这是我们程序实现与上述理论不同造成的。</span> 
<img src ="http://www.blogjava.net/topquan/aggbug/75132.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/topquan/" target="_blank">topquan</a> 2006-10-14 16:21 <a href="http://www.blogjava.net/topquan/articles/75132.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>