Base64 实例 ?

   1. 什么是Base64 ?

       按照RFC2045的定义,Base64被定义为:Base64内容传送编码

 

  被设计用来把任意序列的8位字节描述为一种不易被人直接识别的形式。

 

(The Base64 Content-Transfer-Encoding is designed to represent

 

 arbitrary sequences of octets in a form that need not be humanly readable.)

  2.Base64加密和解密实例?

package org.test.md5;

import java.io.IOException;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * Base64加密和解密
 * 
@author keven.kan
 *
 
*/
public class Base64 {

    private static BASE64Encoder encoder = new BASE64Encoder();    
    
private static BASE64Decoder decoder = new BASE64Decoder();    
    
public void BASE64Coding() {}   

    
//加密方法
    public static String encode (String s){   

        
return encoder.encode(s.getBytes());   
    }  

    
//解密方法
    public static String decode (String s){   

        
try {   
            
byte[] temp = decoder.decodeBuffer(s);   
            
return new String(temp);  
        } 
catch (IOException io) {    
            io.printStackTrace();
        }   
        
return s;   
    }  

    
//测试Main
    /*public static void main(String args[]){
        String str="kanjingcai";   
        System.out.println("加密");   
        System.out.println(Base64.encode(str));   

        System.out.println("解密");   
        System.out.println(Base64.decode(Base64.encode(str)));
    }
*/
}

 

posted @ 2011-03-16 10:33  kanjc  阅读(416)  评论(0编辑  收藏  举报