码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2020-12-04 11:14:08      阅读:5      评论:0      收藏:0      [点我收藏+]

标签:method   ima   character   var   chunk   ascii码   start   细节   utf-8   

技术图片

首先,我们来看一个例子:

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

  1. 跨平台要考虑编码格式,如utf-8 或者gbk 或者iso-8895-1等。

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

标签:method   ima   character   var   chunk   ascii码   start   细节   utf-8   

原文地址:https://blog.51cto.com/15015181/2556802

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!