日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

跨平台传输中使用base64来保证非ascii码字符串的完整性

發布時間:2025/4/5 52 豆豆
生活随笔 收集整理的這篇文章主要介紹了 跨平台传输中使用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 TypeMethod and Description
static byte[]decodeBase64(byte[]?base64Data) Decodes Base64 data into octets
static byte[]decodeBase64(String?base64String) Decodes a Base64 String into octets
static?BigIntegerdecodeInteger(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?StringencodeBase64String(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?StringencodeBase64URLSafeString(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 booleanisArrayByteBase64(byte[]?arrayOctet) Deprecated.? 1.5 Use?isBase64(byte[]), will be removed in 2.0.
static booleanisBase64(byte?octet) Returns whether or not the?octet?is in the base 64 alphabet.
static booleanisBase64(byte[]?arrayOctet) Tests a given byte array to see if it contains only valid characters within the Base64 alphabet.
static booleanisBase64(String?base64) Tests a given String to see if it contains only valid characters within the Base64 alphabet.
protected booleanisInAlphabet(byte?octet) Returns whether or not the?octet?is in the Base64 alphabet.
booleanisUrlSafe() 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等。

轉載于:https://www.cnblogs.com/davidwang456/p/3935601.html

總結

以上是生活随笔為你收集整理的跨平台传输中使用base64来保证非ascii码字符串的完整性的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。