码迷,mamicode.com
首页 > 编程语言 > 详细

Java加密系列之(二)Base64加密

时间:2015-04-25 15:11:14      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:base64   java   加密   commonscodec   bouncycastle   

sun.misc.BASE64Encoder/BASE64Decoder类不属于JDK标准库范畴,但在JDK中包含了该类,可以直接使用,但这个没有java doc,属于jdk里不推荐使用的工具类。强烈建议不要用sun.misc,是不安全的。

bouncycastle的jar包下载地址:http://www.bouncycastle.org/latest_releases.html


package com.tvm.mrz.security.base64;

import java.io.IOException;

import org.apache.commons.codec.binary.Base64;

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

public class Base64Test {

	private static String src = "Mr.Z Security Base64";

	public static void jdkBase64() {
		BASE64Encoder encoder = new BASE64Encoder();
		String encode = encoder.encode( src.getBytes() );
		System.out.println( "encode:" + encode );

		BASE64Decoder decoder = new BASE64Decoder();
		try {
			byte[] decode = decoder.decodeBuffer( encode );
			System.out.println( "decode:" + new String( decode ) );
		} catch( IOException e ) {
			e.printStackTrace();
		}
	}

	public static void commonsCodecBase64() {
		// String encode = Base64.encodeBase64String( src.getBytes() );
		byte[] encode = Base64.encodeBase64( src.getBytes() );
		System.out.println( "encode:" + new String( encode ) );

		byte[] decode = Base64.decodeBase64( encode );
		System.out.println( "decode:" + new String( decode ) );
	}

	public static void bouncyCastleBase64() {
		byte[] encode = org.bouncycastle.util.encoders.Base64.encode( src.getBytes() );
		System.out.println( "encode:" + new String( encode ) );

		byte[] decode = org.bouncycastle.util.encoders.Base64.decode( encode );
		System.out.println( "decode:" + new String( decode ) );
	}

	public static void main( String[] args ) {
		jdkBase64();
		commonsCodecBase64();
		bouncyCastleBase64();
	}
}


Java加密系列之(二)Base64加密

标签:base64   java   加密   commonscodec   bouncycastle   

原文地址:http://blog.csdn.net/gangannini/article/details/45270215

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