基于java的一种简易的随机密码生成器

第一步,取一个当前时间毫秒数,MD5成64位。
第二步,取这个64位的MD5的前6位和后6位。
第三步,将这12位中一部分数字转为特殊字符,一部分字母的大小写反转,如果数字过少,将部分字母转为特殊字符。
第四步,整体洗个牌。
代码如下:

public class PassWord {
    public static void main(String[] args) {
        Long saltLong = System.currentTimeMillis();
        String str = getMD5("加盐MD5",String.valueOf(saltLong));
        System.out.println(str);
        System.out.println(getPassWord(str));
    }
    //用于加密的字符
    private static final char md5String[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
            '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'};
    public static final char []chars = {'!','@',')','(','&','~','$','#','*','?','+','-'};
    /**
     *   单次MD5加密函数
     * @Author 李
     * @MethodName MD5
     * @Description //单次MD5加密函数
     * @Date 15:51 2020/9/25
     * @param pwd
     * @return java.lang.String
     * @exception
     **/
    public  static String getMD5(String pwd) {
        try {
            //使用平台的默认字符集将此 String 编码为 byte序列,并将结果存储到一个新的 byte数组中
            byte[] btInput = pwd.getBytes();
            //信息摘要是安全的单向哈希函数,它接收任意大小的数据,并输出固定长度的哈希值。
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            //MessageDigest对象通过使用 update方法处理数据, 使用指定的byte数组更新摘要
            mdInst.update(btInput);
            // 摘要更新之后,通过调用digest()执行哈希计算,获得密文
            byte[] md = mdInst.digest();

            // 把密文转换成十六进制的字符串形式
            int j = md.length;
            char str[] = new char[j * 4];
            int k = 0;
            for (int i = 0; i < j; i++) {   //  i = 0
                byte byte0 = md[i];  //95
                str[k++] = md5String[byte0 >>> 4 & 0x3d];    //    5
                str[k++] = md5String[byte0 & 0x3d];   //   z
                int n = k + 30;
                str[n++] = md5String[byte0 >>> 3 & 0x3d];
                str[n++] = md5String[byte0 >>> 2& 0x3d];
            }
            //返回经过加密后的字符串
            return new String(str);

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    /*
     *
     * @Author 李
     * @MethodName saltMD5
     * @Description //加盐MD5
     * @Date 15:51 2020/9/25
     * @param str
     * @param salt
     * @return java.lang.String
     * @exception
     **/
    public static String getMD5(String password ,String salt){
        return  getMD5(getMD5(password)+getMD5(salt));
    }
    /**
     *
     * 功能描述:随机密码生成器
     * @Author 李
     * @MethodName getPassWord
     * @Date 10:02 2020/11/17
     * @param passWord
     * @return java.lang.String
     * @exception
     **/
    public static String getPassWord(String passWord){
        String prefix = passWord.substring(0,6);
        String suffix = passWord.substring(passWord.length()-6);
        passWord = prefix + suffix;
        char [] charPassWord = passWord.toCharArray();
        List<Integer> intKeys = new ArrayList<>(16);
        List<String> letterKeys = new ArrayList<>(16);
        int intKey = 0;
        int letterKey = 0;
        for (int i = 0; i<charPassWord.length;i++){
            intKeys.add(i,null);
            letterKeys.add(i,null);
            if (charPassWord[i]< '9' && charPassWord[i]>='0'){
                intKeys.add(i,Integer.parseInt(String.valueOf(charPassWord[i])));
                intKey++;
            }
            if ((charPassWord[i]<='Z'&&charPassWord[i]>='A') ||
                    (charPassWord[i]<='z'&&charPassWord[i]>='a')){
                letterKeys.add(i,String.valueOf(charPassWord[i]));
                letterKey++;
            }
        }
        intKey = getRandom(intKey);
        letterKey = getRandom(letterKey);
        int ik = 0;
        int il = 0;
        for (int i = 0; i<charPassWord.length;i++){
            //抽选部分数字转为特殊字符
            if (intKeys != null && intKeys.size()>0 && ik <= intKey){
                if (intKeys.get(i) != null && Integer.parseInt(String.valueOf(charPassWord[i])) == intKeys.get(i)){
                    ik++;
                    charPassWord[i] = getHash(i);
                }
            }
            //抽选部分字母大小写转换
            if (letterKeys != null && letterKeys.size()>0 && il< letterKey){
                if (letterKeys.get(i) != null && String.valueOf(charPassWord[i]).equals(letterKeys.get(i))){
                    il++;
                    if (charPassWord[i]>='a' && charPassWord[i]<='z'){
                        charPassWord[i] -= 32;
                    }else {
                        charPassWord[i] += 32;
                    }
                    if(letterKey-intKey>=4 && i>=6){//当数字过少时,抽选部分字母转成特殊字符
                        charPassWord[i/2] = getHash(i);
                    }
                }
            }
        }
        charPassWord = shuffle(charPassWord);//洗牌算法
        String result = String.valueOf(charPassWord);
        return result;
    }
    public static int getRandom(int key){
        if (key == 0){
            return 0;
        }
        key = new Random().nextInt(key);
        return key;
    }
    public static char getHash(int c){
        return chars[c];
    }
    public static char [] shuffle(char [] c){//洗牌算法
        for (int i = 0;i<c.length;i++){
            int j = new Random().nextInt(c.length-1);
            if (c[i] != c[j] && !String.valueOf(c[i]).equals(String.valueOf(c[j]))){
                char tem = c[i];
                c[i] = c[j];
                c[j] = tem;
            }
        }
        return c;
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值