String text = "base64 in java8 lib";
//编码
String encode = Base64.getEncoder().encodeToString(text.getBytes(StandardCharsets.UTF_8));
System.out.println(encode);
//解码
String decode = new String(Base64.getDecoder().decode(encode), StandardCharsets.UTF_8);
System.out.println(decode);
package com.want.imdg.utils;
import java.security.*;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
/**
* EncryptUtils.java
* Original Author:
* Company: mdcl
* Copyright @ mdcl Co.,Ltd.
**/
public class EncryptUtils {
private static final String PASSWORD_CRYPT_KEY = "88888888";
private final static String DES = "DES";
/**
* 加密
* @param src 数据源
* @param key 密钥,长度必须是8的倍数
* @return 返回加密后的数据
* @throws Exception
*/
public byte[] encrypt(byte[] src, byte[] key) throws Exception {
// DES算法要求有一个可信任的随机数源
SecureRandom sr = new SecureRandom();
// 从原始密匙数据创建DESKeySpec对象
DESKeySpec dks = new DESKeySpec(key);
// 创建一个密匙工厂,然后用它把DESKeySpec转换成
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
SecretKey securekey = keyFactory.generateSecret(dks);
// Cipher对象实际完成加密操作
Cipher cipher = Cipher.getInstance(DES);
// 用密匙初始化Cipher对象
cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
// 获取数据并加密
return cipher.doFinal(src);
}
/**
* 解密
* @param src 数据源
* @param key 密钥,长度必须是8的倍数
* @return 返回解密后的原始数据
* @throws Exception
*/
public byte[] decrypt(byte[] src, byte[] key) throws Exception {
// DES算法要求有一个可信任的随机数源
SecureRandom sr = new SecureRandom();
// 从原始密匙数据创建一个DESKeySpec对象
DESKeySpec dks = new DESKeySpec(key);
// 创建一个密匙工厂,然后用它把DESKeySpec对象转换成
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
SecretKey securekey = keyFactory.generateSecret(dks);
// Cipher对象实际完成解密操作
Cipher cipher = Cipher.getInstance(DES);
// 用密匙初始化Cipher对象
cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
//获取数据并解密
return cipher.doFinal(src);
}
/**
* 密码解密
* @param data
* @return
* @throws Exception
*/
public final String decrypt(String data) throws Exception {
try {
return new String(decrypt(hex2byte(data.getBytes()),PASSWORD_CRYPT_KEY.getBytes()));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 密码加密
* @param password
* @return
* @throws Exception
*/
public final String encrypt(String password) {
try {
return byte2hex(encrypt(password.getBytes(), PASSWORD_CRYPT_KEY.getBytes()));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 二行制转字符串
* @param b
* @return
*/
public String byte2hex(byte[] b) {
String hs = "";
String stmp = "";
for (int n = 0; n < b.length; n++) {
stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length() == 1)
hs = hs + "0" + stmp;
else
hs = hs + stmp;
}
return hs.toUpperCase();
}
public byte[] hex2byte(byte[] b) {
if ((b.length % 2) != 0)
throw new IllegalArgumentException("长度不是偶数");
byte[] b2 = new byte[b.length / 2];
for (int n = 0; n < b.length; n += 2) {
String item = new String(b, n, 2);
b2[n / 2] = (byte) Integer.parseInt(item, 16);
}
return b2;
}
public static void main(String[] args) throws Exception {
EncryptUtils en = new EncryptUtils();
String str = "password";
String str1 = en.encrypt(str);
System.out.println("加密后的串 : " + str1);
System.out.println("解密后的串 : " + en.decrypt(str1));
}
}
ApplicationContextUtil
package com.want.imdg.utils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ApplicationContextUtil {
static ApplicationContext ctx;
public static ApplicationContext getContext() {
if (ctx != null) {
return ctx;
}
ctx = new ClassPathXmlApplicationContext("/applicationContent.xml");
return ctx;
}
public static Object getBean(String name) {
if (name == null || "".equals(name)) {
return null;
}
return getContext().getBean(name);
}
}
public static String exception(Throwable t) {
if (t == null)
return null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
t.printStackTrace(new PrintStream(baos));
try {
baos.close();
} catch (IOException e) {
log.error("batch-TaskResponse-exception(),IOException:", e);
}
return baos.toString();
}
public class SystemGCTask implements Runnable, Serializable {
static Logger log = Logger.getLogger(SystemGCTask.class);
/**
*
*/
private static final long serialVersionUID = -2805100782229098414L;
@Override
public void run() {
log.info("Run System.gc();");
System.gc();
}
}
package com.want.imdg.utils;
import java.security.MessageDigest;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 加密明文密码
* @author 00272334
*
*/
public class PwdUtils {
/**
* MD5加密字符串
* @param inStr
* @return 加密后的字符串
* @throws Exception
*/
public static String md5Encode(String inStr) throws Exception {
MessageDigest md5 = null;
try {
md5 = MessageDigest.getInstance("MD5");
} catch (Exception e) {
System.out.println(e.toString());
e.printStackTrace();
return "";
}
byte[] byteArray = inStr.getBytes("UTF-8");
byte[] md5Bytes = md5.digest(byteArray);
StringBuffer hexValue = new StringBuffer();
for (int i = 0; i < md5Bytes.length; i++) {
int val = ((int) md5Bytes[i]) & 0xff;
if (val < 16) {
hexValue.append("0");
}
hexValue.append(Integer.toHexString(val));
}
return hexValue.toString();
}
/**
* 正则表达式验证密码规则
* @param str
* @return
*/
public static boolean regex(String str){
// 至少包含一个大写字母、一个数字、一个特殊字符、一些小写字母 大于八位~@#$%^&*()_+|\\=-`
// String pattern = "(?=^.{8,25}$)(?=(?:.*?\\d){1})(?=.*[a-z])(?=(?:.*?[A-Z]){1})(?=(?:.*?[!@#$%*()_+^&}{:;?.]){1})(?!.*\\s)[0-9a-zA-Z!@#$%?*()_+^&]*$";
// String pattern = "(?=^.{8,25}$)(?=(?:.*?\\d){1})(?=.*[a-z])(?=(?:.*?[A-Z]){1})(?=(?:.*?[~@#$%^&*()_+|\\=-`}{:;?.]){1})(?!.*\\s)[0-9a-zA-Z!~@#$%^&*()_+|\\=-`]*$";
String pattern = "(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9]).{8,}";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(str);
return m.matches();
}
/**
* 测试主函数
* @param args
* @throws Exception
*/
public static void main(String args[]) throws Exception {
// String str = new String("P@ssw0rd3");//43c6b452ddf13e5e74254dbca7b5ca68
// System.out.println("原始:" + str);
// System.out.println("MD5后:" + md5Encode(str));
String str = "wwwwwwwww@";
String pattern = "(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9]).{8,}";
// String pattern = "(?=^.{8,25}$)(?=(?:.*?\\d){1})(?=.*[a-z])(?=(?:.*?[A-Z]){1})(?=(?:.*?[!@#$%*()_+^&}{:;?.]){1})(?!.*\\s)[0-9a-zA-Z!@#$%*()_+^&]*$";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(str);
System.out.println(m.matches());
}
}