Say base64 bit code. __java

Source: Internet
Author: User
Tags base64 printable characters

   Although there have been a lot of introductions about Base64 coding on the web, I still want to sum it up in order to prevent myself from forgetting. First to show you a bunch of code, out of the unknown said, this is a string of Java code that uses Base64 to encode strings.

public class Base64encoder {private static final char Last2byte = (char) integer.parseint ("00000011", 2); private static
Final char last4byte = (char) integer.parseint ("00001111", 2);
private static final char Last6byte = (char) integer.parseint ("00111111", 2);
private static final char Lead6byte = (char) integer.parseint ("11111100", 2);
private static final char Lead4byte = (char) integer.parseint ("11110000", 2);
private static final char Lead2byte = (char) integer.parseint ("11000000", 2);  private static final char[] encode_table = new char[] {' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' I ', ' J ', ' K ', ' L ', ' M ', ' N ', ' O ', ' P ', ' Q ', ' R ', ' S ', ' T ', ' U ', ' V ', ' W ', ' X ', ' Y ', ' Z ', ' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' h ', ' I ', ' j ', ' K ', ' l ' , ' m ', ' n ', ' o ', ' P ', ' Q ', ' R ', ' s ', ' t ', ' u ', ' V ', ' w ', ' x ', ' y ', ' z ', ' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' +
', '/' };  private static final String default_encoding = "UTF-8"; Specifies that the encoding set public Base64encoder () {} is public static String encode (StrinG data) throws Unsupportedencodingexception {return encode (Data.getbytes (default_encoding));} public static String Enco De (byte[] from) {StringBuffer to = new StringBuffer (int) (FROM.LENGTH * 1.34) + 3) int num = 0; char currentbyte = 0; F or (int i = 0; i < from.length i++) {num = num% 8; while (num < 8) {switch (num) {Case 0:currentbyte = (char)
(From[i] & Lead6byte);
Currentbyte = (char) (Currentbyte >>> 2);
Break
Case 2:currentbyte = (char) (From[i] & Last6byte);
Break
Case 4:currentbyte = (char) (From[i] & Last4byte);
Currentbyte = (char) (Currentbyte << 2);
if ((i + 1) < From.length) {Currentbyte |= (from[i + 1] & Lead2byte) >>> 6;} break;
Case 6:currentbyte = (char) (From[i] & Last2byte);
Currentbyte = (char) (Currentbyte << 4);
if ((i + 1) < From.length) {Currentbyte |= (from[i + 1] & Lead4byte) >>> 4;} break;
} to.append (Encode_table[currentbyte]);
num + + 6; } if (To.length ()% 4!=0 {for (int i = 4-to.length ()% 4; i > 0; i--) {to.append ("=");}} return To.tostring (); }

}

Base64 is a representation of binary data based on 64 printable characters, because 2 of the 6 times equals 64, so each 6 bit is a unit, corresponding to a printable character. The 6-bit front then complements 2 0 to form a 8-bit byte form. For example, I now have a 24-bit byte. It's a form of 3*8=24, and then we're going to divide it into 4*6=24 form.

eg:11010101 11000101 00110011 (3*8=24)

     00110101 00011100 00010100 00110011 (4*6=24)

is to take out the first 6 digits to the left: 110101 and then 2 0 in the front, forming 00110101 so that is the first byte.

Then we take the remaining 6-digit 01 1100, and the front 2 0 turns 00011100.

Finally in order to go again 6 digits: 010100, the front of 2 0 into 00010100.

Similarly, the last remaining 6 digits: 110011, the front 2 0 to 00110011.

Next we'll do base64 coding. First convert all binary to 10, and find the corresponding number from our encode_table. In fact, the number in the encode--table is the letter A-Z, A-Z, the number 0-9, so that there are 62 characters, in addition to the two printable symbols in different systems and different, generally + and/. Note that there is also a saying in the V pad =

Just need to find their corresponding serial number. Attached encdode_table table:


For an example of how to:

For example, there is a 2 number: 00110101 encoded into what after it. (Pretending this number has changed from 8 bits to 6 in addition to 0)

First of all, it should be binary decimal, it should turn. 2^5+2^4+2^2+2^0=53, and then find the number in the corresponding table is 53 corresponding to the 1.BASE64 encoded result is 1.

Next all introduced, to the whole exercise one.

String "Zhang 3" for Base64 code to get what it is.

This involves the operation of Chinese binary conversion. Provides a way to convert a string to binary, and I provide Java code

String str = "Zhang 3";
   Byte[] B = str.getbytes ();
   for (int i=0;i<b.length;i++) {
       System.out.println (integer.tobinarystring (B[i]&0xff));
 }

And then get 11010101 11000101 00110011 (3*8)

00110101 00011100 00010100 00110011 (4*6)

are converted to decimal 53 28 20 51

The base64 code for the conversion is: 1cUz Well, that's it.

But I have to say a question. What if your numbers don't multiply by 6?

eg:11010101 11000101 (2*8=16)

00110101 00011100 00010100 (according to our words, in front of 2 0, the last number will be only 6, so that in the last 0, a few to make up a few 0, to fill 8 in the line to the decimal: a pad (only the end of 0 of the remember to write pad )

So the base64 code is: 1cu= (because the pad corresponds to equals).

Follow-up: Later I also met in front of the need to use the signature of the place, is called Cryptojs Library, which provides a variety of JavaScript to write algorithms, very convenient.

Talk about the subsequent use of TS written for the calculation of the signature algorithm: (mentioned above, previously used in the library of Crypto, directly in the library inside the calculation of the line);

GetData (data, App_secret) {let
        myData = [];
        for (Let X. data) {Let
            temp = ' ${x}=${data[x]} ';
            Mydata.push (temp);
        }
        Let result = Mydata.sort (). Join (' & ');
        Let B = new window[' Base64 '] ();
        Let sign = window[' Cryptojs ']. MD5 (window[' Cryptojs '). HmacSHA1 (B.encode (Result), App_secret)). toString ();
        return sign;
    }

Welcome advice, if there is wrong, remember to tell me.

Thanks





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.