欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java实现的密码强度检测功能完整示例

程序员文章站 2024-02-18 16:02:46
本文实例讲述了java实现的密码强度检测功能。分享给大家供大家参考,具体如下: checkstrength.java文件: package com.wx.pwd...

本文实例讲述了java实现的密码强度检测功能。分享给大家供大家参考,具体如下:

checkstrength.java文件:

package com.wx.pwd;
/**
 * 检测密码强度
 *
 * @author venshine
 */
public class checkstrength {
  public enum level {
    easy, midium, strong, very_strong, extremely_strong
  }
  /**
   * num 数字
   * small_letter 小写字母
   * capital_letter 大写字母
   * other_char 特殊字符
   */
  private static final int num = 1;
  private static final int small_letter = 2;
  private static final int capital_letter = 3;
  private static final int other_char = 4;
  /**
   * 简单的密码字典
   */
  private final static string[] dictionary = {"password", "abc123", "iloveyou", "adobe123", "123123", "sunshine",
      "1314520", "a1b2c3", "123qwe", "aaa111", "qweasd", "admin", "passwd"};
  /**
   *检查字符类型,包括num、大写字母、小写字母和其他字符。
   *
   * @param c
   * @return
   */
  private static int checkcharactertype(char c) {
    if (c >= 48 && c <= 57) {
      return num;
    }
    if (c >= 65 && c <= 90) {
      return capital_letter;
    }
    if (c >= 97 && c <= 122) {
      return small_letter;
    }
    return other_char;
  }
  /**
   * 按不同类型计算密码的数量
   *
   * @param passwd
   * @param type
   * @return
   */
  private static int countletter(string passwd, int type) {
    int count = 0;
    if (null != passwd && passwd.length() > 0) {
      for (char c : passwd.tochararray()) {
        if (checkcharactertype(c) == type) {
          count++;
        }
      }
    }
    return count;
  }
  /**
   * 检查密码的强度
   *
   * @param passwd
   * @return strength level
   */
  public static int checkpasswordstrength(string passwd) {
    if (stringutils.equalsnull(passwd)) {
      throw new illegalargumentexception("password is empty");
    }
    int len = passwd.length();
    int level = 0;
    // 增加点
    //判断密码是否含有数字有level++
    if (countletter(passwd, num) > 0) {
      level++;
    }
    //判断密码是否含有小写字母有level++
    if (countletter(passwd, small_letter) > 0) {
      level++;
    }
    //判断密码是否还有大写字母有level++
    if (len > 4 && countletter(passwd, capital_letter) > 0) {
      level++;
    }
    //判断密码是否还有特殊字符有level++
    if (len > 6 && countletter(passwd, other_char) > 0) {
      level++;
    }
    //密码长度大于4并且2种类型组合......(不一一概述)
    if (len > 4 && countletter(passwd, num) > 0 && countletter(passwd, small_letter) > 0
        || countletter(passwd, num) > 0 && countletter(passwd, capital_letter) > 0
        || countletter(passwd, num) > 0 && countletter(passwd, other_char) > 0
        || countletter(passwd, small_letter) > 0 && countletter(passwd, capital_letter) > 0
        || countletter(passwd, small_letter) > 0 && countletter(passwd, other_char) > 0
        || countletter(passwd, capital_letter) > 0 && countletter(passwd, other_char) > 0) {
      level++;
    }
    //密码长度大于6并且3中类型组合......(不一一概述)
    if (len > 6 && countletter(passwd, num) > 0 && countletter(passwd, small_letter) > 0
        && countletter(passwd, capital_letter) > 0 || countletter(passwd, num) > 0
        && countletter(passwd, small_letter) > 0 && countletter(passwd, other_char) > 0
        || countletter(passwd, num) > 0 && countletter(passwd, capital_letter) > 0
        && countletter(passwd, other_char) > 0 || countletter(passwd, small_letter) > 0
        && countletter(passwd, capital_letter) > 0 && countletter(passwd, other_char) > 0) {
      level++;
    }
    //密码长度大于8并且4种类型组合......(不一一概述)
    if (len > 8 && countletter(passwd, num) > 0 && countletter(passwd, small_letter) > 0
        && countletter(passwd, capital_letter) > 0 && countletter(passwd, other_char) > 0) {
      level++;
    }
    //密码长度大于6并且2种类型组合每种类型长度大于等于3或者2......(不一一概述)
    if (len > 6 && countletter(passwd, num) >= 3 && countletter(passwd, small_letter) >= 3
        || countletter(passwd, num) >= 3 && countletter(passwd, capital_letter) >= 3
        || countletter(passwd, num) >= 3 && countletter(passwd, other_char) >= 2
        || countletter(passwd, small_letter) >= 3 && countletter(passwd, capital_letter) >= 3
        || countletter(passwd, small_letter) >= 3 && countletter(passwd, other_char) >= 2
        || countletter(passwd, capital_letter) >= 3 && countletter(passwd, other_char) >= 2) {
      level++;
    }
    //密码长度大于8并且3种类型组合每种类型长度大于等于3或者2......(不一一概述)
    if (len > 8 && countletter(passwd, num) >= 2 && countletter(passwd, small_letter) >= 2
        && countletter(passwd, capital_letter) >= 2 || countletter(passwd, num) >= 2
        && countletter(passwd, small_letter) >= 2 && countletter(passwd, other_char) >= 2
        || countletter(passwd, num) >= 2 && countletter(passwd, capital_letter) >= 2
        && countletter(passwd, other_char) >= 2 || countletter(passwd, small_letter) >= 2
        && countletter(passwd, capital_letter) >= 2 && countletter(passwd, other_char) >= 2) {
      level++;
    }
    //密码长度大于10并且4种类型组合每种类型长度大于等于2......(不一一概述)
    if (len > 10 && countletter(passwd, num) >= 2 && countletter(passwd, small_letter) >= 2
        && countletter(passwd, capital_letter) >= 2 && countletter(passwd, other_char) >= 2) {
      level++;
    }
    //特殊字符>=3 level++;
    if (countletter(passwd, other_char) >= 3) {
      level++;
    }
    //特殊字符>=6 level++;
    if (countletter(passwd, other_char) >= 6) {
      level++;
    }
    //长度>12 >16 level++
    if (len > 12) {
      level++;
      if (len >= 16) {
        level++;
      }
    }
    // 减少点
    if ("abcdefghijklmnopqrstuvwxyz".indexof(passwd) > 0 || "abcdefghijklmnopqrstuvwxyz".indexof(passwd) > 0) {
      level--;
    }
    if ("qwertyuiop".indexof(passwd) > 0 || "asdfghjkl".indexof(passwd) > 0 || "zxcvbnm".indexof(passwd) > 0) {
      level--;
    }
    if (stringutils.isnumeric(passwd) && ("01234567890".indexof(passwd) > 0 || "09876543210".indexof(passwd) > 0)) {
      level--;
    }
    if (countletter(passwd, num) == len || countletter(passwd, small_letter) == len
        || countletter(passwd, capital_letter) == len) {
      level--;
    }
    if (len % 2 == 0) { // aaabbb
      string part1 = passwd.substring(0, len / 2);
      string part2 = passwd.substring(len / 2);
      if (part1.equals(part2)) {
        level--;
      }
      if (stringutils.ischarequal(part1) && stringutils.ischarequal(part2)) {
        level--;
      }
    }
    if (len % 3 == 0) { // ababab
      string part1 = passwd.substring(0, len / 3);
      string part2 = passwd.substring(len / 3, len / 3 * 2);
      string part3 = passwd.substring(len / 3 * 2);
      if (part1.equals(part2) && part2.equals(part3)) {
        level--;
      }
    }
    if (stringutils.isnumeric(passwd) && len >= 6) { // 19881010 or 881010
      int year = 0;
      if (len == 8 || len == 6) {
        year = integer.parseint(passwd.substring(0, len - 4));
      }
      int size = stringutils.sizeofint(year);
      int month = integer.parseint(passwd.substring(size, size + 2));
      int day = integer.parseint(passwd.substring(size + 2, len));
      if (year >= 1950 && year < 2050 && month >= 1 && month <= 12 && day >= 1 && day <= 31) {
        level--;
      }
    }
    if (null != dictionary && dictionary.length > 0) {// dictionary
      for (int i = 0; i < dictionary.length; i++) {
        if (passwd.equals(dictionary[i]) || dictionary[i].indexof(passwd) >= 0) {
          level--;
          break;
        }
      }
    }
    if (len <= 6) {
      level--;
      if (len <= 4) {
        level--;
        if (len <= 3) {
          level = 0;
        }
      }
    }
    if (stringutils.ischarequal(passwd)) {
      level = 0;
    }
    if (level < 0) {
      level = 0;
    }
    return level;
  }
  /**
   *获得密码强度等级,包括简单、复杂、强、强、强
   *
   * @param passwd
   * @return
   */
  public static level getpasswordlevel(string passwd) {
    int level = checkpasswordstrength(passwd);
    switch (level) {
      case 0:
      case 1:
      case 2:
      case 3:
        return level.easy;
      case 4:
      case 5:
      case 6:
        return level.midium;
      case 7:
      case 8:
      case 9:
        return level.strong;
      case 10:
      case 11:
      case 12:
        return level.very_strong;
      default:
        return level.extremely_strong;
    }
  }
}

