我们先在maven中添加base64所需要的依赖
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.6</version>
</dependency>
一、对文字进行base64编码的方法
public String plus(String data) throws UnsupportedEncodingException {
String s = new String(Base64.encodeBase64(data.getBytes(code)), code);
return s;
}
使用方法:
public static void main(String[] args) throws UnsupportedEncodingException {
String data = "哈喽!";
Base64Test base64Test = new Base64Test();
String plus = base64Test.plus(data);
System.out.println("加密后----" + plus);
}
运行结果
二、进行对base64编码后的数据进行解码
public String sulotion(String data) throws UnsupportedEncodingException {
String s = new String(Base64.decodeBase64(data.getBytes(code)), code);
return s;
}
public static void main(String[] args) throws UnsupportedEncodingException {
String data = "哈喽!";
Base64Test base64Test = new Base64Test();
String plus = base64Test.plus(data);
System.out.println("加密后----" + plus);
String sulotion = base64Test.sulotion(plus);
System.out.println("解码后---" + sulotion);
}
运行结果:
三、使用MD5加密
首先先引入需要的maven依赖
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
</dependency>
以下是具体代码
public String MD5(String data) throws UnsupportedEncodingException {
String s = DigestUtils.md5Hex(data.getBytes(code));
return s;
}
public static void main(String[] args) throws UnsupportedEncodingException {
String data = "哈喽!";
Base64Test base64Test = new Base64Test();
String md5 = base64Test.MD5(data);
System.out.println("MD5加密的值为--"+md5);
}
运行结果为: