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

加解密算法之Base64

时间:2016-08-29 13:25:38      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:java base64

Base64不算严格的加密算法,因为加解密的算法都是公开的.

Base64的的三种提供者:

   1.jdk (不推荐)

   2.commonsCodes 

   3.bouncy castle


例:

import java.io.IOException;

import sun.misc.BASE64Decoder;

import sun.misc.BASE64Encoder;


public class Base64Demo

{


public static void main(String[] args) throws Exception

{

jdkBase64("测试 test");

commonsCodecBase64("测试 test");

bouncyCastleBase64("测试 test");

}


public static void jdkBase64(String src) throws IOException

{

BASE64Encoder encoder = new BASE64Encoder();

String encodeStr = encoder.encode(src.getBytes());

System.out.println("encode:" + encodeStr);


BASE64Decoder decoder = new BASE64Decoder();

byte[] decodeBuffer = decoder.decodeBuffer(encodeStr);

String decodeStr = new String(decodeBuffer);

System.out.println("decode:" + decodeStr);

}

public static void commonsCodecBase64(String src)

{

byte[] encodeBase64 = org.apache.commons.codec.binary.Base64.encodeBase64(src.getBytes());

String encodeStr = new String(encodeBase64);

System.out.println("encode:" + encodeStr);

byte[] decodeBase64 = org.apache.commons.codec.binary.Base64.decodeBase64(encodeStr);

String decodeStr = new String(decodeBase64);

System.out.println("decode:" + decodeStr);

}

public static void bouncyCastleBase64(String src)

{

byte[] encodeBase64 = org.bouncycastle.util.encoders.Base64.encode(src.getBytes());

String encodeStr = new String(encodeBase64);

System.out.println("encode:" + encodeStr);

byte[] decodeBase64 = org.bouncycastle.util.encoders.Base64.decode(encodeStr);

String decodeStr = new String(decodeBase64);

System.out.println("decode:" + decodeStr);

}


}


加解密算法之Base64

标签:java base64

原文地址:http://881206524.blog.51cto.com/10315134/1843660

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