stringutils.java文件:

package com.wx.pwd;
/**
 * 字符串工具类
 *
 * @author venshine
 */
public class stringutils {
  private final static int[] size_table = {9, 99, 999, 9999, 99999, 999999, 9999999, 99999999, 999999999,
      integer.max_value};
  /**
   * 计算一个整数的大小
   *
   * @param x
   * @return
   */
  public static int sizeofint(int x) {
    for (int i = 0; ; i++)
      if (x <= size_table[i]) {
        return i + 1;
      }
  }
  /**
   * 判断字符串的每个字符是否相等
   *
   * @param str
   * @return
   */
  public static boolean ischarequal(string str) {
    return str.replace(str.charat(0), ' ').trim().length() == 0;
  }
  /**
   * 确定字符串是否为数字
   *
   * @param str
   * @return
   */
  public static boolean isnumeric(string str) {
    for (int i = str.length(); --i >= 0; ) {
      if (!character.isdigit(str.charat(i))) {
        return false;
      }
    }
    return true;
  }
  /**
   * 判断字符串是否为空格、空(“)”或null。
   *
   * @param str
   * @return
   */
  public static boolean equalsnull(string str) {
    int strlen;
    if (str == null || (strlen = str.length()) == 0 || str.equalsignorecase("null")) {
      return true;
    }
    for (int i = 0; i < strlen; i++) {
      if ((character.iswhitespace(str.charat(i)) == false)) {
        return false;
      }
    }
    return true;
  }
}

checkpwd.java文件:

package com.wx.pwd;
public class checkpwd {
 public static void main(string[] args) {
    string passwd = "mynamejob123_-+=";
    system.out.println(checkstrength.checkpasswordstrength(passwd));
    system.out.println(checkstrength.getpasswordlevel(passwd));
  }
}

运行结果:

13
extremely_strong

ps:这里再为大家提供两款功能类似的在线工具供大家参考:

密码安全性在线检测:

高强度密码生成器:
http://tools.jb51.net/password/createstrongpassword

在线随机数字/字符串生成工具:

更多关于java算法相关内容感兴趣的读者可查看本站专题:《java数据结构与算法教程》、《java字符与字符串操作技巧总结》、《java操作dom节点技巧总结》、《java文件与目录操作技巧汇总》和《java缓存操作技巧汇总

希望本文所述对大家java程序设计有所帮助。