您的位置:首页 > 其它

【华为OJ】【086-密码强度等级】

2016-05-20 06:53 387 查看

【华为OJ】【算法总篇章】

【华为OJ】【086-密码强度等级】

【工程下载】

题目描述

密码按如下规则进行计分,并根据不同的得分为密码进行安全等级划分。
一、密码长度:
5 分: 小于等于4 个字符
10 分: 5 到7 字符
25 分: 大于等于8 个字符
二、字母:
0 分: 没有字母
10 分: 全都是小(大)写字母
20 分: 大小写混合字母
三、数字:
0 分: 没有数字
10 分: 1 个数字
20 分: 大于1 个数字
四、符号:
0 分: 没有符号
10 分: 1 个符号
25 分: 大于1 个符号
五、奖励:
2 分: 字母和数字
3 分: 字母、数字和符号
5 分: 大小写字母、数字和符号
最后的评分标准:
>= 90: 非常安全
>= 80: 安全(Secure)
>= 70: 非常强
>= 60: 强(Strong)
>= 50: 一般(Average)
>= 25: 弱(Weak)
>= 0:  非常弱

对应输出为:
VERY_WEAK,
WEAK,
AVERAGE,
STRONG,
VERY_STRONG,
SECURE,
VERY_SECURE

请根据输入的密码字符串,进行安全评定。
注:
字母:a-z, A-Z
数字:-9
符号包含如下: (ASCII码表可以在UltraEdit的菜单view->ASCII Table查看)
!"#$%&'()*+,-./     (ASCII码:x21~0x2F)
:;<=>?@             (ASCII<=><=><=><=><=>码:x3A~0x40)
[\]^_`              (ASCII码:x5B~0x60)
{|}~                (ASCII码:x7B~0x7E)

接口描述:

Input Param : String pPasswordStr:    密码,以字符串方式存放。
Return Value : 根据规则评定的安全等级。
public static Safelevel GetPwdSecurityLevel(String pPasswordStr) {
/*在这里实现功能*/
return null;
}


输入描述

输入一个string的密码


输出描述

输出密码等级


输入例子

38$@NoNoNo


输出例子

VERY_SECURE


算法实现

import java.util.Scanner;

/**
* Author: 王俊超
* Date: 2016-01-04 10:08
* Declaration: All Rights Reserved !!!
*/
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
//        Scanner scanner = new Scanner(Main.class.getClassLoader().getResourceAsStream("data.txt"));
while (scanner.hasNext()) {
String s = scanner.nextLine();
System.out.println(getPwdSecurityLevel(s));
}

scanner.close();
}

private static String getPwdSecurityLevel(String s) {
int len = s.length();
int num = 0;
int lowerCase = 0;
int upperCase = 0;
int ch = 0;
int score = 0;

for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);

if (c >= '0' && c <= '9') {
num++;
} else if (c >= 'A' && c <= 'Z') {
upperCase++;
} else if (c >= 'a' && c <= 'z') {
lowerCase++;
} else if (c >= 0x21 && c <= 0x2F || c >= 0x3A && c <= 0x40
|| c >= 0X5B && c <= 0x60 || c >= 0x7B && c <= 0x7E) {
ch++;
}
}

// 一、密码长度
if (len <= 4) {
score += 5;
} else if (len <= 7) {
score += 10;
} else {
score += 25;
}

// 二、字母
if (lowerCase > 0) {
score += 10;
}

if (upperCase > 0) {
score += 10;
}

// 三、数字
if (num == 1) {
score += 10;
} else if (num > 1) {
score += 20;
}

// 四、符号
if (ch == 1) {
score += 10;
} else if (ch > 1) {
score += 25;
}

if (num > 0 && (upperCase > 0 || lowerCase > 0)) {
score += 2;
if (ch > 0) {
score += 1;

if (upperCase > 0 && lowerCase > 0) {
score += 2;
}
}
}

if (score >= 90) {
return "VERY_SECURE";
} else if (score >= 80) {
return "SECURE";
} else if (score >= 70) {
return "VERY_STRONG";
} else if (score > 60) {
return "STRONG";
} else if (score >= 50) {
return "AVERAGE";
} else if (score >= 25) {
return "WEAK";
} else {
return "VERY_WEAK";
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: