The use of the Java BASE64 algorithm

Source: Internet
Author: User
Tags base64

Base64 is a common network encryption algorithm, and BASE64 encoding can be used to pass longer identity information in an HTTP environment. See Base64 Introduction

1 Customizing the Base64 algorithm

Base64encrypt.java

 Public classBase64encrypt {Private StaticFinal String CODES ="abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/="; //Base64 Decryption    Private Static byte[] Base64decode (String input) {if(Input.length ()%4!=0) {            Throw NewIllegalArgumentException ("Invalid Base64 Input"); }        byteDecoded[] =New byte[((Input.length () *3) /4)                -(Input.indexof ('=') >0? (Input.length ()-Input.indexof ('=')) :0)]; Char[] Inchars =Input.tochararray (); intj =0; intB[] =New int[4];  for(inti =0; i < inchars.length; i + =4) {            //This could is made faster (but more complicated) by precomputing//these index locations.b[0] =Codes.indexof (Inchars[i]); b[1] = Codes.indexof (inchars[i +1]); b[2] = Codes.indexof (inchars[i +2]); b[3] = Codes.indexof (inchars[i +3]); Decoded[j++] = (byte) ((b[0] <<2) | (b[1] >>4)); if(b[2] < -) {decoded[j++] = (byte) ((b[1] <<4) | (b[2] >>2)); if(b[3] < -) {decoded[j++] = (byte) ((b[2] <<6) | b[3]); }            }        }        returnDecoded; }    //Base64 Encryption    Private StaticString Base64Encode (byte[]inch) {StringBuilder out=NewStringBuilder ((inch. length *4) /3); intb;  for(inti =0; I <inch. length; i + =3) {b= (inch[I] &0xFC) >>2;  out. Append (Codes.charat (b)); b= (inch[I] &0x03) <<4; if(i +1<inch. Length) {b|= (inch[i +1] &0xF0) >>4;  out. Append (Codes.charat (b)); b= (inch[i +1] &0x0F) <<2; if(i +2<inch. Length) {b|= (inch[i +2] &0xC0) >>6;  out. Append (Codes.charat (b)); b=inch[i +2] &0x3F;  out. Append (Codes.charat (b)); } Else {                     out. Append (Codes.charat (b));  out. Append ('='); }            } Else {                 out. Append (Codes.charat (b));  out. Append ("=="); }        }        return  out. toString (); }

Test code:

     Public Static voidMain (string[] args) {String input="we are a team."; String encode=Base64Encode (Input.getbytes ()); System. out. println ("encode:"+encode); String Decode=NewString (Base64decode (encode)); System. out. println ("Decode:"+decode); }
The Base64 algorithm of 2 Bcprov

Introduction of Bcprov-jdk15on-154.jar, providing support for Base64 algorithms

Bcprov-jdk15on-154.jar Address: https://commons.apache.org/proper/commons-codec/download_codec.cgi

Test code:

String input ="we are a team."; byte[] Encodebytes =Org.bouncycastle.util.encoders.Base64.encode (Input.getbytes ()); System. out. println ("encode:"+NewString (encodebytes)); byte[] Decodebytes =Org.bouncycastle.util.encoders.Base64.decode (encodebytes); System. out. println ("Decode:"+NewString (decodebytes));

The use of the Java BASE64 algorithm

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.