Fork me on GitHub

BASE64实现加密

有两种实现的方法:

  自己添加额外jar包:javabase64-1.3.1

  使用jdk自带的类,但通常都会报找不到类,解决方法就是:http://blog.csdn.net/a0501bqzhxy/article/details/6441526

  < buildpath -->  access rule --> add --> accessible --> ** >

 

  方式<一>:

package com.lzj.www.base64.test;

import it.sauronsoftware.base64.Base64;

import org.junit.Test;


public class BASE64Test {

	public String ecodeObject(String object){
		return Base64.encode(object);
	}
	
	public String decodeObject(String object){
		return Base64.decode(object);
	}

	
	@Test
	public void test(){
		System.out.println(ecodeObject("hello"));
	}
	
	@Test
	public void test_decode(){
		System.out.println(decodeObject(ecodeObject("hello")));
	}
	
}

   方式<二>:

package com.lzj.www.base64.test;

import org.junit.Test;

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


public class BASE64Test {
	
	public String ecodeObject_2(String object){
		byte[] b = null;
		if(object != null){
			b = object.getBytes();
		}
		return new BASE64Encoder().encode(b);
	}
	
	public String decodeObject_2(String object){
		byte[] b = null;
		String result = null;
		if(object != null){
			try {
				b = new BASE64Decoder().decodeBuffer(object);
				result = new String(b, "utf-8");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return result;
	}
	
	@Test
	public void test(){
		System.out.println(ecodeObject_2("hello"));
	}
	
	@Test
	public void test_decode(){
		System.out.println(decodeObject_2(ecodeObject_2("hello")));
	}
	
}

 

posted @ 2014-10-16 21:05  BeCoded  Views(706)  Comments(0)    收藏  举报