使用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,感覺還是沒懂啥意思,反正就是省略掉命令行得回車符號就對了;