Base64编码换行的问题

it2025-07-24  10

今天我在使用Base64对字符串进行编码,然后传给第三方进行比对的时候怎么都匹配不正确

编码的方法如下:

/** * 编码 * @param bStr * @return String */ public static String encode(byte[] bStr){ return new sun.misc.BASE64Encoder().encode(bStr); }

根据RFC822规定,BASE64Encoder编码每76个字符,还需要加上一个回车换行  部分Base64编码的java库还按照这个标准实行

而sun自带的这个Base64还是支持这个原则的,所以编码出来的字符串里面存在大量的换行符

其实解决方法很简单,更换编码的包就行采用apache的Base64

/** * 不带换行符的base64 * @param str * @return */ public static String encodeBase64(String str){ return Base64.encodeBase64String(str.getBytes()); }

 

最新回复(0)