2011-05-27 110 views
12

如何检查使用.NET Framework密码(作为string)的实力?如何检查密码强度?

+0

你已经找到了那些生产一致性和可比较的结果? – 2011-05-27 12:36:17

+0

你的实力标准是什么?你能更清楚地知道你想要测试什么吗? – 2014-09-29 12:49:01

+0

我的标准是熵 - 没有答案满足那个。根据[加密上的这个问题](https://crypto.stackexchange.com/questions/374/how-should-i-calculate-the-entropy-of-a-password)香农是理论上的解决方案。在实践中,你还必须从字典/维基百科/技术语言中消除所有内容。 – mbx 2017-06-09 09:40:08

回答

7

基本的,但逻辑之一:

enum PasswordScore 
{ 
    Blank = 0, 
    VeryWeak = 1, 
    Weak = 2, 
    Medium = 3, 
    Strong = 4, 
    VeryStrong = 5 
} 

public class PasswordAdvisor 
{ 
    public static PasswordScore CheckStrength(string password) 
    { 
     int score = 1; 

     if (password.Length < 1) 
      return PasswordScore.Blank; 
     if (password.Length < 4) 
      return PasswordScore.VeryWeak; 

     if (password.Length >= 8) 
      score++; 
     if (password.Length >= 12) 
      score++; 
     if (Regex.Match(password, @"/\d+/", RegexOptions.ECMAScript)) 
      score++; 
     if (Regex.Match(password, @"/[a-z]/", RegexOptions.ECMAScript) && 
      Regex.Match(password, @"/[A-Z]/", RegexOptions.ECMAScript)) 
      score++; 
     if (Regex.Match(password, @"/.[!,@,#,$,%,^,&,*,?,_,~,-,£,(,)]/", RegexOptions.ECMAScript)) 
      score++; 

     return (PasswordScore)score; 
    } 
} 

编号:http://passwordadvisor.com/CodeAspNet.aspx

+0

'password123'是一个中等密码?当你在0而不是1,更好地利用类似https://github.com/dropbox/zxcvbn – Tieme 2016-02-13 00:12:32

1

这是我用的,将它移植到.NET应该不会很辛苦一个简单的JavaScript例如,

var getStrength = function (passwd) { 
    intScore = 0; 
    intScore = (intScore + passwd.length); 
    if (passwd.match(/[a-z]/)) { 
     intScore = (intScore + 1); 
    } 
    if (passwd.match(/[A-Z]/)) { 
     intScore = (intScore + 5); 
    } 
    if (passwd.match(/\d+/)) { 
     intScore = (intScore + 5); 
    } 
    if (passwd.match(/(\d.*\d)/)) { 
     intScore = (intScore + 5); 
    } 
    if (passwd.match(/[!,@#$%^&*?_~]/)) { 
     intScore = (intScore + 5); 
    } 
    if (passwd.match(/([!,@#$%^&*?_~].*[!,@#$%^&*?_~])/)) { 
     intScore = (intScore + 5); 
    } 
    if (passwd.match(/[a-z]/) && passwd.match(/[A-Z]/)) { 
     intScore = (intScore + 2); 
    } 
    if (passwd.match(/\d/) && passwd.match(/\D/)) { 
     intScore = (intScore + 2); 
    } 
    if (passwd.match(/[a-z]/) && passwd.match(/[A-Z]/) && passwd.match(/\d/) && passwd.match(/[!,@#$%^&*?_~]/)) { 
     intScore = (intScore + 2); 
    } 
    return intScore; 
} 
3

如果我可以展示我的定制化实施的例子,如泰奥曼Soygul的(和其他人我见过像他)......我的实现有不同的计分方案,并使用分imum要求以及重复字符的检查。

public enum PasswordScore 
{ 
    Blank = 0, 
    TooShort = 1, 
    RequirementsNotMet = 2, 
    VeryWeak = 3, 
    Weak = 4, 
    Fair = 5, 
    Medium = 6, 
    Strong = 7, 
    VeryStrong = 8 
} 

public static PasswordScore CheckStrength(string password) 
    { 
     int score = 0; 

     // using three requirements here: min length and two types of characters (numbers and letters) 
     bool blnMinLengthRequirementMet = false; 
     bool blnRequirement1Met = false; 
     bool blnRequirement2Met = false; 

     // check for chars in password 
     if (password.Length < 1) 
      return PasswordScore.Blank; 

     // if less than 6 chars, return as too short, else, plus one 
     if (password.Length < 6) 
     { 
      return PasswordScore.TooShort; 
     } 
     else 
     { 
      score++; 
      blnMinLengthRequirementMet = true; 
     } 

     // if 8 or more chars, plus one 
     if (password.Length >= 8) 
      score++; 

     // if 10 or more chars, plus one 
     if (password.Length >= 10) 
      score++; 

     // if password has a number, plus one 
     if (Regex.IsMatch(password, @"[\d]", RegexOptions.ECMAScript)) 
     { 
      score++; 
      blnRequirement1Met = true; 
     } 

     // if password has lower case letter, plus one 
     if (Regex.IsMatch(password, @"[a-z]", RegexOptions.ECMAScript)) 
     { 
      score++; 
      blnRequirement2Met = true; 
     } 

     // if password has upper case letter, plus one 
     if (Regex.IsMatch(password, @"[A-Z]", RegexOptions.ECMAScript)) 
     { 
      score++; 
      blnRequirement2Met = true; 
     } 

     // if password has a special character, plus one 
     if (Regex.IsMatch(password, @"[~`[email protected]#$%\^\&\*\(\)\-_\+=\[\{\]\}\|\\;:'\""<\,>\.\?\/£]", RegexOptions.ECMAScript)) 
      score++; 

     // if password is longer than 2 characters and has 3 repeating characters, minus one (to minimum of score of 3) 
     List<char> lstPass = password.ToList(); 
     if (lstPass.Count >= 3) 
     { 
      for (int i = 2; i < lstPass.Count; i++) 
      { 
       char charCurrent = lstPass[i]; 
       if (charCurrent == lstPass[i - 1] && charCurrent == lstPass[i - 2] && score >= 4) 
       { 
        score++; 
       } 
      } 
     } 

     if (!blnMinLengthRequirementMet || !blnRequirement1Met || !blnRequirement2Met) 
     { 
      return PasswordScore.RequirementsNotMet; 
     } 

     return (PasswordScore)score; 
    } 
4

这里有一个简单的我已经写了:

/// <summary> 
/// Evaluates a password 
/// </summary> 
public class PasswordEvaluator 
{ 
    public string Password { get; private set; } 
    public int Length { get; private set; } 
    public int TotalNumberChars { get; private set; } 
    public bool ContainsNumberChars{get { return TotalNumberChars > 0; }} 
    public int TotalUppercaseChars { get; private set; } 
    public bool ContainsUppercaseChars { get { return TotalUppercaseChars > 0; } } 
    public int TotalLowercaseChars { get; private set; } 
    public bool ContainsLowercaseChars { get { return TotalLowercaseChars > 0; } } 
    public int TotalSpecialChars { get; private set; } 
    public bool ContainsSpecialChars { get { return TotalSpecialChars > 0; } } 

    public PasswordEvaluator(string password) 
    { 
     Password = password.Trim(); 
     Length = Password.Length; 
     foreach (var c in Password) 
     { 
      var charCode = (int)c; 
      if (charCode >= 48 && charCode <= 57) TotalNumberChars++; 
      else if (charCode >= 65 && charCode <= 90) TotalUppercaseChars++; 
      else if (charCode >= 97 && charCode <= 122) TotalLowercaseChars++; 
      else TotalSpecialChars++; 
     } 
    } 
    public bool StrongEnough() 
    { 
     // Minimum length requirement 
     if (Length < Settings.PasswordMinLength) return false; 

     // Mixed case requirement 
     if (!ContainsLowercaseChars && !ContainsUppercaseChars) return false; 

     // Special chars requirement 
     if (TotalSpecialChars < 3) return false; 

     // Min lower case chars requirement 
     if (TotalLowercaseChars < 3) return false; 

     // Min upper case chars requirement 
     if (TotalUppercaseChars < 3) return false; 

     return true; 
    } 
} 

可以在StrongEnough()

+0

这是远远优于许多其他的答案,或者至少从东西这种实现衍生开始得分好一点。期望的是获得一个对象,记录有关密码的有用信息,从中可以做出明智的决定。虽然在这种情况下这并不重要(甚至不是瓶颈),但这种方式的性能也大大提高。我看到上面的正则表达式实现,通过重新运行正则表达式来重复测试某个特定情况的字母是否存在!更好的办法是获取一次信息:是否有大写字母等等。还可以添加其他检查: – 2017-10-18 22:59:06

+0

如:'passwd.match(/(\ d。* \ d)/)' - 我认为这是从上面的答案之一是问:一个数字或更多是否出现,然后是另一个数字(然而,这个正则表达式实际上可能会匹配两行数字,您可以使用!重写!)。在这个C#的情况下,人们可以不用正则表达式来计算出这种情况,记录:大小写不止一次出现不是大写,而是小写和数字相同。无论如何,这是最好的方法,你有一个记录来作出决定。 – 2017-10-18 23:02:50

0

定义自己的规则,这是基于信息熵对密码强度检查用我自己的代码和NIST指南。但是这种方法并没有考虑到“人”语言因素。密码的

public enum PasswordScore 
{ 
    Blank, 
    VeryWeak, 
    Weak, 
    Medium, 
    Strong, 
    VeryStrong 
} 

public static PasswordScore CheckPasswordStrength(string password) 
{ 
    int N = 0; 
    int L = password.Length; 
    if (L == 0) 
     return PasswordScore.Blank; 
    if (Regex.IsMatch(password, @"[\d]", RegexOptions.ECMAScript)) 
     N += 10; 
    if (Regex.IsMatch(password, @"[a-z]", RegexOptions.ECMAScript)) 
     N += 26; 
    if (Regex.IsMatch(password, @"[A-Z]", RegexOptions.ECMAScript)) 
     N += 26; 
    if (Regex.IsMatch(password, @"[~`[email protected]#$%\^\&\*\(\)\-_\+=\[\{\]\}\|\\;:'\""<\,>\.\?\/£]", RegexOptions.ECMAScript) && password.Length > 8) 
     N += 33; 
    int H = Convert.ToInt32(L * (Math.Round(Math.Log(N)/Math.Log(2)))); 
    if (H <= 32) return PasswordScore.VeryWeak; 
    if (H <= 48) return PasswordScore.Weak; 
    if (H <= 64) return PasswordScore.Medium; 
    if (H <= 80) return PasswordScore.Strong; 
    return PasswordScore.VeryStrong; 
} 
0

强度应该代表的几个参数,如特殊字符和数字,密码长度存在检查等

我发现下面的教程,很好的演示:

http://tinytute.com/2014/06/03/animated-password-strength-checker-quick-easy/

jQuery的代码块:

$(document).ready(function(){ 

    $("#textBox").keyup(function(){ 

     var passWord = $("#textBox").val(); 
     var passLength = passWord.length; 
     var specialFlag = 0; 
     var numberFlag = 0; 
     var numberGenerator = 0; 
     var total = 0; 

     if(/^[a-zA-Z0-9- ]*$/.test(passWord) == false) { 

      specialFlag =20; 
     } 


     if(passWord.match(/[0-9]/)) { 

      numberFlag = 25; 
     } 

     if(passLength>4&&passLength<=6){ 
      numberGenerator =25; 
     }else if(passLength>=7&&passLength<=9){ 
      numberGenerator =35; 
     }else if(passLength>9){ 
      numberGenerator =55; 
     }else if(passLength>0&&passLength<=4){ 
      numberGenerator =15; 
     }else{ 
      numberGenerator =0; 
     } 

     total = numberGenerator + specialFlag + numberFlag; 
     if(total<30){ 
      $('#progressBar').css('background-color','#CCC'); 
     }else if(total<60&&total>=30){ 

      $('#progressBar').css('background-color','#FF6600'); 

     }else if(total>=60&&total<90){ 

      $('#progressBar').css('background-color','#FFCC00'); 

     }else if(total>=90){ 

      $('#progressBar').css('background-color','#0f0'); 

     } 
     $('#progressBar').css('width',total+'%'); 

    }); 

}); 
5

“密码强度”是一个相当通用的术语,它可能意味着密码字符数,使用的字符范围(基数),破解密码所需的时间(暴力破解)等。

其中一种最好的方法衡量一个密码的加密强度是计算密码多少entropy位有(虽然这通常是衡量随机密码更准确。你会得到否则的估计熵结果),

// Only accurate for passwords in ASCII. 
public double CalculateEntropy(string password) 
{ 
    var cardinality = 0; 

    // Password contains lowercase letters. 
    if (password.Any(c => char.IsLower(c))) 
    { 
     cardinality = 26; 
    } 

    // Password contains uppercase letters. 
    if (password.Any(c => char.IsUpper(c))) 
    { 
     cardinality += 26; 
    } 

    // Password contains numbers. 
    if (password.Any(c => char.IsDigit(c))) 
    { 
     cardinality += 10; 
    } 

    // Password contains symbols. 
    if (password.IndexOfAny("\\|¬¦`!\"£$%^&*()_+-=[]{};:'@#~<>,./? ".ToCharArray()) >= 0) 
    { 
     cardinality += 36; 
    } 

    return Math.Log(cardinality, 2) * password.Length; 
}