java 加密之Base64示例

JDK

会创建一个BASE64Encoder 和 BASEDecoder 对象加解密

public static void jdkBase64(){
		try {
		BASE64Encoder encoder = new BASE64Encoder() ;
		String encode = encoder.encode(src.getBytes());
		System.out.println(encode);
		
		BASE64Decoder decoder = new BASE64Decoder();
		String str = new String(decoder.decodeBuffer(encode));
		System.out.println(str);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

Commons Codec

    public static void CommonsCodecBase64(){
        byte[] encode = Base64.encodeBase64(src.getBytes());
        System.out.println(new String(encode));
        byte[] decode = Base64.decodeBase64(encode);
        System.out.println(new String(decode));
    }

Bouncy Castle

public static void BouncyCastleBase64(){
		byte[] encode = org.bouncycastle.util.encoders.Base64.encode(src.getBytes());
		System.out.println(new String(encode));
		byte[] decode = org.bouncycastle.util.encoders.Base64.decode(encode);
		System.out.println(new String(decode));
	}



你可能感兴趣的:(java)