天天看點

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);

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

String decodeStr = new String(decodeBase64);

public static void bouncyCastleBase64(String src)

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

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

}