由于MD5解密网站的存在,普通的md5加密可能被破解
利用random函数动态生成slat盐进行加密。
突然写工具类是因为单位给的md5加密工具能加密的量太少了,千把条数据就卡死
自己写的类一两秒就六七万条加密完成了,因为不是多线程工作,用的StringBuilder加快字符串操作效率。
package com.utils;
import org.springframework.util.DigestUtils;
import java.io.*;
import java.util.ArrayList;
import java.util.Random;
/**
* MD5动态加密类
*
* @author 丁子恒
* @since 2020.10.23
* @version 1.0
*
*/
public class MD5Util {
//用于后台输出加密总数
static int flag=0;
//用于加盐混交md5
private static String slat = "dtjm@DZH";
/**
* 生成md5
*/
public static String getMD5(String str) throws UnsupportedEncodingException {
String base = str + "/" + slat;
String md5 = DigestUtils.md5DigestAsHex(base.getBytes("UTF-8"));
return md5;
}
/**
*传入文件路径,加密文件内容
* 并输出到指定目录
*/
public static void encryptFileContent(String fileName,String newFileName) {
Random rm = new Random();//先生成一个对象rm,
int b= 0;
StringBuilder sb = new StringBuilder();
File file1 = new File(fileName);
File file2 = new File(newFileName);
if(!file2.getParentFile().exists()){
file2.getParentFile().mkdirs();
}
BufferedReader reader = null;
BufferedWriter writer=null;
try {
reader = new BufferedReader(new FileReader(file1));
String tempStr;
while ((tempStr = reader.readLine()) != null) {
System.out.println(tempStr);
slat+=rm.nextLong(); //调用rm对象的nextLong方法,随机生成一个长随机数
flag++;
if (tempStr.length()==18) {
sb.append(tempStr.substring(0, 14));
sb.append(getMD5(tempStr.substring(14, 18)));
sb.append("\r\n");
}else{
sb.append(tempStr);
sb.append("\r\n");
}
}
reader.close();
//2:准备输出流
String str=sb.toString();
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file2)));
writer.write(str);
writer.flush();
} catch (IOException e) {
System.out.println();
e.printStackTrace();
} finally {
if (reader != null) {
try {
System.out.println(flag);
reader.close();
writer.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
System.out.println("输出完毕");
}
public static void main(String[] args) throws Exception {
encryptFileContent("D:\\demo\\test.txt","D:\\demo\\testResult.txt");
}
}
springboot的framework依赖,大家应该创建sp工程默认都有。。
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>