日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時間:2025/4/5 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 跨平台传输中使用base64来保证非ascii码字符串的完整性 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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

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

這種情況下,b和b1字節(jié)數(shù)組是相同的。

那下面這種情況呢?

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

打印出來的ss是一堆我們看不懂的東西!而且我們發(fā)現(xiàn)b和b1字節(jié)數(shù)組長度都不同啦?為什么?

我們知道ascii編碼的范圍為0~127,那么-2,-9該如何編碼呢??

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

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

通過使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.

注意,當(dāng)url傳輸過程中,為了保證不傳輸錯誤(例如缺少“+”等),請盡量使用urlSafe方法。

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

我們看一下編碼后的s是什么樣子的?

47, 118, 99, 114

編碼后全部變?yōu)?~127的ascii編碼,解碼后b1的值為:

-2, -9, 43

b和b1相同,沒有數(shù)據(jù)失真。

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

一些小細(xì)節(jié):

1. 跨平臺傳輸時可能傳輸?shù)氖鞘M制字符串,要轉(zhuǎn)換為byte數(shù)組再進行編碼,轉(zhuǎn)換方法為:從高位開始,兩個十六進制字符為一組轉(zhuǎn)為byte。實例如下:

String hex="1a2bcc";

先拆分,把“1a”,“2b” “cc”分別解析為byte數(shù)組 26,43,208?

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

轉(zhuǎn)載于:https://www.cnblogs.com/davidwang456/p/3935601.html

總結(jié)

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

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。