Using Java Base64 encryption, decryption __java

Source: Internet
Author: User
Tags base64
Package com;
Import java.io.UnsupportedEncodingException;
Import Sun.misc.BASE64Decoder;
Import Sun.misc.BASE64Encoder;
		public class Demo {public static void main (string[] args) {String str = "Zhang";
		String base64=getbase64 (str);
		System.out.println (base64);
		String res = getFromBase64 (base64);
	System.out.println (RES);
		}//Encrypt public static string getBase64 (String str) {byte[] b=null;
		String S=null;
		try {b = str.getbytes ("Utf-8");
		catch (Unsupportedencodingexception e) {e.printstacktrace ();
		} if (b!=null) {s=new base64encoder (). Encode (b);
	return s;  
        }//Decrypt public static string GetFromBase64 (string s) {byte[] b = null;  
        String result = null;  
            if (s!= null) {Base64decoder decoder = new Base64decoder ();  
                try {b = Decoder.decodebuffer (s);  
            result = new String (b, "Utf-8"); catch (Exception e) {E.printstacktracE ();  
    } return result;
 }  

}

If the compiler complains,

Access Restriction:the method Encode (byte[]) from the type Characterencoder are not accessible due to restriction on Requi Red Library C:\Program Files\java\jre1.6.0_02\lib\rt.jar

An error occurred while calling Base64encoder and Base64decoder

The workaround is as follows

Eclipse under Window-->preferences-->java-->compiler-->error/warnings.
Select deprecated and restricted APIs. Change it to warning.
Change Forbidden and discouraged Reference and the IT to warning. (or as your need.)

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.