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.)