import java.io.UnsupportedEncodingException; import org.apache.tomcat.util.codec.binary.Base64; /** * Base64内容传送编码被设计用来把任意序列的8位字节描述为一种不易被人直接识别的形式。*/ public class DEbase64{ /** * 加密 */ public static String encodeBase64(String cleartext) { try {
cleartext = new String(Base64.encodeBase64(cleartext.getBytes("UTF-8")));
} catch (UnsupportedEncodingException e) { e.printStackTrace(); } return cleartext; } /** * 解密 */ public static String decodeBase64(String ciphertext) { try {
ciphertext = new String(Base64.decodeBase64(ciphertext.getBytes()),"UTF-8");
} catch (UnsupportedEncodingException e) { e.printStackTrace(); } return ciphertext; } public static void main(String[] args) throws UnsupportedEncodingException {
String str = "dsfdsgfd";
String encode =DEbase64.encodeBase64(str); System.out.println(encode); String decode =DEbase64.decodeBase64(encode); System.out.println(decode); } }