java 根据指定最新最多长度生成字典

package com.yhx.utils;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;


public class DictionarySeek {


    //密码可能会包含的字符集合
    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 int fullCharLength = fullCharSource.length;

    /**
     * 穷举打印输出,可以将打印输出的文件形成字典
     *
     * @param maxLength:生成的字符串的最大长度
     */
    public static void generate(int maxLength) {
        //计数器,多线程时可以对其加锁,当然得先转换成Integer类型。
        int counter = 0;
        StringBuilder buider = new StringBuilder();
        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++;
            System.out.println(buider.toString());
        }
    }

    public static void createPwd(int pwdStartLen,int pwdMaxLen,char[] pwdDict){
        long pwdDictLen=pwdDict.length;
        char[] pwd = new char[pwdMaxLen+2];
        int[] array = new int[pwdMaxLen];

        int currentLen = pwdStartLen;
        int i,j = 0;
        boolean next;
        try{
            long startTime = System.currentTimeMillis();
            FileOutputStream fos = new FileOutputStream("pwd1.txt");
            OutputStreamWriter osw = new OutputStreamWriter(fos);
            BufferedWriter bw = new BufferedWriter(osw);

            while(currentLen <= pwdMaxLen){
                for(i = 0; i < pwdMaxLen; i ++ )
                    array[i] = 0;
                next = true;
                //String tempPwd;
                while(next){

                    for(i = 0; i < currentLen; i ++ )
                        pwd[i]= pwdDict[array[i]];
//                    pwd[i] = '';
                    //tempPwd = String.valueOf(pwd);
                    bw.write(new String(pwd,0,currentLen));
                    System.out.println(new String(pwd,0,currentLen));
                    bw.write('n');

                    for(j = currentLen -1 ; j >= 0; j -- ){
                        array[j]++;
                        if(array[j]!=pwdDictLen)
                            break;
                        else{
                            array[j]=0;
                            if(j==0)
                                next = false;
                        }
                    }
                }
                currentLen++;
            }
            bw.flush();
            bw.close();
            long endTime = System.currentTimeMillis();
            System.out.println("Pwd Generate Complete! "+ (endTime - startTime)/1000.0 + " seconds");
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        long beginMillis = System.currentTimeMillis();
        System.out.println(beginMillis);//开始时间
//        generate(50);                    //以最大长度为50测试
        createPwd(8, 8, fullCharSource);
        long endMillis = System.currentTimeMillis();
        System.out.println(endMillis);//结束时间
        System.out.println(endMillis - beginMillis);//总耗时,毫秒
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值