Base64 is used in Java encryption to guarantee the integrity of non-ASCII code strings

Source: Internet
Author: User

First, let's look at an example:

byte [] b=newbyte[]{2,9,43}; string SS=New string (b, "Utf-8"); byte [] b1=ss.getbytes ();

In this case, the B and B1 byte arrays are the same.

What about the following situation?

byte [] b=newbyte[]{-2,-9,43}; string SS=New string (b, "Utf-8"); byte [] b1=ss.getbytes ();

The printed SS is a bunch of things we can't read! And we found that the B and B1 byte arrays are different lengths? Why?

We know that the ASCII encoding range is 0~127, so how does -2,-9 encode it?

The byte of B1 and B means that the data is distorted during the transfer process, so how to solve the distortion problem?

We can use Base64 to transform the value of -128~127 (please google for yourself).

You can prevent errors during transmission by making Base64 encoding decoding. Base64 can be used with COMMONS-CODEC, as shown below:

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 the "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 character blocks.
static byte[] encodeBase64(byte[] binaryData, boolean isChunked, boolean urlSafe)Encodes binary data using the Base64 algorithm, optionally chunking the output into 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 character blocks.
static byte[] encodeBase64Chunked(byte[] binaryData)Encodes binary data using the Base64 algorithm and chunks the encoded output into 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 an integer according to crypto standards such as a "s xml-signature
static boolean isArrayByteBase64(byte[] arrayOctet)Deprecated. 1.5 Use isBase64(byte[]) , would be is removed in 2.0.
static boolean isBase64(byte octet)Returns whether or not, in the octet base, 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 are in the octet Base64 alphabet.
boolean isUrlSafe()Returns our current encode mode.

Note that when the URL is transferred, use the Urlsafe method as much as possible in order to ensure that no transmission errors, such as "+", are missing.

        byte [] b=newbyte[]{-2,-9,43};         byte [] s=base64.encodebytestobytes (b);         byte [] B1=base64.decode (s);

Let's see what the coded s looks like.

47, 118, 99, 114

After encoding, the ASCII encoding becomes 0~127, and the value of B1 after decoding is:

-2,-9, 43

B and B1 are the same, no data distortion.

Alternatively, you can use bouncy castle support. Specifically, Google can do it.

Some small details:

1. Cross-platform transmission may be transmitted by a hexadecimal string, to be converted to a byte array and then encoded, the conversion method is: Starting from high, two hexadecimal characters for a set to byte. Examples are as follows:

String hex= "1A2BCC";

First split, "1a", "2b" "CC" Resolved to byte array 26,43,208

2. Cross-platform to consider the encoding format, such as Utf-8 or GBK or iso-8895-1.

Base64 is used in Java encryption to guarantee the integrity of non-ASCII code strings

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.