Java简单加密算法
一切由Base64推敲
import java.io.UnsupportedEncodingException;
import java.util.Base64;
//简单加密
public class Base64Strategy {
static String append = "昊天锤";
//加密方法1
public String enCode(String password) throws UnsupportedEncodingException {
//格式:Base64获取加密器.加密(加密的内容),注意字符串转byte或者byte转字符串需要设置编码统一
byte[] encode = Base64.getEncoder().encode(password.getBytes("UTF-8"));
return new String(encode,"Utf-8");
}
//加密方法2
public String superEncode(String password) throws UnsupportedEncodingException {
//将一个固定串追加到密码后,解密成字符串时使用string的replace方法用空串取代追加的字符串
byte[] encode = Base64.getEncoder().encode((password+append).getBytes("UTF-8"));
return new String(encode,"UTF-8");
}
//解密方法,对加密后的字符串明文解密
public String decode(String encode) throws UnsupportedEncodingException {
//格式:Base64获取加密器.加密(加密的内容),注意字符串转byte或者byte转字符串需要设置编码统一
byte[] decode = Base64.getDecoder().decode(encode.getBytes("UTF-8"));
return new String(decode,"UTF-8");
}
public static void main(String[] args) throws UnsupportedEncodingException {
String password = "123456";
Base64Strategy base64Strategy = new Base64Strategy();
System.out.println("法1加密:"+base64Strategy.enCode(password));
System.out.println("法1解密:"+base64Strategy.decode(base64Strategy.enCode(password)));
System.out.println("法2加密:"+base64Strategy.superEncode(password));
System.out.println("法1解密:"+base64Strategy.decode(base64Strategy.superEncode(password)).
replace(append,""));
}
}