Base64加密解密
import java.util.Base64;
public class YootkDemo {
public static void main(String[] args) throws Exception {
String message = "www.yootk.com" ;
Base64.Encoder encoder = Base64.getEncoder();
byte[] encodeData = encoder.encode(message.getBytes()) ;
System.out.println("【加密后的数据】" + new String(encodeData));
Base64.Decoder decoder = Base64.getDecoder();
byte[] decodeData = decoder.decode(encodeData);
System.out.println("【解密后的数据】" + new String(decodeData));
}
}
封装一个加密解决的工具类
package com.yootk.util;
import java.util.Base64;
public class PasswordUtil {
private static final int REPEAT = 5 ;
private static final String SALT = "edu.yootk.com" ;
private PasswordUtil() {}
public static String encrypt(String str) {
String encodeData = "{" + SALT + "}" + str ;
Base64.Encoder encoder = Base64.getEncoder();
for (int x = 0 ; x < REPEAT ; x ++) {
encodeData = encoder.encodeToString(encodeData.getBytes()) ;
}
return encodeData ;
}
public static String decrypt(String str) {
Base64.Decoder decoder = Base64.getDecoder();
byte data [] = str.getBytes() ;
for (int x = 0 ; x < REPEAT ; x ++) {
data = decoder.decode(data) ;
}
String decodeData = new String(data) ;
return decodeData.substring(("{" + SALT + "}").length()) ;
}
}
package com.yootk.demo;
import com.yootk.util.PasswordUtil;
public class YootkDemo {
public static void main(String[] args) throws Exception {
String message = "www.yootk.com" ;
System.out.println(PasswordUtil.encrypt(message));
System.out.println(PasswordUtil.decrypt(PasswordUtil.encrypt(message)));
}
}