1. JDK
/** * Use JDK to implement Base64 encryption */public static void JdkBase64 () {try {Base64encoder encoder = new Base64encoder (); String encode = Encoder.encode (Src.getbytes ()); System.out.println ("Encode:" + encode); Base64decoder decoder = new Base64decoder (); System.out.println ("Decode:" + New String (Decoder.decodebuffer (encode));} catch (IOException e) {e.printstacktrace ();}}
In the case of using this method, there may be a situation where base64encoder cannot be found, which resolves:
Right-click Project--Properties-->java build Path-->libraries-->jre System library-->access rules-->edit-->add--> Resolution Select Accessible,rule pattern to fill in the **-->ok.
2. Commons Codec
/** * Use Commonscodes to implement Base64 encryption */public static void CommonsCodesBase64 () {byte[] encodebytes = base64.encodebase64 ( Src.getbytes ()); System.out.println ("Encode:" + new String (encodebytes)); byte[] decodebytes = base64.decodebase64 (encodebytes); System.out.println ("Decode:" + new String (decodebytes));}
3. Bouncy Castle
/** * Use Bouncycastle to implement Base64 encryption */public static void BouncyCastleBase64 () {byte[] encodebytes = Org.bouncycastle.util.encoders.Base64.encode (Src.getbytes ()); System.out.println ("Encode:" + new String (encodebytes)); byte[] Decodebytes = Org.bouncycastle.util.encoders.Base64.decode (encodebytes); System.out.println ("Decode:" + new String (decodebytes));}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Java" (1) BASE64 encryption technology