import java.io.UnsupportedEncodingException;
import org.apache.commons.codec.binary.Base64;
public class base64 {
public static void main(String[] args) throws UnsupportedEncodingException {
String str = "加密的文字abc";
//加密
String str1 = new String(Base64.encodeBase64(str.getBytes("UTF-8")));
System.out.println("encode=="+str1);
//解密
String str2 = new String(Base64.decodeBase64(new String(str1).getBytes("UTF-8")));
System.out.println("decode=="+str2);
}
}
结果:
encode==5Yqg5a+G55qE5paH5a2XYWJj
decode==加密的文字abc
需要的jar:commons-codec-1.2.jar
maven:
<groupId>apache-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.2</version>
===========================================