package appoint; import org.apache.commons.codec.binary.Base64; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.security.MessageDigest; import java.text.DateFormat; import java.text.DecimalFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.List; import java.util.Locale; public class test { /** * 利用Java实现字母(大小写)+数字+字符的穷举 * 如果需要其他的字符,直接接到字符数组中即可 * 如果只需要 * 1.数字 * 2.字母 * 3.字符 * 4.数字+字母 * 5.字母+字符 * 6.数字+字符 * 拆分fullCharSource数组即可 * */ //密码可能会包含的字符集合 // private static char[] fullCharSource = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', // 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', // 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', // '~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '{', '}', '|', ':', '"', '<', '>', '?', ';', '\'', ',', '.', '/', '-', '=', '`'}; private static char[] fullCharSource = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'}; //将可能的密码集合长度 private static int fullCharLength = fullCharSource.length; private static String fileName = "/password"; //密码穷举文件较大,全部打印非常消耗内存 private static int fileSize = 1; // 单位 MB /** * 穷举打印输出,可以将打印输出的文件形成字典 * * @param minLength:生成的字符串的最小长度 * @param maxLength:生成的字符串的最大长度 */ public static List<String> generate(int minLength,int maxLength) throws IOException { List<String> result = new ArrayList<>(); //计数器 int counter = 0; int counter2 = 0*fullCharLength+1; StringBuilder buider = new StringBuilder(); for (int i = 0; i < minLength-1; i++) { buider.insert(0, fullCharSource[fullCharLength-1]); counter2 = counter2*fullCharLength+1; } counter= counter2-1; while (buider.toString().length() <= maxLength) { buider = new StringBuilder(maxLength * 2); int _counter = counter; //10进制转换成26进制 while (_counter >= fullCharLength) { //获得低位 buider.insert(0, fullCharSource[_counter % fullCharLength]); _counter = _counter / fullCharLength; //精髓所在,处理进制体系中只有10没有01的问题,在穷举里面是可以存在01的 _counter--; } //最高位 buider.insert(0, fullCharSource[_counter]); counter++; if(buider.toString().length()<=maxLength){ writeFile(buider.toString()); result.add(buider.toString()); } } return result; } public static String encryptSha256(String inputStr){ try{ MessageDigest md = MessageDigest.getInstance("SHA-256"); byte[] digest = md.digest(inputStr.getBytes("UTF-8")); return new String(Base64.encodeBase64(digest)); } catch (Exception e) { e.printStackTrace(); } return null; } public static void writeFile(String inputStr) throws IOException { //获取日志文件保存路径 // Properties props = PropertiesLoaderUtils.loadAllProperties("systemParam.properties"); String realPath = "/home/cwbx/logs"; //props.getProperty("itemRevenueStatistics.file.path").trim(); // 指定文件路径 String filePath = realPath+fileName; // 文件大小 文件过大时,自动生成新的文件 double size = getFileSize(filePath); String[] name = fileName.split("\\."); String[] name2 = name[0].split("-"); int fileCountNum = Integer.parseInt(name2[1]); if(size >= fileSize){ fileCountNum++; fileName = name2[0]+"-"+fileCountNum+"."+name[1]; filePath = realPath+fileName; } // 文件内容 StringBuilder sb = new StringBuilder(); String password = encryptSha256(inputStr); String jg = "明文:【"+inputStr+"】 密文:【"+password+"】"; sb.append(jg).append("\n"); System.out.println(jg); try (FileOutputStream fos = new FileOutputStream(filePath, true)) { fos.write(sb.toString().getBytes()); fos.close(); System.out.println("数据成功存储在文件中"); } catch (IOException e){ e.printStackTrace(); } } /** * 计算文件大小 * @param filePath * @return MB */ private static double getFileSize(String filePath){ File imageFile = new File(filePath); long size = imageFile.length(); DecimalFormat format = new DecimalFormat("###.0"); double i = (size / (1024.0 * 1024.0)); return Double.parseDouble(format.format(i)); } public static void main(String[] args) throws IOException { // int length = 4; Calendar calendar = Calendar.getInstance(); DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss", Locale.ENGLISH); dateFormat.setLenient(false); java.util.Date timeDate = calendar.getTime(); String currentTime = dateFormat.format(timeDate); fileName = fileName+currentTime+"-1.txt"; // writeFile("123456","/password20240126131327-1.txt"); generate(1,1); } }
穷举---数字+字符+特殊符号
于 2024-01-29 08:22:20 首次发布