Several encryption algorithms

Source: Internet
Author: User

BASE64 Encryption:

1.org.apache.commons.codec, no line break after encryption

 Packageutil;Importjava.io.UnsupportedEncodingException;Importorg.apache.commons.codec.binary.Base64;/*** @todo Apache Commons-codec.jar The resulting encoded string is no line break *@authorZhangyanan * @date August 7, 2018*/ Public classCommonscodecencodedecode {/*** Encrypt STR * *@paramSTR *@return     */     Public Staticstring getBase64 (String str) {byte[] B =NULL; String s=NULL; Try{b= Str.getbytes ("Utf-8"); } Catch(unsupportedencodingexception e) {e.printstacktrace (); }        if(b! =NULL) {s=base64.encodebase64string (b); }        returns; }        /*** Decrypt S * *@paramSTR *@return     */     Public Staticstring GetFromBase64 (string s) {byte[] B =NULL; String result=NULL; if(s! =NULL) {            Try{b=Base64.decodebase64 (s); Result=NewString (b, "Utf-8"); } Catch(Exception e) {e.printstacktrace (); }        }        returnresult; }}
View Code

2.sun.misc.base64, not recommended, with a newline return after encryption

 Packageutil;ImportJava.io.FileOutputStream;ImportJava.io.OutputStream;Importjava.io.UnsupportedEncodingException;ImportSun.misc.BASE64Decoder;ImportSun.misc.BASE64Encoder;/*** @todo BASE64 Encryption and decryption class *@authorZhangyanan * @date August 6, 2018*/@SuppressWarnings ("Restriction") Public classEncodeanddecode { Public Static voidMain (string[] args) {String base64= GetBase64 ("HTTP:WWW.BAIDU.COMASDFSDFCAEWFSDCSDFQWEAFD input 1230.,. "); String fromBase64=GetFromBase64 (base64);        System.out.println (base64);        System.out.println ();            System.out.println (FROMBASE64); }    /*** Encrypt STR * *@paramSTR *@return     */     Public Staticstring getBase64 (String str) {byte[] B =NULL; String s=NULL; Try{b= Str.getbytes ("Utf-8"); } Catch(unsupportedencodingexception e) {e.printstacktrace (); }        if(b! =NULL) {s=NewBase64encoder (). Encode (b); }        returns; }        /*** Decrypt S * *@paramSTR *@return     */     Public Staticstring GetFromBase64 (string s) {byte[] B =NULL; String result=NULL; if(s! =NULL) {Base64decoder decoder=NewBase64decoder (); Try{b=Decoder.decodebuffer (s); Result=NewString (b, "Utf-8"); } Catch(Exception e) {e.printstacktrace (); }        }        returnresult; }     Public Static BooleanGenerateimage (String base64str, String savepath) {//Base64 decoding a byte array string and generating a picture        if(Base64str = =NULL)//image data is empty            return false; //System.out.println ("Start decoding");Base64decoder decoder =NewBase64decoder (); Try {            //Base64 decoding            byte[] B =Decoder.decodebuffer (BASE64STR); //System.out.println ("decode complete");             for(inti = 0; i < b.length; ++i) {if(B[i] < 0) {//Adjust Exception DataB[i] + = 256; }            }            //System.out.println ("Start image generation"); //Create a JPEG pictureOutputStream out =NewFileOutputStream (Savepath);            Out.write (b);            Out.flush ();            Out.close (); return true; } Catch(Exception e) {e.printstacktrace (); return false; }    }}
View Code

3.javax.xml.bind.datatypeconverter recommended use of concise speed, no line break, java1.6 start built-in

 Packageutil;ImportJavax.xml.bind.DatatypeConverter;/*** @todo *@authorZhangyanan * @date August 7, 2018*/ Public classDatatypeconvertertest { Public Static voidMain (string[] args) {String s= "Www.bai Comment"; String printbase64binary=datatypeconverter.printbase64binary (S.getbytes ());        System.out.println (printbase64binary); byte[] Parsebase64binary =datatypeconverter.parsebase64binary (printbase64binary); System.out.println (NewString (parsebase64binary)); System.out.println ("----------"); }}
View Code

4.java.util.base64 recommended, the fastest, concise, no line-wrapping, but to java1.8 environment (@ Reference blog)

Importjava.io.UnsupportedEncodingException;Importjava.util.Base64;/*** @todo java8 java.util.Base64 Test *@authorZhangyanan * @date August 7, 2018*/ Public classTestBase64 { Public Static voidMain (string[] args)throwsunsupportedencodingexception {//CodingString asB64 = Base64.getencoder (). encodetostring ("chat ss33!%". GetBytes ("Utf-8")); System.out.println (asB64);//output is: c29tzsbzdhjpbmc=//decoding        byte[] Asbytes =Base64.getdecoder (). Decode (asB64); System.out.println (NewString (Asbytes, "utf-8"));//output is: some string    }    }
View Code

Several encryption algorithms

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.