2010-01-20 80 views
0
  • 分3個字母
  • 最多15個
  • 字符A-ZA-Z0-9
  • 特殊的字符@#$%^ & + =

這是什麼我有:正則表達式驗證密碼

Regex.IsMatch(Password.Text, @"^[[email protected]#$%^&+=]{3,15}$ ") 

它總是返回false。

請幫忙。

+0

Offtopic,但如果我想用什麼'<我的密碼>'爲了更安全?或超過15個字符? (實際上我的密碼比超級安全的密碼長) – Earlz 2010-01-20 00:11:16

+1

@aloo - 你在A和Z之間有一個下劃線 - 所以你在B-Y上不匹配;還有215 - 應該是15?還是那些張貼錯別字? – 2010-01-20 00:12:01

+2

'A-Z'而不是'A_Z' – moonshadow 2010-01-20 00:12:10

回答

3

取出正則表達式字符串末尾的空格。另外:更正了可能的錯別字。

Regex.IsMatch(Password.Text, @"^[[email protected]#$%^&+=]{3,15}$") 
+0

他是不是也意味着它成爲'^ [AZ ...'? – Earlz 2010-01-20 00:22:38

1

爲什麼地球上你想限制可能的密碼?!?

  • 爲什麼禁止法語和德語字符(unicode一般)?
  • 爲什麼將密碼限制爲15個字符?很多人使用整個密碼。
  • 爲什麼要限制特殊字符?你爲什麼排除.:
+1

+1對於您的帖子。 -1不回答真正的問題。 – Earlz 2010-01-20 00:22:10

1

如前所述,這是一個非常非常好的。更好的方法是測試輸入密碼的密碼強度並設置分數密碼必須擊敗。

有算法來計算密碼的強度。從德爾福加密綱要由哈根Reddmann採取以下(以帕斯卡的這樣,但我想這可以很容易地被翻譯)

function PassphraseQuality(const Password: String): Extended; 
// returns computed Quality in range 0.0 to 1.0 
// source extracted from Delphi Encryption Compendium, DEC 

    function Entropy(P: PByteArray; L: Integer): Extended; 
    var 
    Freq: Extended; 
    I: Integer; 
    Accu: array[Byte] of LongWord; 
    begin 
    Result := 0.0; 
    if L <= 0 then Exit; 
    FillChar(Accu, SizeOf(Accu), 0); 
    for I := 0 to L-1 do Inc(Accu[P[I]]); 
    for I := 0 to 255 do 
     if Accu[I] <> 0 then 
     begin 
     Freq := Accu[I]/L; 
     Result := Result - Freq * (Ln(Freq)/Ln(2)); 
     end; 
    end; 

    function Differency: Extended; 
    var 
    S: String; 
    L,I: Integer; 
    begin 
    Result := 0.0; 
    L := Length(Password); 
    if L <= 1 then Exit; 
    SetLength(S, L-1); 
    for I := 2 to L do 
     Byte(S[I-1]) := Byte(Password[I-1]) - Byte(Password[I]); 
    Result := Entropy(Pointer(S), Length(S)); 
    end; 

    function KeyDiff: Extended; 
    const 
    Table = '^1234567890ß´qwertzuiopü+asdfghjklöä#<yxcvbnm,.-°!"§$%&/()=?`QWERTZUIOPÜ*ASDFGHJKLÖÄ''>YXCVBNM;:_'; 
    var 
    S: String; 
    L,I,J: Integer; 
    begin 
    Result := 0.0; 
    L := Length(Password); 
    if L <= 1 then Exit; 
    S := Password; 
    UniqueString(S); 
    for I := 1 to L do 
    begin 
     J := Pos(S[I], Table); 
     if J > 0 then S[I] := Char(J); 
    end; 
    for I := 2 to L do 
     Byte(S[I-1]) := Byte(S[I-1]) - Byte(S[I]); 
    Result := Entropy(Pointer(S), L-1); 
    end; 

const 
    GoodLength = 10.0; // good length of Passphrases 
var 
    L: Extended; 
begin 
    Result := Entropy(Pointer(Password), Length(Password)); 
    if Result <> 0 then 
    begin 
    Result := Result * (Ln(Length(Password))/Ln(GoodLength)); 
    L := KeyDiff + Differency; 
    if L <> 0 then L := L/64; 
    Result := Result * L; 
    if Result < 0 then Result := -Result; 
    if Result > 1 then Result := 1; 
    end; 
end; 
+1

確實。 Ruby的'密碼'庫可以很容易地檢查密碼強度。該庫調用cracklib,它比大多數程序員想做的事情做得更多更好的檢查。例如,它會檢查密碼不是字典單詞。 – 2010-01-20 00:25:55

+0

我的目的是防止跨瀏覽器腳本。如果任何人都可以告訴我如何爲密碼字段做到這一點。 – 2010-01-20 00:29:09

+0

@aloo:如果這個密碼永遠不會顯示在一個頁面上(**應該永遠不會是**),那麼跨站點腳本不是問題。真正的問題是SQL注入(http://en.wikipedia.org/wiki/SQL_injection),您可以通過編寫適當的SQL查詢來防止這些問題(請參閱wiki頁面)。 – 2010-01-20 00:34:38