java弱口令检测机制
1. 设计要求
- 应具备检测口令的长度和是否在指定字符集合内的能力。
- 应具备检测口令字符逻辑相邻的能力,如aBc,abC等。
- 应具备检测口令字符键盘物理位置相邻的能力,包括横向和左右斜线方向的相邻,如qwer 1qaz 0okm等。
- 应具备检测口令是否出现在弱口令库中的能力。
- 应具备检测口令中是否包含用户名(不区分大小写)。
- 应具备相邻单字符多次重复检测。
2. 二级系统配置要求
- 口令应为字母(区分大小写)+数字的组合,长度不小于8位,数字个数必须大于等于1,小写或大写字母个数大于等于1。
- 口令字符在键盘物理位置上横向不允许有连续4个及以上的相邻,物理横向字符集包含“qwertyuiop asdfghjkl zxcvbnm 01234567890”。
- 口令中的字符在键盘物理位置上斜线方向不允许有连续4个及以上的相邻,物理斜线方向字符集包含“1qaz 2wsx 3edc 4rfv 5tgb 6yhn 7ujm 8ik, 9ol. 0p;/ =[;. -pl, 0okm 9ijn 8uhb 7ygv 6tfc 5rdx 4esz”。
- 口令中的字符在逻辑位置上不允许有连续4个及以上相邻,例如abcd,1234等,必须是严格意义上的逻辑相邻abcD、aBcd、Abcd也属于逻辑相邻;逻辑字符集为“abcdefghijklmnopqrstuvwxyz 0123456789”。
- 相邻单字符重复次数不得超过4次。
3. 三级系统配置要求
- 口令应为英文字母(区分大小写)+数字+特殊字符三者的组合,长度不小于8位;数字个数必须大于等于1,小写或大写字母个数大于等于1,特殊字符个数大于等于1。
- 口令中的字符在键盘物理位置上横向不允许有连续3个及以上相邻,物理横向字符集包含“qwertyuiop asdfghjkl zxcvbnm 01234567890”。
- 口令中字符在键盘物理位置上斜线方向不允许有连续3个及以上相邻,物理斜线方向字符集包含“1qaz 2wsx 3edc 4rfv 5tgb 6yhn 7ujm 8ik, 9ol. 0p;/ =[;.、-pl,、0okm、9ijn、8uhb、7ygv 6tfc、5rdx 4esz”。
- 口令中字符在逻辑位置上不允许有连续3个及以上相邻的,例如abc,123等,必须是严格意义上的逻辑相邻abC、aBc、Abc也属于逻辑相邻;逻辑字符集为“abcdefghijklmnopqrstuvwxyz 0123456789”。
- 口令中的特殊字符集为“!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~” 不包含两边引号共32位字符。
- 相邻单字符重复次数不得超过3次。
4. java编码
package com.security.weakpassword;
import java.lang.String;
public class CheckPWD {
public static boolean checkPasswordLength(String password) {
boolean flag =false;
if("".equals(config.MAX_LENGTH)) {
if (password.length() >= Integer.parseInt(config.MIN_LENGTH)) {
flag = true;
}
}else{
if (password.length() >= Integer.parseInt(config.MIN_LENGTH) &&
password.length() <= Integer.parseInt(config.MAX_LENGTH)) {
flag = true;
}
}
return flag;
}
public static boolean checkContainDigit(String password) {
char[] chPass = password.toCharArray();
boolean flag = false;
int num_count = 0;
for (int i = 0; i < chPass.length; i++) {
if (Character.isDigit(chPass[i])) {
num_count++;
}
}
if (num_count >= 1){
flag = true;
}
return flag;
}
public static boolean checkContainCase(String password) {
char[] chPass = password.toCharArray();
boolean flag = false;
int char_count = 0;
for (int i = 0; i < chPass.length; i++) {
if (Character.isLetter(chPass[i])) {
char_count++;
}
}
if (char_count >= 1) {
flag = true;
}
return flag;
}
public static boolean checkContainLowerCase(String password) {
char[] chPass = password.toCharArray();
boolean flag = false;
int char_count = 0;
for (int i = 0; i < chPass.length; i++) {
if (Character.isLowerCase(chPass[i])) {
char_count++;
}
}
if (char_count >= 1) {
flag = true;
}
return flag;
}
public static boolean checkContainUpperCase(String password) {
char[] chPass = password.toCharArray();
boolean flag = false;
int char_count = 0;
for (int i = 0; i < chPass.length; i++) {
if (Character.isUpperCase(chPass[i])) {
char_count++;
}
}
if (char_count >= 1) {
flag = true;
}
return flag;
}
public static boolean checkContainSpecialChar(String password) {
char[] chPass = password.toCharArray();
boolean flag = false;
int special_count = 0;
for (int i = 0; i < chPass.length; i++) {
if (config.SPECIAL_CHAR.indexOf(chPass[i]) != -1) {
special_count++;
}
}
if (special_count >= 1){
flag = true;
}
return flag;
}
public static boolean checkLateralKeyboardSite(String password) {
String t_password = new String(password);
//将所有输入字符转为小写
t_password = t_password.toLowerCase();
int n = t_password.length();
boolean flag = false;
int arrLen = config.KEYBOARD_HORIZONTAL_ARR.length;
int limit_num = Integer.parseInt(config.LIMIT_HORIZONTAL_NUM_KEY) ;
for(int i=0; i+limit_num<=n; i++) {
String str = t_password.substring(i, i+limit_num);
String distinguishStr = password.substring(i, i+limit_num);
for(int j=0; j<arrLen; j++) {
String configStr = config.KEYBOARD_HORIZONTAL_ARR[j];
String revOrderStr = new StringBuffer(config.KEYBOARD_HORIZONTAL_ARR[j]).reverse().toString();
//检测包含字母(区分大小写)
if ("enable".equals(config.CHECK_DISTINGGUISH_CASE)) {
//考虑 大写键盘匹配的情况
String UpperStr = config.KEYBOARD_HORIZONTAL_ARR[j].toUpperCase();
if((configStr.indexOf(distinguishStr) != -1) || (UpperStr.indexOf(distinguishStr) != -1)) {
flag = true;
return flag;
}
//考虑逆序输入情况下 连续输入
String revUpperStr = new StringBuffer(UpperStr).reverse().toString();
if((revOrderStr.indexOf(distinguishStr) != -1) || (revUpperStr.indexOf(distinguishStr) != -1)) {
flag = true;
return flag;
}
}else {
if(configStr.indexOf(str) != -1) {
flag = true;
return flag;
}
//考虑逆序输入情况下 连续输入
if(revOrderStr.indexOf(str) != -1) {
flag = true;
return flag;
}
}
}
}
return flag;
}
public static boolean checkKeyboardSlantSite(String password) {
String t_password = new String(password);
t_password = t_password.toLowerCase();
int n = t_password.length();
boolean flag = false;
int arrLen = config.KEYBOARD_SLOPE_ARR.length;
int limit_num = Integer.parseInt(config.LIMIT_SLOPE_NUM_KEY);
for(int i=0; i+limit_num<=n; i++) {
String str = t_password.substring(i, i+limit_num);
String distinguishStr = password.substring(i, i+limit_num);
for(int j=0; j<arrLen; j++) {
String configStr = config.KEYBOARD_SLOPE_ARR[j];
String revOrderStr = new StringBuffer(config.KEYBOARD_SLOPE_ARR[j]).reverse().toString();
//检测包含字母(区分大小写)
if ("enable".equals(config.CHECK_DISTINGGUISH_CASE)) {
//考虑 大写键盘匹配的情况
String UpperStr = config.KEYBOARD_SLOPE_ARR[j].toUpperCase();
if((configStr.indexOf(distinguishStr) != -1) || (UpperStr.indexOf(distinguishStr) != -1)) {
flag = true;
return flag;
}
//考虑逆序输入情况下 连续输入
String revUpperStr = new StringBuffer(UpperStr).reverse().toString();
if((revOrderStr.indexOf(distinguishStr) != -1) || (revUpperStr.indexOf(distinguishStr) != -1)) {
flag = true;
return flag;
}
}else {
if(configStr.indexOf(str) != -1) {
flag = true;
return flag;
}
//考虑逆序输入情况下 连续输入
if(revOrderStr.indexOf(str) != -1) {
flag = true;
return flag;
}
}
}
}
return flag;
}
public static boolean checkSequentialChars(String password) {
String t_password = new String(password);
boolean flag = false;
int limit_num = Integer.parseInt(config.LIMIT_LOGIC_NUM_CHAR);
int normal_count = 0;
int reversed_count = 0;
//检测包含字母(区分大小写)
if ("enable".equals(config.CHECK_DISTINGGUISH_CASE)) {
}else{
t_password = t_password.toLowerCase();
}
int n = t_password.length();
char[] pwdCharArr = t_password.toCharArray();
for (int i=0; i+limit_num<=n; i++) {
normal_count = 0;
reversed_count = 0;
for (int j=0; j<limit_num-1; j++) {
if (pwdCharArr[i+j+1]-pwdCharArr[i+j]==1) {
normal_count++;
if(normal_count == limit_num -1){
return true;
}
}
if (pwdCharArr[i+j]-pwdCharArr[i+j+1]==1) {
reversed_count++;
if(reversed_count == limit_num -1){
return true;
}
}
}
}
return flag;
}
public static boolean checkSequentialSameChars(String password) {
String t_password = new String(password);
int n = t_password.length();
char[] pwdCharArr = t_password.toCharArray();
boolean flag = false;
int limit_num = Integer.parseInt(config.LIMIT_NUM_SAME_CHAR);
int count = 0;
for (int i=0; i+limit_num<=n; i++) {
count=0;
for (int j=0; j<limit_num-1; j++) {
if(pwdCharArr[i+j] == pwdCharArr[i+j+1]) {
count++;
if (count == limit_num -1){
return true;
}
}
}
}
return flag;
}
public static boolean EvalPWD(String password) {
if (password == null || "".equals(password)) {
return false;
}
boolean flag = false;
if ("enable".equals(config.CHECK_PASSWORD_LENGTH)){
flag = checkPasswordLength(password);
if (!flag) {
return false;
}
}
if ("enable".equals(config.CHECK_CONTAIN_DIGIT)){
flag = checkContainDigit(password);
if (!flag) {
return false;
}
}
if ("enable".equals(config.CHECK_DISTINGGUISH_CASE)){
//检测包含小写字母
if ("enable".equals(config.CHECK_LOWER_CASE)){
flag = checkContainLowerCase(password);
if (!flag) {
return false;
}
}
//检测包含大写字母
if ("enable".equals(config.CHECK_UPPER_CASE)){
flag = checkContainUpperCase(password);
if (!flag) {
return false;
}
}
}else {
flag = checkContainCase(password);
if (!flag) {
return false;
}
}
if ("enable".equals(config.CHECK_CONTAIN_SPECIAL_CHAR)){
flag = checkContainSpecialChar(password);
if (!flag) {
return false;
}
}
if ("enable".equals(config.CHECK_HORIZONTAL_KEY_SEQUENTIAL)){
flag = checkLateralKeyboardSite(password);
if (flag) {
return false;
}
}
if ("enable".equals(config.CHECK_SLOPE_KEY_SEQUENTIAL)){
flag = checkKeyboardSlantSite(password);
if (flag) {
return false;
}
}
if ("enable".equals(config.CHECK_LOGIC_SEQUENTIAL)){
flag = checkSequentialChars(password);
if (flag) {
return false;
}
}
if ("enable".equals(config.CHECK_SEQUENTIAL_CHAR_SAME)){
flag = checkSequentialSameChars(password);
if (flag) {
return false;
}
}
return true;
}
}
package com.security.weakpassword;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Properties;
public class config {
public static String SYSTEM_GRADE;
public static String CHECK_PASSWORD_LENGTH;
public static String MIN_LENGTH;
public static String MAX_LENGTH;
public static String CHECK_CONTAIN_DIGIT;
public static String CHECK_DISTINGGUISH_CASE;
public static String CHECK_LOWER_CASE;
public static String CHECK_UPPER_CASE;
public static String CHECK_CONTAIN_SPECIAL_CHAR;
public static String DEFAULT_SPECIAL_CHAR="!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";
public static String SPECIAL_CHAR;
public static String CHECK_HORIZONTAL_KEY_SEQUENTIAL;
public static String LIMIT_HORIZONTAL_NUM_KEY;
public static String CHECK_SLOPE_KEY_SEQUENTIAL;
public static String LIMIT_SLOPE_NUM_KEY;
public static String CHECK_LOGIC_SEQUENTIAL;
public static String LIMIT_LOGIC_NUM_CHAR;
public static String CHECK_SEQUENTIAL_CHAR_SAME;
public static String LIMIT_NUM_SAME_CHAR;
public static String[] KEYBOARD_HORIZONTAL_ARR = {
"01234567890",
"qwertyuiop",
"asdfghjkl",
"zxcvbnm",
};
public static String[] KEYBOARD_SLOPE_ARR = {
"1qaz",
"2wsx",
"3edc",
"4rfv",
"5tgb",
"6yhn",
"7ujm",
"8ik,",
"9ol.",
"0p;/",
"=[;.",
"-pl,",
"0okm",
"9ijn",
"8uhb",
"7ygv",
"6tfc",
"5rdx",
"4esz"
};
static {
Properties prop = new Properties();
try{
//读取属性文件enc.properties
InputStream in = new BufferedInputStream(new FileInputStream("password.properties"));
prop.load(in);
Iterator<String> it=prop.stringPropertyNames().iterator();
while(it.hasNext()) {
String key = it.next();
if (key.equals("systemGrade")) {
SYSTEM_GRADE = prop.getProperty(key);
}
if (key.equals("checkPasswordLength")) {
CHECK_PASSWORD_LENGTH = prop.getProperty(key);
}
if (key.equals("limitPassMinLength")) {
MIN_LENGTH = prop.getProperty(key);
}
if (key.equals("limitPassMaxLength")) {
MAX_LENGTH = prop.getProperty(key);
}
if (key.equals("checkContainDigit")) {
CHECK_CONTAIN_DIGIT = prop.getProperty(key);
}
if (key.equals("checkContainUpperLowerCase")) {
CHECK_DISTINGGUISH_CASE = prop.getProperty(key);
}
if (key.equals("checkContainLowerCase")) {
CHECK_LOWER_CASE = prop.getProperty(key);
}
if (key.equals("checkContainUpperCase")) {
CHECK_UPPER_CASE = prop.getProperty(key);
}
if (key.equals("checkContainSpecialChar")) {
CHECK_CONTAIN_SPECIAL_CHAR = prop.getProperty(key);
}
if (key.equals("specialCharSet")) {
SPECIAL_CHAR = prop.getProperty(key);
}
if (key.equals("checkHorizontalKeySequential")) {
CHECK_HORIZONTAL_KEY_SEQUENTIAL = prop.getProperty(key);
}
if (key.equals("horizontalKeyLimitNum")) {
LIMIT_HORIZONTAL_NUM_KEY = prop.getProperty(key);
}
if (key.equals("checkSlopeKeySequential")) {
CHECK_SLOPE_KEY_SEQUENTIAL = prop.getProperty(key);
}
if (key.equals("slopeKeyLimitNum")) {
LIMIT_SLOPE_NUM_KEY = prop.getProperty(key);
}
if (key.equals("checkLogicSequential")) {
CHECK_LOGIC_SEQUENTIAL = prop.getProperty(key);
}
if (key.equals("logicLimitNum")) {
LIMIT_LOGIC_NUM_CHAR = prop.getProperty(key);
}
if (key.equals("checkSequentialCharSame")) {
CHECK_SEQUENTIAL_CHAR_SAME = prop.getProperty(key);
}
if (key.equals("sequentialCharNum")) {
LIMIT_NUM_SAME_CHAR = prop.getProperty(key);
}
}
in.close();
if("2".equals(SYSTEM_GRADE) || "3".equals(SYSTEM_GRADE) ) {
if("".equals(CHECK_PASSWORD_LENGTH)){
CHECK_PASSWORD_LENGTH = "enable";
MIN_LENGTH = "8";
MAX_LENGTH = "20";
}
if("".equals(CHECK_CONTAIN_DIGIT)) {
CHECK_CONTAIN_DIGIT = "enable";
}
if("".equals(CHECK_DISTINGGUISH_CASE)) {
CHECK_DISTINGGUISH_CASE = "disable";
}
if("".equals(CHECK_LOWER_CASE)) {
CHECK_LOWER_CASE = "enable";
}
if("".equals(CHECK_UPPER_CASE)) {
CHECK_UPPER_CASE = "enable";
}
if("".equals(CHECK_CONTAIN_SPECIAL_CHAR)) {
if("2".equals(SYSTEM_GRADE)) {
CHECK_CONTAIN_SPECIAL_CHAR = "disable";
}else{
CHECK_CONTAIN_SPECIAL_CHAR = "enable";
if("".equals(SPECIAL_CHAR)) {
SPECIAL_CHAR = DEFAULT_SPECIAL_CHAR;
}
}
}
if("".equals(CHECK_HORIZONTAL_KEY_SEQUENTIAL)) {
CHECK_HORIZONTAL_KEY_SEQUENTIAL = "enable";
if("2".equals(SYSTEM_GRADE)) {
LIMIT_HORIZONTAL_NUM_KEY = "4";
}else{
LIMIT_HORIZONTAL_NUM_KEY = "3";
}
}
if("".equals(CHECK_SLOPE_KEY_SEQUENTIAL)) {
CHECK_SLOPE_KEY_SEQUENTIAL = "enable";
if("2".equals(SYSTEM_GRADE)) {
LIMIT_SLOPE_NUM_KEY = "4";
}else{
LIMIT_SLOPE_NUM_KEY = "3";
}
}
if("".equals(CHECK_LOGIC_SEQUENTIAL)) {
CHECK_LOGIC_SEQUENTIAL = "enable";
if("2".equals(SYSTEM_GRADE)) {
LIMIT_LOGIC_NUM_CHAR = "4";
}else{
LIMIT_LOGIC_NUM_CHAR = "3";
}
}
if("".equals(CHECK_SEQUENTIAL_CHAR_SAME)) {
CHECK_SEQUENTIAL_CHAR_SAME = "enable";
if("2".equals(SYSTEM_GRADE)) {
LIMIT_NUM_SAME_CHAR = "4";
}else{
LIMIT_NUM_SAME_CHAR = "3";
}
}
}else{
SYSTEM_GRADE = "3";
CHECK_PASSWORD_LENGTH = "enable";
MIN_LENGTH = "8";
MAX_LENGTH = "20";
CHECK_CONTAIN_DIGIT = "enable";
CHECK_LOWER_CASE = "enable";
CHECK_UPPER_CASE = "enable";
CHECK_CONTAIN_SPECIAL_CHAR = "enable";
CHECK_HORIZONTAL_KEY_SEQUENTIAL = "enable";
LIMIT_HORIZONTAL_NUM_KEY = "3";
CHECK_SLOPE_KEY_SEQUENTIAL = "enable";
LIMIT_SLOPE_NUM_KEY = "3";
CHECK_LOGIC_SEQUENTIAL = "enable";
LIMIT_LOGIC_NUM_CHAR = "3";
CHECK_SEQUENTIAL_CHAR_SAME = "enable";
LIMIT_NUM_SAME_CHAR = "3";
}
}
catch(Exception e){
System.out.println(e);
}
}
}
import com.security.weakpassword.CheckPWD;
public class TestDemo {
public static void main(String[] args) {
System.out.println("this is a test code");
boolean flag = false;
String[] testPass = {
null,
"",
"123456",
"12345678",
"abcdefgh",
"123abc456",
"1231adf@",
"12341adf@",
"fdahuier243335ddfa#$*&",
"aBcd1859d4!@",
"zaq13edfgt#",
"Bgt5sj4#"
};
for (int i = 0; i < testPass.length; i++) {
System.out.printf("testpass[%d] = %s\n", i,testPass[i]);
flag = CheckPWD.EvalPWD(testPass[i]);
if (flag) {
System.out.println("secret pass.\n");
} else {
System.out.println("secret failed.\n");
}
}
}
}
5. 配置文件
#保护系统级别 系统级别分为二级系统和三级系统
#当systemGrade不为2或者3时,系统默认使用系统级别2的规则
#当systemGrade设置为2或者3时,其他选项若未配置,则按照默认值处理,具体默认值信息见每一项说明
systemGrade=2
#检测密码口令长度
#当checkPasswordLength为disable时,下面两项设置无效
#其他检测功能设置为disable时,对应设置项设置数据无效
#当系统级别为2或3时,checkPasswordLength默认为enable
#当系统级别为2或3时,limitPassMinLength默认为8,limitPassMaxLength默认为20
#是否检测密码口令长度,如为enable则检测,如为disable则不检测,如为空,
checkPasswordLength=enable
#允许密码口令最小长度
limitPassMinLength=8
#允许密码口令最大长度,如为空,则不设置上限
limitPassMaxLength=20
#是否包含数字、小写字母、大写字母、特殊符号
#当设置checkContainUpperLowerCase为enable时,区分大小写
#当系统级别为2时:checkContainDigit默认为enable,checkContainUpperLowerCase默认为disable
#当系统级别为2时,checkContainLowerCase默认为disable,checkContainUpperCase默认为disable
#当系统级别为2时,checkContainSpecialChar默认为disable
#当系统级别为3时:checkContainDigit默认为enable,checkContainUpperLowerCase默认为disable
#当系统级别为3时,checkContainLowerCase默认为disable,checkContainUpperCase默认为disable
#当系统级别为3时,checkContainSpecialChar默认为enable
#是否检测包含数字
checkContainDigit=enable
#是否检测包含大小写字母,区分密码口令大小写
checkContainUpperLowerCase = disable
#是否检测包含小写字母
checkContainLowerCase = enable
#是否检测包含大写字母
checkContainUpperCase = enable
#是否检测包含特殊符号
checkContainSpecialChar=disable
#支持自定义特殊符号集合
specialCharSet="!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"
#检测键盘按键连续 比如qwer、1qaz、0okm等
#允许横向最大连续num为7,最小为3
#允许斜向最大连续num为4,最小为3
#当系统级别为2时:checkHorizontalKeySequential默认为enable,horizontalKeyLimitNum默认为4
#当系统级别为2时,checkSlopeKeySequential默认为enable,slopeKeyLimitNum默认为4
#当系统级别为3时:checkHorizontalKeySequential默认为enable,horizontalKeyLimitNum默认为3
#当系统级别为3时,checkSlopeKeySequential默认为enable,slopeKeyLimitNum默认为3
#是否检测键盘横向连续
checkHorizontalKeySequential=enable
#允许键盘横向连续最小数值,如为空,则设置为默认值
horizontalKeyLimitNum=4
#是否检测键盘斜向连续
checkSlopeKeySequential=enable
#允许键盘斜向连续最小数值,如为空,则设置为默认值
slopeKeyLimitNum=4
#检测逻辑位置连续 比如1234、abcd等
#当系统级别为2时,checkLogicSequential默认为enable,logicLimitNum默认为4
#当系统级别为3时,checkLogicSequential默认为enable,logicLimitNum默认为3
#是否检测逻辑位置连续
checkLogicSequential=enable
#允许逻辑位置连续最小数值,如为空,则设置为默认值
logicLimitNum=4
#检测相邻字符相同 比如aaaa、2222、@@@@等
#当系统级别为2时,checkSequentialCharSame默认为enable,sequentialCharNum默认为4
#当系统级别为3时,checkSequentialCharSame默认为enable,sequentialCharNum默认为3
#是否检测相邻字符相同
checkSequentialCharSame=enable
#允许相邻位置相同最小连续数,如为空,则设置为默认值
sequentialCharNum=4
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。