跨平台传输中使用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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值