码迷,mamicode.com
首页 > 其他好文 > 详细

BASE64实现加密

时间:2014-10-16 22:35:43      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:blog   http   io   使用   ar   java   sp   div   art   

有两种实现的方法:

  自己添加额外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")));
	}
	
}

 

BASE64实现加密

标签:blog   http   io   使用   ar   java   sp   div   art   

原文地址:http://www.cnblogs.com/D-Key/p/4029696.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!