使用Des加密的时候想必有人遇到过这种问题,每个加密结果都会带有一个换行符号,其实这是Base64对byte数组编码成字符串时flag值得问题
解决办法:flag使用Base64.NO_WRAP
Base64.encodeToString(bytes, Base64.NO_WRAP);
下面接Base64关键源码(废话开始):
首先是编码函数👇
/**
* Base64-encode the given data and return a newly allocated
* String with the result.
*
* @param input the data to encode
* @param flags controls certain features of the encoded output.
* Passing {@code DEFAULT} results in output that
* adheres to RFC 2045.
*/
public static String encodeToString(byte[] input, int flags) {
try {
return new String(encode(input, flags), "US-ASCII");
} catch (UnsupportedEncodingException e) {
// US-ASCII is guaranteed to be available.
throw new AssertionError(e);
}
}
接下来看flag参数:
Base64{
/**
* Default values for encoder/decoder flags.
*/
public static final int DEFAULT = 0;
/**
* Encoder flag bit to omit the padding '=' characters at the end
* of the output (if any).
*/
public static final int NO_PADDING = 1;
/**
* Encoder flag bit to omit all line terminators (i.e., the output
* will be on one long line).
*/
public static final int NO_WRAP = 2;
//...省略一万字
}
给NO_WRAP上个翻译:编码器标志位,用于省略所有行结束符(即,输出
*会排很长的队)。
hh,感觉还是没懂啥意思,反正就是省略掉命令行得回车符号就对了;