JAVA Base64 解码 编码出现丢失末尾 “=”

Base64 解码 编码出现丢失末尾 “=”

测试代码:


import java.io.IOException;

import org.apache.tomcat.util.codec.binary.Base64;

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


public class TT {

	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		String str = "DTE5MTQ1OQ1lbmRvYmoNMyAwIG9iag08PCANL1R5cGUgL1BhZ2VzIA0vS2lkcyBbIDQgMCBSIF0g DS9Db3VudCAxDT4+DWVuZG9iag14cmVAATAgIDkNAAAwAAAwAAAwMCA2NTUzNSBAAQowAAAwAAAw AAE5IDAwAAAwIG4NCjAwAAAwAAAwNjkgAAAwAAAgbg0KAAAwAAE5MjEwNSAwAAAwMCBuDQowAAAw AAAwMTgyIDAwAAAwIG4NCjAwAAAwAAAzMzMgAAAwAAAgbg0KAAAwAAAwAAQxOSAwAAAwMCBuDQow AAAwAAAwNDM3IDAwAAAwIG4NCjAwAAAxOTIwODMgAAAwAAAgbg0KdHJhaWxlcg08PA0vU2l6ZSA5 DS9Sb290IDEgMCBSDS9JbmZvIDIgMCBSDS9JRFs8MjkwAAAwAAAyMzQ4AAAwMEZGMTgwAAAwRkY2 NzAwAAA+PDI5AAAwAAAwMjM0ODAwAABGRjE4AAAwMEZGNjcwAAAwPl0NPj4Nc3RhcnR4cmVAATE5 MjE2Nw0lJUVPRg0=";
//		jdkBase64(str);
//		apacheBase64(str);
		
		test1(str);
		test2(str);
		test3(str);
		test4(str);
		
	}
	

	/**
	 * jdk 解码编码
	 * @param str
	 * @throws Exception 
	 */
	public static void test1(String str) throws Exception {
		System.out.println("test1 ... [begin]");
		jdkEncode(jdkDecode(str));
		System.out.println("test1 ... [ end ]");
	}
	/**
	 * apache 解码编码
	 * @param str
	 */
	public static void test2(String str) throws Exception{
		System.out.println("test2 ... [begin]");
		apacheEncode(apacheDecode(str));
		System.out.println("test2 ... [ end ]");
	}
	/**
	 * jdk 解码 apache 编码
	 * @param str
	 */
	public static void test3(String str) throws Exception{
		System.out.println("test3 ... [begin]");
		apacheEncode(jdkDecode(str));
		System.out.println("test3 ... [ end ]");
	}
	/**
	 * apache 解码 jdk 编码
	 * @param str
	 */
	public static void test4(String str) throws Exception{
		System.out.println("test4 ... [begin]");
		jdkEncode(apacheDecode(str));
		System.out.println("test4 ... [ end ]");
	}
	
	@SuppressWarnings("restriction")
	public static void jdkBase64(String str) throws Exception {
		System.out.println("jdk:"+str.substring(str.length()-10));
		byte[] bytes = new BASE64Decoder().decodeBuffer(str);
		str = new BASE64Encoder().encode(bytes);
		System.out.println("jdk:"+str.substring(str.length()-10));
	}
	/**
	 *  jdk 解码
	 * @param str
	 * @return
	 * @throws IOException 
	 */
	@SuppressWarnings("restriction")
	public static byte[] jdkDecode(String str) throws IOException {
		System.out.println("jdk:"+str.substring(str.length()-10));
		return new BASE64Decoder().decodeBuffer(str);
	}

	/**
	 *  jdk 编码
	 * @param str
	 * @return
	 * @throws IOException
	 */
	@SuppressWarnings("restriction")
	public static String jdkEncode(byte[] bytes) throws IOException {
		String str = new BASE64Encoder().encode(bytes);
		System.out.println("jdk:"+str.substring(str.length()-10));
		return str;
	}

	/**
	 * apache 解码
	 * @param str
	 * @return
	 * @throws Exception
	 */
	public static byte[] apacheDecode(String str) throws Exception {
		System.out.println("apa:"+str.substring(str.length()-10));
		Base64 base64 = new Base64();
		return base64.decode(str);
	}
	/**
	 * apache 编码
	 * @param bytes
	 * @return
	 * @throws Exception
	 */
	public static String apacheEncode(byte[] bytes) throws Exception {
		Base64 base64 = new Base64();
		String str = base64.encodeAsString(bytes);
		System.out.println("apa:"+str.substring(str.length()-10));
		return str;
	}
	
	
	public static void apacheBase64(String str) {
		System.out.println("apache:"+str.substring(str.length()-10));
		Base64 base64 = new Base64();
		byte[] bytes = base64.decode(str);
		str = base64.encodeAsString(bytes);
		System.out.println("apache:"+str.substring(str.length()-10));
	}
	public static void zlcBase64(String str) throws Exception {
		System.out.println("zlc:"+str.substring(str.length()-10));
		byte[] bytes = new BASE64Decoder().decodeBuffer(str);
		Base64 base64 = new Base64();
		str = base64.encodeAsString(bytes);
		System.out.println("apache:"+str.substring(str.length()-10));
		
	}
	
}

不建议使用 JDK 自带的 BASE64 解码;
使用 apache 工具包中的BASE64工具类(不只这一种) org.apache.tomcat.util.codec.binary.Base64

参考:从原理上搞定编码-- Base64编码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值