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


首先,我們來看一個例子:

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

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

那下面這種情況呢?

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該如何編碼呢? 

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

我們可以使用base64對-128~127的值進行改造(具體請自行google之)。

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

Method Summary

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方法。

        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 

2. 跨平台要考慮編碼格式,如utf-8 或者gbk 或者iso-8895-1等。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM