跨平臺傳輸中使用base64來保證非ascii碼字符串的完整性

跨平臺傳輸中使用base64來保證非ascii碼字符串的完整性

首先,咱們來看一個例子:數組

byte[] b=new byte[]{2,9,43};
String ss=new String(b,"utf-8");
byte[] b1=ss.getbytes();

這種狀況下,b和b1字節數組是相同的。ide

那下面這種狀況呢?google

byte[] b=new byte[]{-2,-9,43};
String ss=new String(b,"utf-8");
byte[] b1=ss.getbytes();

打印出來的ss是一堆咱們看不懂的東西!並且咱們發現b和b1字節數組長度都不一樣啦?爲何?編碼

咱們知道ascii編碼的範圍爲0~127,那麼-2,-9該如何編碼呢? url

b1和b的字節表示在傳遞過程當中,數據失真了,那如何解決失真問題呢?code

咱們能夠使用base64對-128~127的值進行改造(具體請自行google之)。blog

經過使base64編碼解碼則能夠防止傳輸過程當中出錯。base64可以使用commons-codec的,以下所示:ip

Method Summaryutf-8

Methods
Modifier and Type Method and Description
static byte[] decodeBase64(byte[] base64Data)
Decodes Base64 data into octets
static byte[] decodeBase64(String base64String)
Decodes a Base64 String into octets
static BigInteger decodeInteger(byte[] pArray)
Decodes a byte64-encoded integer according to crypto standards such as W3C's XML-Signature
static byte[] encodeBase64(byte[] binaryData)
Encodes binary data using the base64 algorithm but does not chunk the output.
static byte[] encodeBase64(byte[] binaryData, boolean isChunked)
Encodes binary data using the base64 algorithm, optionally chunking the output into 76 character blocks.
static byte[] encodeBase64(byte[] binaryData, boolean isChunked, boolean urlSafe)
Encodes binary data using the base64 algorithm, optionally chunking the output into 76 character blocks.
static byte[] encodeBase64(byte[] binaryData, boolean isChunked, boolean urlSafe, int maxResultSize)
Encodes binary data using the base64 algorithm, optionally chunking the output into 76 character blocks.
static byte[] encodeBase64Chunked(byte[] binaryData)
Encodes binary data using the base64 algorithm and chunks the encoded output into 76 character blocks
static String encodeBase64String(byte[] binaryData)
Encodes binary data using the base64 algorithm but does not chunk the output.
static byte[] encodeBase64URLSafe(byte[] binaryData)
Encodes binary data using a URL-safe variation of the base64 algorithm but does not chunk the output.
static String encodeBase64URLSafeString(byte[] binaryData)
Encodes binary data using a URL-safe variation of the base64 algorithm but does not chunk the output.
static byte[] encodeInteger(BigInteger bigInt)
Encodes to a byte64-encoded integer according to crypto standards such as W3C's XML-Signature
static boolean isArrayByteBase64(byte[] arrayOctet)
Deprecated.
1.5 Use isBase64(byte[]), will be removed in 2.0.
static boolean isBase64(byte octet)
Returns whether or not the octet is in the base 64 alphabet.
static boolean isBase64(byte[] arrayOctet)
Tests a given byte array to see if it contains only valid characters within the Base64 alphabet.
static boolean isBase64(String base64)
Tests a given String to see if it contains only valid characters within the Base64 alphabet.
protected boolean isInAlphabet(byte octet)
Returns whether or not the octet is in the Base64 alphabet.
boolean isUrlSafe()
Returns our current encode mode.
注意,當url傳輸過程當中,爲了保證不傳輸錯誤(例如缺乏「+」等),請儘可能使用urlSafe方法。ci

byte[] b=new byte[]{-2,-9,43};
        byte[] s=Base64.encodeBytesToBytes(b);
        byte[] b1=Base64.decode(s);

咱們看一下編碼後的s是什麼樣子的?

47, 118, 99, 114
編碼後所有變爲0~127的ascii編碼,解碼後b1的值爲:

-2, -9, 43
b和b1相同,沒有數據失真。

另外,也能夠是使用bouncy castle支持。具體能夠google之。

一些小細節:

  1. 跨平臺傳輸時可能傳輸的是十六進制字符串,要轉換爲byte數組再進行編碼,轉換方法爲:從高位開始,兩個十六進制字符爲一組轉爲byte。實例以下:

String hex="1a2bcc";

先拆分,把「1a」,「2b」 「cc」分別解析爲byte數組 26,43,208

  1. 跨平臺要考慮編碼格式,如utf-8 或者gbk 或者iso-8895-1等。
相關文章
相關標籤/搜索